You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add database persistence layer and conversation tracking system
# Background / Context
When the user queries lightspeed-stack, they get a conversation ID in
the response that they can later use to retrieve the conversation
history and continue the conversation.
# Issue / Requirement / Reason for change
We've put the burden of remembering the conversation ID on the user,
which is not ideal. The user (e.g. a UI) has to store the conversation
ID somewhere, e.g. browser local storage, but this is not ideal because
it can easily get lost. It also complicated the UI implementation.
Another issue is that a user could read another user's conversation if
they only knew the ID, which is hard, but not ideal. It's not
immediately obvious that the conversation ID is a secret that should not
be shared with others.
# Solution / Feature Overview
Add a database persistence layer to lightspeed-stack to store user
conversation IDs and a bit of metadata about the conversation.
Allow users to retrieve their conversations by calling a new endpoint
`/conversations` (without an ID - with an ID is an existing endpoint
that lists the message history for a conversation), which will return a
list of conversations associated with the user, along with metadata
like creation time, last message time, and message count.
Verify that the user owns the conversation when querying or streaming a
conversation, and automatically associate the conversation with the user
in the database when the user queries or streams a conversation.
# Implementation Details
**Database Layer (src/app/database.py):**
- SQLAlchemy
- Support for both SQLite and Postgre databases
- PostgreSQL custom schema (table namespacing) support with automatic
schema creation. This is useful because llama-stack itself needs a
database for persisting conversation history, and we want to avoid
conflicts with that when users use the same Postgres database as
llama-stack.
- Automatically enable SQL statement tracing when debug logging is
enabled
**Models (src/models/):**
- Base model class for shared SQLAlchemy configuration
- UserConversation model with indexed user_id fields for efficient querying, automatic timestamp tracking (created_at, last_message_at) and message count tracking
**Configuration Updates:**
- Extended configuration system to support database connection settings
**API:**
- Conversations endpoint without an ID now returns a list of
conversations for the authenticated user, along with metadata
- Query/streaming endpoint automatically associates conversations
with the user in the database, and verifies ownership of conversations
**Dependencies:**
- Added SQLAlchemy dependency
**Tests:**
More tests will be added (and unit tests will be fixed) once I get some
initial feedback on the implementation.
# Future Work
This change does not include migrations, so any future changes to the
databases require writing custom backwards compatibility code. We should
probably add Alembic or something simpler in the near future.
0 commit comments