Tech Stack
FastAPI
A modern, fast (high-performance) web framework for building APIs with Python 3.10+.
SQLAlchemy
The Python SQL toolkit and Object Relational Mapper, used for all relational database interactions.
PydanticSettings
Roboust configuration management using environment variables.
LanceDB / Chroma
Pluggable support for various vector databases to power the RAG pipeline.
APScheduler
Advanced Python Scheduler for handling background jobs and periodic tasks.
Passlib & JWT
Secure password hashing and token-based authentication (JSON Web Tokens).
System Diagrams
High-Level Architecture
The below diagram illustrates the primary components and how data flows between the client, the API server, and the various backing services.System Architecture
Chat & RAG Flow
When a user sends a message, the system orchestrates a complex flow to provide a context-aware response.- Receive: The
chatendpoint receives the user message. - Retrieve: The
embedding_servicesearches the Vector DB for relevant documents. - Construct: The
chat_servicebuilds a prompt containing the user message, conversation history, and retrieved context. - Generate: The prompt is sent to the configured LLM provider (e.g., OpenAI).
- Stream: The response is streamed back to the client in real-time.
Database Layer
The application uses SQLAlchemy (AsyncIO) for database persistence.- Session Management: Handled via
app.core.database.get_dbdependency. - Initialization:
init_db()inapp.core.databaseensures tables are created on startup. - Migrations: Currently, the system uses
Base.metadata.create_all()for schema creation.
Key Models (app/models/)
- User: Stores user credentials and profile info.
- Workspace: Groups resources (chats, docs) for isolation.
- WorkspaceDocument: Metadata for uploaded files.
- ChatSession: Represents a conversation thread.
- ChatMessage: Individual messages within a session.
Background Jobs
IfENABLE_BACKGROUND_JOBS is set to True, the server initializes an APScheduler instance.
- Location:
app/tasks/scheduler.py - Lifecycle: Started/Shutdown via
app.main.lifespan. - Use Cases: Periodic vector store cleanup, email notifications, system maintenance.