Chat
Build conversational AI experiences with stateful session managementOverview
In the Upsonic framework,Chat
is a comprehensive conversational interface that provides stateful session management for AI agents. It handles the complete lifecycle of chat interactions, including message history, memory integration, cost tracking, and both streaming and non-streaming interfaces.
The Chat class serves as a high-level orchestrator that manages:
- Session lifecycle and state management
- Memory integration and conversation persistence
- Cost and token tracking across sessions
- Both blocking and streaming response modes
- Error handling and retry mechanisms
- Concurrent invocation control
Chat Attributes
The Chat class provides comprehensive configuration options to customize conversational behavior and session management.Core Attributes
Attribute | Type | Description |
---|---|---|
session_id | str | Unique identifier for this chat session |
user_id | str | Unique identifier for the user |
agent | Agent | The Agent instance to handle conversations |
storage | Optional[Storage] | Storage backend for persistence (defaults to InMemoryStorage) |
Memory Configuration
Attribute | Type | Description |
---|---|---|
full_session_memory | bool | Enable full conversation history storage (default: True) |
summary_memory | bool | Enable conversation summarization (default: False) |
user_analysis_memory | bool | Enable user profile analysis (default: False) |
user_profile_schema | Optional[Type[BaseModel]] | Custom user profile schema |
dynamic_user_profile | bool | Enable dynamic profile schema generation (default: False) |
num_last_messages | Optional[int] | Limit conversation history to last N messages |
feed_tool_call_results | bool | Include tool calls in memory (default: False) |
user_memory_mode | Literal[‘update’, ‘replace’] | How to update user profiles (default: ‘update’) |
Advanced Configuration
Attribute | Type | Description |
---|---|---|
debug | bool | Enable debug logging (default: False) |
max_concurrent_invocations | int | Maximum concurrent invoke calls (default: 1) |
retry_attempts | int | Number of retry attempts for failed calls (default: 3) |
retry_delay | float | Delay between retry attempts in seconds (default: 1.0) |
Creating a Chat Session
Chat sessions are created directly in code using the constructor. Each chat session can be customized with specific memory settings, storage backends, and session management options.Basic Chat Creation
Chat with Custom Memory Settings
Chat with Debug and Retry Configuration
Advanced Chat Configuration
Custom Storage Backends
Memory Management Strategies
Chat Interface Methods
Basic Messaging
Streaming Responses
Session Management
Session State Management
Session States
The Chat class manages several session states:- IDLE: Session is ready to accept new messages
- AWAITING_RESPONSE: Processing a user message
- STREAMING: Streaming a response to the user
- ERROR: Session encountered an error
Session Lifecycle
Memory Integration
Conversation History
Memory Persistence
Cost and Token Tracking
Cost Monitoring
Session Analytics
Error Handling and Retry Logic
Automatic Retry
Error Recovery
Practical Examples
Customer Support Chat
Educational Assistant
Multi-User Chat System
Streaming Chat Interface
Best Practices
Performance Optimization
- Choose appropriate storage: Use in-memory for development, persistent storage for production
- Configure memory settings: Enable only needed memory features to optimize performance
- Set retry limits: Balance between reliability and response time
- Use streaming for long responses: Improve user experience with real-time output
Session Management
- Use unique session IDs: Ensure proper session isolation
- Implement session cleanup: Close unused sessions to free resources
- Monitor session state: Handle errors gracefully
- Use context managers: Ensure proper resource cleanup
Memory Configuration
- Enable summarization for long conversations: Prevent memory overflow
- Use user analysis for personalization: Improve user experience
- Configure message limits: Balance between context and performance
- Choose appropriate memory modes: Update vs replace based on use case