How leviben/assessment-stack-ai works
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: ingestion_service.py Ingestion orchestration · retrieval_service.py Hybrid retrieval · query_processing_service.py Intent & query rewrite · embedding_service.py Embedding service
Components 16
Client
- Streamlit client
streamlit_app.py
FastAPI backend
- FastAPI entrypoint
main.py - Documents router · Upload, list, and delete endpoints.
app/api/documents.py - Query router · Question-answering endpoint.
app/api/query.py - Runtime config · Centralizes API key and runtime tuning.
app/core/config.py
RAG pipeline
- Ingestion orchestration
app/services/ingestion_service.py - PDF page chunking · Creates overlapping chunks without crossing pages.
app/services/pdf_service.py - Embedding service · Embeds indexed chunks and retrieval queries.
app/services/embedding_service.py - Intent & query rewrite · Short-circuits greetings and rewrites knowledge queries.
app/services/query_processing_service.py - Hybrid retrieval · Fuses keyword and cosine rankings across original and rewritten queries.
app/services/retrieval_service.py - Grounded generation · Answers only from selected chunk context.
app/services/generation_service.py
Storage & models
- In-memory corpus · Process-local replaceable vector and document store.
app/repositories/in_memory_store.py - Document & chunk domain
app/models/domain.py - API schemas · Carries answers, citations, and ranking scores.
app/models/schemas.py
External AI
- Mistral embeddings
- Mistral chat
Connections 20
- 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