# leviben/assessment-stack-ai architecture

> Architecture diagram of the GitHub repository leviben/assessment-stack-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/leviben/assessment-stack-ai
- Repository: https://github.com/leviben/assessment-stack-ai

## Overview

#### Purpose and Entry Points
This is a PDF-focused RAG system: users upload PDFs, then ask grounded questions with page-level citations and retrieval scores. The FastAPI backend starts at `main.py`; the Streamlit client is `streamlit_app.py`. HTTP boundaries are implemented by `app/api/documents.py` (ingest/list/delete) and `app/api/query.py` (question answering).

#### Application Layering
The backend is deliberately split into API, service, repository, and model layers:
- Routers validate/translate HTTP requests and responses: `app/api/documents.py`, `app/api/query.py`.
- Pipeline orchestration and model integrations live in `app/services/`.
- Storage is isolated behind `app/repositories/in_memory_store.py`.
- Shared document/chunk domain objects and API schemas are in `app/models/domain.py` and `app/models/schemas.py`.
This separation makes the in-memory repository replaceable without redesigning ingestion or retrieval.

#### Ingestion Pipeline
Document upload flows from the documents router into `app/services/ingestion_service.py`. It coordinates:
1. PDF extraction and per-page chunking with overlap in `app/services/pdf_service.py`.
2. One Mistral embedding call per chunk via `app/services/embedding_service.py`.
3. Storage of document identity, chunk text, page metadata, and vectors in `app/repositories/in_memory_store.py`.

Re-uploading the same filename replaces its indexed contents. Page-local chunking is a key traceability boundary: chunks do not cross page boundaries, enabling reliable file/page citations.

#### Query Classification and Rewrite
`app/services/query_processing_service.py` first separates exact greeting/thanks inputs from knowledge queries. Conversational inputs return canned responses and avoid retrieval and Mistral calls. Knowledge queries are rewritten into concise search terms through the chat model, while retaining the original query as an independent retrieval input. Rewrite failure falls back to the original query.

#### Retrieval and Ranking
`app/services/retrieval_service.py` searches the in-memory corpus in two modes: stop-word-filtered keyword matching and brute-force cosine similarity over stored embeddings. It uses reciprocal-rank fusion first to merge keyword and semantic rankings for each query, then again to merge original-query and rewritten-query results. A semantic-score floor and top-k cutoff define the context boundary passed downstream. The repository (`app/repositories/in_memory_store.py`) is the intended seam for replacing brute-force memory storage with Qdrant, pgvector, or another persistent/vector backend.

#### Grounded Answer Generation
`app/services/generation_service.py` builds a context prompt from the selected chunks and calls Mistral to answer only from supplied evidence. It is instructed to return “Insufficient evidence.” if support is absent. Query responses expose the answer plus intent, rewrite, source chunk identity, file/page metadata, and ranking scores; schemas are defined in `app/models/schemas.py`.

#### Runtime and External Dependencies
Python 3.11+ runs FastAPI/Uvicorn for the backend and Streamlit as a separate UI process. Mistral AI is the only required external service: `mistral-embed` produces chunk/query vectors and `mistral-small-latest` performs rewriting and generation. Runtime tuning and API-key-backed configuration are centralized in `app/core/config.py`. State is process-local and is lost on restart.

Main entry points: [`app/services/ingestion_service.py`](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/ingestion_service.py) (Ingestion orchestration), [`app/services/retrieval_service.py`](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/retrieval_service.py) (Hybrid retrieval), [`app/services/query_processing_service.py`](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/query_processing_service.py) (Intent & query rewrite), [`app/services/embedding_service.py`](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/embedding_service.py) (Embedding service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client"]
  node_streamlit["Streamlit client<br/>UI process<br/>[streamlit_app.py]"]
end

subgraph group_backend["FastAPI backend"]
  node_main["FastAPI entrypoint<br/>ASGI app<br/>[main.py]"]
  node_documents_api["Documents router<br/>HTTP router<br/>[documents.py]"]
  node_query_api["Query router<br/>HTTP router<br/>[query.py]"]
  node_config["Runtime config<br/>configuration<br/>[config.py]"]
end

subgraph group_pipeline["RAG pipeline"]
  node_ingestion["Ingestion orchestration"]
  node_pdf["PDF page chunking<br/>[pdf_service.py]"]
  node_embedding["Embedding service"]
  node_query_processing["Intent &amp; query rewrite"]
  node_retrieval["Hybrid retrieval"]
  node_generation["Grounded generation"]
end

subgraph group_storage["Storage &amp; models"]
  node_store[("In-memory corpus<br/>[in_memory_store.py]")]
  node_domain["Document &amp; chunk domain<br/>domain models<br/>[domain.py]"]
  node_schemas["API schemas<br/>response models<br/>[schemas.py]"]
end

subgraph group_external["External AI"]
  node_mistral_embed{{"Mistral embeddings<br/>mistral-embed"}}
  node_mistral_chat{{"Mistral chat<br/>mistral-small-latest"}}
end

node_streamlit -->|"HTTP"| node_main
node_main -->|"routes"| node_documents_api
node_main -->|"routes"| node_query_api
node_config -.->|"configures"| node_embedding
node_config -.->|"configures"| node_query_processing
node_config -.->|"configures"| node_generation
node_documents_api -->|"uploads"| node_ingestion
node_ingestion -->|"extracts &amp; chunks"| node_pdf
node_ingestion -->|"embeds chunks"| node_embedding
node_ingestion -->|"indexes documents/chunks"| node_store
node_pdf -->|"creates"| node_domain
node_embedding -->|"embedding calls"| node_mistral_embed
node_query_api -->|"question"| node_query_processing
node_query_processing -->|"original + rewrite"| node_retrieval
node_query_processing -->|"rewrite knowledge query"| node_mistral_chat
node_retrieval -->|"embed query"| node_embedding
node_retrieval -->|"keyword + vector search"| node_store
node_retrieval -->|"top context"| node_generation
node_generation -->|"grounded answer"| node_mistral_chat
node_query_api -->|"response"| node_schemas

click node_streamlit "https://github.com/leviben/assessment-stack-ai/blob/main/streamlit_app.py"
click node_main "https://github.com/leviben/assessment-stack-ai/blob/main/main.py"
click node_documents_api "https://github.com/leviben/assessment-stack-ai/blob/main/app/api/documents.py"
click node_query_api "https://github.com/leviben/assessment-stack-ai/blob/main/app/api/query.py"
click node_config "https://github.com/leviben/assessment-stack-ai/blob/main/app/core/config.py"
click node_ingestion "https://github.com/leviben/assessment-stack-ai/blob/main/app/services/ingestion_service.py"
click node_pdf "https://github.com/leviben/assessment-stack-ai/blob/main/app/services/pdf_service.py"
click node_embedding "https://github.com/leviben/assessment-stack-ai/blob/main/app/services/embedding_service.py"
click node_query_processing "https://github.com/leviben/assessment-stack-ai/blob/main/app/services/query_processing_service.py"
click node_retrieval "https://github.com/leviben/assessment-stack-ai/blob/main/app/services/retrieval_service.py"
click node_generation "https://github.com/leviben/assessment-stack-ai/blob/main/app/services/generation_service.py"
click node_store "https://github.com/leviben/assessment-stack-ai/blob/main/app/repositories/in_memory_store.py"
click node_domain "https://github.com/leviben/assessment-stack-ai/blob/main/app/models/domain.py"
click node_schemas "https://github.com/leviben/assessment-stack-ai/blob/main/app/models/schemas.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_streamlit toneBlue
class node_main,node_documents_api,node_query_api,node_config toneAmber
class node_ingestion,node_pdf,node_embedding,node_query_processing,node_retrieval,node_generation toneMint
class node_store,node_domain,node_schemas toneRose
class node_mistral_embed,node_mistral_chat toneIndigo
```

## Components

### Client

- [Streamlit client](https://github.com/leviben/assessment-stack-ai/blob/main/streamlit_app.py): UI process · `streamlit_app.py`

### FastAPI backend

- [FastAPI entrypoint](https://github.com/leviben/assessment-stack-ai/blob/main/main.py): ASGI app · `main.py`
- [Documents router](https://github.com/leviben/assessment-stack-ai/blob/main/app/api/documents.py): HTTP router · Upload, list, and delete endpoints. · `app/api/documents.py`
- [Query router](https://github.com/leviben/assessment-stack-ai/blob/main/app/api/query.py): HTTP router · Question-answering endpoint. · `app/api/query.py`
- [Runtime config](https://github.com/leviben/assessment-stack-ai/blob/main/app/core/config.py): configuration · Centralizes API key and runtime tuning. · `app/core/config.py`

### RAG pipeline

- [Ingestion orchestration](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/ingestion_service.py): service · `app/services/ingestion_service.py`
- [PDF page chunking](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/pdf_service.py): service · Creates overlapping chunks without crossing pages. · `app/services/pdf_service.py`
- [Embedding service](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/embedding_service.py): service · Embeds indexed chunks and retrieval queries. · `app/services/embedding_service.py`
- [Intent & query rewrite](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/query_processing_service.py): service · Short-circuits greetings and rewrites knowledge queries. · `app/services/query_processing_service.py`
- [Hybrid retrieval](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/retrieval_service.py): service · Fuses keyword and cosine rankings across original and rewritten queries. · `app/services/retrieval_service.py`
- [Grounded generation](https://github.com/leviben/assessment-stack-ai/blob/main/app/services/generation_service.py): service · Answers only from selected chunk context. · `app/services/generation_service.py`

### Storage & models

- [In-memory corpus](https://github.com/leviben/assessment-stack-ai/blob/main/app/repositories/in_memory_store.py): repository · Process-local replaceable vector and document store. · `app/repositories/in_memory_store.py`
- [Document & chunk domain](https://github.com/leviben/assessment-stack-ai/blob/main/app/models/domain.py): domain models · `app/models/domain.py`
- [API schemas](https://github.com/leviben/assessment-stack-ai/blob/main/app/models/schemas.py): response models · Carries answers, citations, and ranking scores. · `app/models/schemas.py`

### External AI

- **Mistral embeddings**: mistral-embed
- **Mistral chat**: mistral-small-latest

## Connections

- Streamlit client → FastAPI entrypoint: HTTP
- FastAPI entrypoint → Documents router: routes
- FastAPI entrypoint → Query router: routes
- Runtime config → Embedding service: configures
- Runtime config → Intent & query rewrite: configures
- Runtime config → Grounded generation: configures
- Documents router → Ingestion orchestration: uploads
- Ingestion orchestration → PDF page chunking: extracts & chunks
- Ingestion orchestration → Embedding service: embeds chunks
- Ingestion orchestration → In-memory corpus: indexes documents/chunks
- PDF page chunking → Document & chunk domain: creates
- Embedding service → Mistral embeddings: embedding calls
- Query router → Intent & query rewrite: question
- Intent & query rewrite → Hybrid retrieval: original + rewrite
- Intent & query rewrite → Mistral chat: rewrite knowledge query
- Hybrid retrieval → Embedding service: embed query
- Hybrid retrieval → In-memory corpus: keyword + vector search
- Hybrid retrieval → Grounded generation: top context
- Grounded generation → Mistral chat: grounded answer
- Query router → API schemas: response
