System Architecture
Backend
The Python backend uses FastAPI to support pottery-journal operations and a chat assistant with RAG and text-to-SQL paths.
Implementation approach
- FastAPI routes requests to journal and chat handlers. Shared dependencies supply authenticated users and user-scoped database sessions.
- Centralised FastAPI exception handlers translate API failures into consistent error responses without exposing internal details. The chat workflow handles failures during streaming separately.
- The backend uses asynchronous I/O for database queries and LLM calls, allowing other requests to progress during those waits. Processing remains within the original HTTP request: the frontend receives the result through its response, with chat answers streamed incrementally.
- Pydantic models define API payloads and structured LLM outputs. The chat workflow branches on validated fields rather than interpreting free-form model responses.
- SQLAlchemy handles database reads and writes through asynchronous sessions, with shared session management responsible for commit, rollback, and cleanup.
- Alembic versions and applies database schema changes.
- PydanticAI manages LLM interactions, structured outputs, retries, tool execution, and streaming.
- OpenRouter provides access to chat models and embeddings. Chat model selection is controlled through configuration.
- Langfuse records chat-workflow and model-call traces so execution, latency, and token usage can be inspected.
- Pydantic Settings loads and validates environment-based configuration,
including database connections, model selection, and credentials. Sensitive
fields use
SecretStrto mask their normal representation.
Backend organisation
- API layer. The
routes/modules expose endpoints for pieces, events, and conversations. Shared dependencies supply the authenticated user and a user-scoped database session. - Business logic. Journal operations are implemented in the route handlers.
Chat orchestration lives in
pipeline/, where individual steps coordinate the RAG and text-to-SQL paths. - Database access. The
database/package contains models, repositories, and session management. Retrieval uses specialised queries, while the text-to-SQL path uses a separate user-scoped, read-only session. - External AI integration. The
agent/modules encapsulate classification, routing, SQL generation, and answer generation. Theservices/modules implement embedding, retrieval, and reranking.