# dv1712/clinicscribe architecture

> Architecture diagram of the GitHub repository dv1712/clinicscribe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dv1712/clinicscribe
- Repository: https://github.com/dv1712/clinicscribe

## Overview

#### Purpose and Entry Points
ClinicScribe is a Python RAG prototype for answering medical-literature questions from a local textbook corpus. The interactive application entry point is `clinicScribe.py`. Offline knowledge-base preparation is separated into `create_memory_for_llm.py` and `connect_memory_with_llm.py`.

#### Source Knowledge Corpus
The initial authoritative source is a local medical PDF: `data/The_GALE_ENCYCLOPEDIA_of_MEDICINE_SECOND.pdf`. It is loaded, text-extracted, and chunked during indexing rather than queried directly at chat time. The system is explicitly educational/research-oriented, not a clinical diagnostic system.

#### Indexing and Embedding Stage
`create_memory_for_llm.py` builds the retrieval memory: document chunks are converted into HuggingFace embedding vectors and persisted as a FAISS index. This is the offline boundary between expensive document processing and interactive question answering.

Persisted artifacts are under:
- `vectorstore/db_faiss/index.faiss`
- `vectorstore/db_faiss/index.pkl`

These files allow the serving path to reuse indexed content without reprocessing the PDF.

#### Retrieval and Generation Pipeline
`connect_memory_with_llm.py` connects the persisted FAISS store to the LLM-facing RAG chain. For each user question, the pipeline performs similarity retrieval over embedded textbook chunks, supplies retrieved context to the language model, and returns a grounded generated response.

This component is the main integration boundary between LangChain orchestration, HuggingFace embedding/model access, FAISS retrieval, and Groq-accelerated inference.

#### Interactive Application Layer
`clinicScribe.py` provides the Streamlit chat interface. It accepts user questions, invokes the configured retrieval/LLM pipeline, and renders responses. It should consume the existing vector store at `vectorstore/db_faiss/` rather than own indexing responsibilities.

#### Runtime and External Dependencies
The project runs as a Python application with Streamlit as the UI runtime. LangChain coordinates document retrieval and prompt/LLM flow; FAISS provides local vector similarity search; HuggingFace supplies open-source model or embedding access; Groq is the external accelerated LLM inference service. Network/API credentials are therefore likely required for the inference path, while corpus and vector index storage are local.

#### Primary Control Flow
1. Run `create_memory_for_llm.py` when the source PDF or embedding strategy changes.
2. Persist embeddings and chunk metadata to `vectorstore/db_faiss/`.
3. Start `clinicScribe.py` for interactive use.
4. User question flows through `connect_memory_with_llm.py`-style RAG wiring: retrieve FAISS neighbors, construct LLM context, invoke inference, display answer.

Main entry points: [`connect_memory_with_llm.py`](https://github.com/dv1712/clinicscribe/blob/main/connect_memory_with_llm.py) (RAG chain wiring), [`create_memory_for_llm.py`](https://github.com/dv1712/clinicscribe/blob/main/create_memory_for_llm.py) (Memory builder), [`clinicScribe.py`](https://github.com/dv1712/clinicscribe/blob/main/clinicScribe.py) (ClinicScribe chat UI), [`vectorstore/db_faiss/index.faiss`](https://github.com/dv1712/clinicscribe/blob/main/vectorstore/db_faiss/index.faiss) (FAISS vector index)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline indexing"]
  node_pdf["Medical textbook PDF<br/>source corpus"]
  node_indexer["Memory builder<br/>indexing script"]
  node_faiss_index[("FAISS vector index<br/>local retrieval store<br/>[index.faiss]")]
  node_faiss_metadata["FAISS metadata<br/>persisted metadata<br/>[index.pkl]"]
end

subgraph group_interactive["Interactive RAG"]
  node_streamlit_app["ClinicScribe chat UI<br/>Streamlit application<br/>[clinicScribe.py]"]
  node_user(("User"))
  node_rag_connector["RAG chain wiring<br/>retrieval and generation module"]
  node_retriever["Similarity retrieval<br/>FAISS retriever"]
  node_context_builder["Grounded context<br/>prompt context stage"]
  node_answer["Generated answer<br/>grounded response"]
end

subgraph group_external["External services"]
  node_hf_embeddings["HuggingFace embeddings<br/>embedding provider"]
  node_langchain["LangChain<br/>RAG orchestration"]
  node_groq{{"Groq inference<br/>external LLM service"}}
end

node_pdf -->|"extract and chunk"| node_indexer
node_indexer -->|"create vectors"| node_hf_embeddings
node_hf_embeddings -->|"embeddings"| node_indexer
node_indexer -->|"persist vectors"| node_faiss_index
node_indexer -->|"persist chunk metadata"| node_faiss_metadata
node_user -->|"asks question"| node_streamlit_app
node_streamlit_app -->|"invoke RAG"| node_rag_connector
node_rag_connector -->|"load existing store"| node_faiss_index
node_rag_connector -->|"load metadata"| node_faiss_metadata
node_rag_connector -->|"orchestrate chain"| node_langchain
node_langchain -->|"run retrieval"| node_retriever
node_retriever -->|"similarity search"| node_faiss_index
node_retriever -->|"retrieved chunks"| node_context_builder
node_context_builder -->|"grounded prompt"| node_groq
node_groq -->|"generated response"| node_rag_connector
node_rag_connector -->|"return answer"| node_answer
node_answer -->|"render"| node_streamlit_app

click node_pdf "https://github.com/dv1712/clinicscribe/blob/main/data/The_GALE_ENCYCLOPEDIA_of_MEDICINE_SECOND.pdf"
click node_indexer "https://github.com/dv1712/clinicscribe/blob/main/create_memory_for_llm.py"
click node_faiss_index "https://github.com/dv1712/clinicscribe/blob/main/vectorstore/db_faiss/index.faiss"
click node_faiss_metadata "https://github.com/dv1712/clinicscribe/blob/main/vectorstore/db_faiss/index.pkl"
click node_streamlit_app "https://github.com/dv1712/clinicscribe/blob/main/clinicScribe.py"
click node_rag_connector "https://github.com/dv1712/clinicscribe/blob/main/connect_memory_with_llm.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_pdf,node_indexer,node_faiss_index,node_faiss_metadata toneBlue
class node_streamlit_app,node_user,node_rag_connector,node_retriever,node_context_builder,node_answer toneAmber
class node_hf_embeddings,node_langchain,node_groq toneMint
```

## Components

### Offline indexing

Rebuild when the corpus or embedding strategy changes.

- [Medical textbook PDF](https://github.com/dv1712/clinicscribe/blob/main/data/The_GALE_ENCYCLOPEDIA_of_MEDICINE_SECOND.pdf): source corpus · Educational and research source material. · `data/The_GALE_ENCYCLOPEDIA_of_MEDICINE_SECOND.pdf`
- [Memory builder](https://github.com/dv1712/clinicscribe/blob/main/create_memory_for_llm.py): indexing script · Extracts text, chunks documents, and creates embeddings. · `create_memory_for_llm.py`
- [FAISS vector index](https://github.com/dv1712/clinicscribe/blob/main/vectorstore/db_faiss/index.faiss): local retrieval store · Persisted vectors and chunk metadata reused by serving. · `vectorstore/db_faiss/index.faiss`
- [FAISS metadata](https://github.com/dv1712/clinicscribe/blob/main/vectorstore/db_faiss/index.pkl): persisted metadata · `vectorstore/db_faiss/index.pkl`

### Interactive RAG

- [ClinicScribe chat UI](https://github.com/dv1712/clinicscribe/blob/main/clinicScribe.py): Streamlit application · Interactive entry point for literature questions. · `clinicScribe.py`
- **User**: chat user
- [RAG chain wiring](https://github.com/dv1712/clinicscribe/blob/main/connect_memory_with_llm.py): retrieval and generation module · Loads the store, retrieves context, and invokes the LLM. · `connect_memory_with_llm.py`
- **Similarity retrieval**: FAISS retriever
- **Grounded context**: prompt context stage · Supplies retrieved textbook chunks to inference.
- **Generated answer**: grounded response · Rendered as educational/research output, not diagnosis.

### External services

- **HuggingFace embeddings**: embedding provider
- **LangChain**: RAG orchestration
- **Groq inference**: external LLM service · Requires network access and API credentials.

## Connections

- Medical textbook PDF → Memory builder: extract and chunk
- Memory builder → HuggingFace embeddings: create vectors
- HuggingFace embeddings → Memory builder: embeddings
- Memory builder → FAISS vector index: persist vectors
- Memory builder → FAISS metadata: persist chunk metadata
- User → ClinicScribe chat UI: asks question
- ClinicScribe chat UI → RAG chain wiring: invoke RAG
- RAG chain wiring → FAISS vector index: load existing store
- RAG chain wiring → FAISS metadata: load metadata
- RAG chain wiring → LangChain: orchestrate chain
- LangChain → Similarity retrieval: run retrieval
- Similarity retrieval → FAISS vector index: similarity search
- Similarity retrieval → Grounded context: retrieved chunks
- Grounded context → Groq inference: grounded prompt
- Groq inference → RAG chain wiring: generated response
- RAG chain wiring → Generated answer: return answer
- Generated answer → ClinicScribe chat UI: render
