How staspog/s21_agent works
Purpose and entry points
This is a Python RAG assistant for S21 School project documentation. It retrieves relevant text chunks with SentenceTransformers + FAISS and asks GigaChat to generate an answer grounded in that context.
Primary runnable entry point is the FastAPI service in src/api.py, launched with Uvicorn. src/old_prototype_main.py is a legacy console-oriented implementation rather than the documented src/main.py, which is absent from the tree. src/test_client.py is the available HTTP client/script.
API boundary
src/api.py owns the HTTP-facing application and /ask/ question workflow. It should be treated as the stateless request boundary: accept a user question, invoke retrieval/generation services, and return the generated response.
The server is containerizable through Dockerfile; it runs as a Python service rather than a browser application.
Knowledge corpus and persisted retrieval state
The repository ships precomputed retrieval artifacts under:
• content/chunks.json
• content/chunks_map.json
• content/chunks.index
These files are the material knowledge boundary. chunks.json stores chunk content or records, chunks_map.json maps FAISS result positions to chunk metadata/content, and chunks.index is the persisted FAISS vector index. Runtime retrieval can therefore avoid re-ingesting source documentation when these artifacts are present.
Ingestion and chunk preparation
src/data_loader.py is responsible for loading source material and/or persisted chunk data. It forms the transition from filesystem documents to normalized text chunks suitable for embedding and retrieval.
The README describes .md and .txt source ingestion, but the checked-in tree contains only generated artifacts in content/; there is no visible content/data/ directory. Any corpus rebuild path must account for that deployment distinction.
Embedding and vector indexing
src/embeddings.py encapsulates SentenceTransformers embedding creation and FAISS index construction/loading. This component defines the vector representation shared by both corpus indexing and query-time search.
Its output is persisted to content/chunks.index, with positional association maintained by content/chunks_map.json. Embedding-model compatibility with an existing index is an important operational boundary: changing the model requires regenerating the index and mappings.
Retrieval
src/search.py performs similarity search against the FAISS index and resolves nearest-neighbor IDs through the chunk mapping. It is the context-selection stage between incoming questions and the LLM.
Control flow is expected to be: API question → query embedding → FAISS top-k search → matched text chunks. Retrieval quality and context size directly constrain answer quality and GigaChat prompt size.
Generation and external service integration
src/gigachat_api.py isolates calls to the external GigaChat API. It receives the user question plus retrieved context and returns a generated answer.
Authentication is supplied via GIGACHAT_API_KEY, normally loaded from environment/.env. This is the principal network and secret-management boundary; FAISS and corpus artifacts remain local, while answer generation depends on GigaChat availability and credentials.
Runtime dependencies
The material runtime is Python 3.10 with sentence-transformers, faiss-cpu, gigachat, FastAPI, and Uvicorn declared in requirements.txt. Deployment needs local access to content/ artifacts and outbound connectivity to GigaChat.
Main entry points: search.py Similarity search · gigachat_api.py GigaChat adapter · embeddings.py Embeddings and FAISS · api.py FastAPI /ask
Components 14
Runtime
- Docker service · Runs the Python API service.
Dockerfile - FastAPI /ask · Stateless question request boundary.
src/api.py - HTTP client script
src/test_client.py - Python dependencies · Python 3.10, FastAPI, SentenceTransformers, FAISS, and GigaChat.
requirements.txt - Legacy console prototype · Not the documented service entry point.
src/old_prototype_main.py
Retrieval pipeline
- Data loader · Loads source material or persisted chunks.
src/data_loader.py - Embeddings and FAISS · Creates embeddings and constructs or loads the vector index.
src/embeddings.py - Similarity search · Embeds questions, searches FAISS, and resolves matched chunks.
src/search.py
Knowledge artifacts
- Chunk content
content/chunks.json - Chunk mapping · Maps FAISS positions to chunk metadata or content.
content/chunks_map.json - FAISS index · Persisted index requiring compatible embeddings.
content/chunks.index
External generation
- GigaChat adapter · Builds grounded requests from question and retrieved context.
src/gigachat_api.py - GigaChat API · Requires outbound access and API credentials.
- GIGACHAT_API_KEY · Environment or .env supplied secret.
Connections 17
- HTTP client script → FastAPI /ask: POST question
- Docker service → FastAPI /ask: runs
- Python dependencies → Docker service: installed in
- FastAPI /ask → Similarity search: question
- Similarity search → Embeddings and FAISS: query embedding
- Similarity search → FAISS index: top-k search
- Similarity search → Chunk mapping: resolve results
- Similarity search → GigaChat adapter: question and context
- GigaChat adapter → GigaChat API: generate answer
- GigaChat API → GigaChat adapter: answer
- GigaChat adapter → GIGACHAT_API_KEY: authenticates with
- GigaChat adapter → FastAPI /ask: generated response
- Data loader → Chunk content: loads or produces
- Data loader → Embeddings and FAISS: normalized chunks
- Embeddings and FAISS → FAISS index: builds or loads
- Embeddings and FAISS → Chunk mapping: maintains positions
- Chunk content → Chunk mapping: chunk records