File:
app/db/database.py
This module provides a streamlined way to connect to and manage database sessions for your application. It supports both PostgreSQL and SQLite, with environment-based configuration for flexible deployment.
The database connection is controlled using environment variables. If these are not set, the module uses sensible defaults.
Variable | Description | Default Value | Supported Values |
---|---|---|---|
DB_MODE |
Specifies the type of database to use | "postgres" |
postgres , sqlite |
DATABASE_URL |
Connection string for the database | PostgreSQL: postgresql://user:password@localhost/ai_hub_db SQLite: sqlite:///./ai_hub.db |
Any SQLAlchemy URI |
export DB_MODE="sqlite"
This module exposes several key components used to interface with the database.
engine
SessionLocal
get_db
dependency.Base
get_db()
To safely interact with the database in your FastAPI routes, use the get_db()
dependency.
Import get_db
:
from app.db.database import get_db
Add it as a dependency:
from fastapi import Depends from sqlalchemy.orm import Session
Inject into your route handler:
@app.get("/items/") def read_items(db: Session = Depends(get_db)): return db.query(Item).all()
This pattern ensures that every request has a dedicated, isolated database session that is properly cleaned up afterward.
Here is your fully formatted and organized documentation for the SQLAlchemy models in app/db/models.py
, suitable for technical documentation or a README:
This document describes the SQLAlchemy models defined in app/db/models.py
. These classes represent the tables used to store application data, including chat sessions, messages, and document metadata for Retrieval-Augmented Generation (RAG).
All models inherit from the Base
class, imported from the database.py
module.
Represents a single conversation between a user and an AI. It serves as the container for all messages in that session.
Field | Data Type | Description |
---|---|---|
id |
Integer | Primary key for the session |
user_id |
String | Unique identifier of the user |
title |
String | Optional AI-generated title |
model_name |
String | Name of the LLM used in the session |
created_at |
DateTime | Timestamp of when the session was created |
is_archived |
Boolean | Soft-delete/archive flag for the session |
messages
: One-to-many relationship with the Message model. Deleting a session will also delete all related messages (via cascade="all, delete-orphan"
).Stores individual messages within a session, including user inputs and AI responses.
Field | Data Type | Description |
---|---|---|
id |
Integer | Primary key for the message |
session_id |
Integer | Foreign key linking to the parent session |
sender |
String | Role of the sender ("user" or "assistant" ) |
content |
Text | Full text of the message |
created_at |
DateTime | Timestamp of message creation |
model_response_time |
Integer | Time (in seconds) taken by the model to generate the response |
token_count |
Integer | Number of tokens in the message |
message_metadata |
JSON | Flexible field for storing unstructured metadata (e.g., tool calls) |
session
: Many-to-one relationship with the Session model.Stores metadata and content of documents ingested into the system for RAG purposes.
Field | Data Type | Description |
---|---|---|
id |
Integer | Primary key for the document |
title |
String | Human-readable title |
text |
Text | Full content of the document |
source_url |
String | URL or path where the document was retrieved from |
author |
String | Author of the document |
status |
String | Processing status ("ready" , "processing" etc.) |
created_at |
DateTime | Timestamp of document creation |
user_id |
String | ID of the user who added the document |
vector_metadata
: One-to-one relationship with the VectorMetadata model.Connects documents to their vector representations (e.g., FAISS indices) for efficient retrieval in RAG workflows.
Field | Data Type | Description |
---|---|---|
id |
Integer | Primary key for the metadata entry |
document_id |
Integer | Foreign key to the parent Document (unique constraint) |
faiss_index |
Integer | Vector's index in the FAISS store |
session_id |
Integer | Foreign key to the Session used in the RAG context |
embedding_model |
String | Embedding model used to generate the vector |
document
: Many-to-one relationship with the Document model
session
: Many-to-one relationship with the Session model