Sanitas

Built by members

Project Overview

The problem

Questions about healthcare operations usually need two very different kinds of data: structured relationships (which doctors work at which hospitals, which insurers each hospital accepts, when visits happened) and unstructured text (patient reviews, hospital policy documents).

Vector-only RAG pipelines tend to break on multi-hop relational questions, like counting visits in a given department or checking whether a physician accepts a particular insurer. Sanitas handles them with an agent that can choose between graph database queries, semantic vector search, document retrieval and purpose-built Python tools.

Architecture

Three containerized services, orchestrated with Docker Compose:

  1. ETL pipeline (hospital_neo4j_etl): a Python script that loads CSVs of hospitals, physicians, patients, visits and reviews, and builds a connected graph in Neo4j AuraDB.
  2. Chatbot API (chatbot_api): a FastAPI app hosting a LangChain agent powered by Google Gemini 2.0 Flash models (HOSPITAL_AGENT_MODEL, HOSPITAL_CYPHER_MODEL, HOSPITAL_QA_MODEL).
  3. User interface (chatbot_frontend): a Streamlit app that provides the chat UI.

Graph schema and safe Cypher

The graph stores Hospital, Physician, Patient, Visit, Payer and Review nodes, connected by relationships such as WORKS_AT, TREATS and COVERED_BY.

Questions that need counts or traversals go through LangChain's GraphCypherQAChain, which writes a Cypher query from the user's prompt.

Since an LLM translating user input could generate a query that modifies data, every generated statement first passes through a custom validator, ReadOnlyCorrector, which rejects anything containing a write keyword:

class ReadOnlyCorrector(CypherQueryCorrector):
    """Ensures generated queries contain only read-only statements."""
    def __call__(self, query: str) -> str:
        forbid = ["CREATE", "DELETE", "SET ", "REMOVE", "DROP", "CALL"]
        upper = query.upper()
        for word in forbid:
            if word in upper:
                raise ValueError(f"Unsafe keyword '{word}' in Cypher: {query}")
        return query

The agent's tools

Depending on the question, the LangChain agent picks one of these tools:

  • Cypher graph queries: for objective, quantitative questions about counts, relationships, insurance networks and physician specialties.
  • Patient review search: a vector chain over review embeddings, for qualitative questions about satisfaction, bedside manner and overall experience.
  • Knowledge base retrieval: reads local text files with institutional policies, emergency procedures and service overviews.
  • Live operational tools: Python functions that compute current wait times (get_current_wait_times) and point patients to the hospital with the most availability right now (get_most_available_hospital).