Chat Database (v1.0.0)
Vector database for storing and retrieving chat history with semantic search capabilities.
Overview
The Chat Database uses Qdrant, a vector database, to store chat history items with semantic search capabilities. This enables efficient retrieval of conversation history and supports AI agent orchestration by maintaining context across chat sessions.
Architecture
Vector Storage Implementation
The database is accessed through VectorChatMessageStore, which implements the ChatMessageStore interface from Microsoft.Agents.AI. This provides:
- Vector Collections: Chat messages are stored in vector collections using
VectorStoreCollection<Guid, ChatHistoryItem> - Thread-based Organization: Messages are grouped by
ThreadIdfor conversation continuity - User Isolation: Messages are filtered by
UserIdto ensure data privacy - Temporal Ordering: Messages are stored with timestamps for chronological retrieval
Data Model
Messages are stored as ChatHistoryItem records with the following structure:
{ Key: string, // ThreadDbKey + MessageId (unique identifier) ThreadId: string, // Conversation thread identifier (UUIDv7) Timestamp: DateTimeOffset, // Message creation time SerializedMessage: string, // Full ChatMessage JSON MessageText: string, // Extracted message text for search UserId: string // User who sent the message}Key Features
1. Thread Management
- Each conversation thread is identified by a UUIDv7-based
ThreadDbKey - Thread keys are generated on first message and maintained throughout the session
- Enables concurrent conversations without interference
2. Message Retrieval
- Retrieves up to
MaxMessagesper query (configurable via AppSettings) - Messages are ordered by timestamp in descending order
- Results are reversed to maintain chronological order
- Filters by both thread ID and user ID for security
3. Message Persistence
- Uses upsert operations for idempotent writes
- Serializes full
ChatMessageobjects with JSON source generation - Maintains metadata (timestamp, user ID) for filtering and analytics
- Extracts message text for semantic search capabilities
4. State Serialization
- Thread state can be serialized to JSON for session management
- Enables conversation resumption across sessions
- Supports state hydration from serialized JSON elements
Technical Details
Vector Store Configuration
- Collection Type:
VectorStoreCollection<Guid, ChatHistoryItem> - Key Type: Guid (for collection operations)
- Record Type: ChatHistoryItem with vector data attributes
Query Operations
// Retrieval query structurex => x.ThreadId == ThreadDbKey && x.UserId == userId// OrderingOrderBy: x => x.Descending(y => y.Timestamp)// LimitMaxMessages (from AppSettings)Serialization
- Uses
JsonSerializerOptionswith camelCase naming policy - Source-generated serialization context for performance
- Property name case-insensitive deserialization
Security
- User Isolation: All queries filter by authenticated user ID from ClaimsPrincipal
- Thread Isolation: Messages are scoped to specific conversation threads
- Access Control: Read/write access controlled through authentication claims
Performance Considerations
- Batch Operations: Uses
UpsertAsyncfor efficient bulk writes - Query Limits: Configurable
MaxMessagesprevents over-fetching - Async Operations: All database operations are asynchronous
- Collection Caching: Ensures collection exists before operations
Integration
The vector store integrates with:
- Microsoft.Agents.AI: Implements
ChatMessageStoreinterface - Microsoft.Extensions.VectorData: Uses vector data attributes and collections
- ASP.NET Core Authentication: Leverages
ClaimsPrincipalfor user context - Qdrant: Underlying vector database engine