- Introduced a new session history feature that tracks and manages conversation history across multiple chat sessions. - Implemented REST API endpoints for creating, listing, retrieving, and deleting chat sessions. - Enhanced error handling and logging for session management operations. - Updated README.md to include documentation for the new session history feature and its usage.
7.3 KiB
7.3 KiB
Session History Integration Summary
Overview
The session history feature has been successfully integrated from the standalone service/ folder into the main LightRAG codebase. This document provides a summary of all changes made.
Changes Made
1. New Files Created
Core Session History Modules (lightrag/api/)
session_models.py- SQLAlchemy database models for sessions, messages, and citationssession_schemas.py- Pydantic schemas for API request/response validationsession_database.py- Database configuration and connection managementsession_manager.py- Business logic for session operations
Updated Files
lightrag/api/routers/history_routes.py- Updated to use new integrated moduleslightrag/api/lightrag_server.py- Added session database initialization
2. Configuration Files Updated
docker-compose.yml
- Added
session-dbservice (PostgreSQL 16) - Configured volume for persistent session data
- Added health checks for database availability
- Set up proper service dependencies
env.example
- Added
SESSION_HISTORY_ENABLEDflag - Added
SESSION_POSTGRES_*configuration variables - Included fallback to main
POSTGRES_*settings
README.md
- Added comprehensive "Session History Feature" section
- Documented configuration options
- Provided Docker deployment instructions
- Added API endpoint examples
- Included usage examples
3. Documentation
New Documents
-
docs/SessionHistoryMigration.md- Complete migration guide- Step-by-step migration instructions
- Configuration reference
- Troubleshooting section
- API examples
-
scripts/migrate_session_history.sh- Automated migration script- Checks and updates
.envconfiguration - Handles backup of old
service/folder - Tests database connectivity
- Provides next steps
- Checks and updates
Architecture Changes
Before (Standalone Service)
service/
├── main.py # Separate FastAPI app
├── app/
│ ├── core/
│ │ ├── config.py # Separate configuration
│ │ └── database.py # Separate DB management
│ ├── models/
│ │ ├── models.py # SQLAlchemy models
│ │ └── schemas.py # Pydantic schemas
│ ├── services/
│ │ ├── history_manager.py # Business logic
│ │ └── lightrag_wrapper.py
│ └── api/
│ └── routes.py # API endpoints
After (Integrated)
lightrag/
└── api/
├── session_models.py # SQLAlchemy models
├── session_schemas.py # Pydantic schemas
├── session_database.py # DB management
├── session_manager.py # Business logic
├── lightrag_server.py # Main server (updated)
└── routers/
└── history_routes.py # API endpoints (updated)
Key Features
1. Automatic Initialization
- Session database is automatically initialized when LightRAG Server starts
- Graceful degradation if database is unavailable
- Tables are created automatically on first run
2. Unified Configuration
- All configuration through main
.envfile - Fallback to main PostgreSQL settings if session-specific settings not provided
- Easy enable/disable via
SESSION_HISTORY_ENABLEDflag
3. Docker Integration
- PostgreSQL container automatically configured in
docker-compose.yml - Persistent volumes for data retention
- Health checks for reliability
- Proper service dependencies
4. API Consistency
- Session endpoints follow LightRAG API conventions
- Proper authentication headers (
X-User-ID) - RESTful endpoint design
- Comprehensive error handling
API Endpoints
All session history endpoints are now under the /history prefix:
| Method | Endpoint | Description |
|---|---|---|
| POST | /history/sessions |
Create a new chat session |
| GET | /history/sessions |
List all sessions for user |
| GET | /history/sessions/{id}/history |
Get message history |
| DELETE | /history/sessions/{id} |
Delete session and messages |
Migration Path
For New Installations
- Copy
env.exampleto.env - Configure
SESSION_POSTGRES_*variables - Run
docker compose up -d(if using Docker) - Start LightRAG server:
lightrag-server
For Existing Installations with service/
- Run migration script:
bash scripts/migrate_session_history.sh - Update
.envwith session configuration - Restart LightRAG server
- Test session endpoints
- Backup and remove old
service/folder (optional)
Configuration Examples
Minimal Configuration (Uses Defaults)
SESSION_HISTORY_ENABLED=true
Full Configuration
SESSION_HISTORY_ENABLED=true
SESSION_POSTGRES_HOST=localhost
SESSION_POSTGRES_PORT=5433
SESSION_POSTGRES_USER=lightrag
SESSION_POSTGRES_PASSWORD=secure_password
SESSION_POSTGRES_DATABASE=lightrag_sessions
Using Main PostgreSQL Instance
SESSION_HISTORY_ENABLED=true
# Session will use main POSTGRES_* settings
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_DATABASE=lightrag_db
Disabled Session History
SESSION_HISTORY_ENABLED=false
# No PostgreSQL required for session history
Testing
Manual Testing
# Create a session
curl -X POST http://localhost:9621/history/sessions \
-H "Content-Type: application/json" \
-H "X-User-ID: test@example.com" \
-d '{"title": "Test Session"}'
# List sessions
curl http://localhost:9621/history/sessions \
-H "X-User-ID: test@example.com"
# Get session history
curl http://localhost:9621/history/sessions/{session_id}/history
Docker Testing
# Start all services
docker compose up -d
# Check logs
docker compose logs -f lightrag session-db
# Verify database
docker exec -it lightrag-session-db psql -U lightrag -d lightrag_sessions -c '\dt'
Dependencies
All required dependencies are already included in pyproject.toml:
sqlalchemy- ORM for database operationspsycopg2-binary- PostgreSQL driverfastapi- Web frameworkpydantic- Data validation
Next Steps
Cleanup (Optional)
After successful migration and testing:
# Backup old service folder
mv service service.backup.$(date +%Y%m%d)
# Or remove completely
rm -rf service
Monitoring
- Check server logs for session initialization messages
- Monitor PostgreSQL connections
- Review session creation and query performance
Customization
- Modify session models in
session_models.py - Extend API endpoints in
routers/history_routes.py - Add custom business logic in
session_manager.py
Rollback Plan
If needed, to rollback to standalone service:
- Restore
service/folder from backup - Remove session configuration from
.env - Revert changes to
docker-compose.yml - Restart services
Support
For issues or questions:
- Review
docs/SessionHistoryMigration.md - Check LightRAG documentation
- Open an issue on GitHub
Conclusion
The session history feature is now fully integrated into LightRAG as a first-class feature. The integration provides:
- ✅ Easier setup and configuration
- ✅ Better maintainability
- ✅ Unified Docker deployment
- ✅ Consistent API design
- ✅ Comprehensive documentation
- ✅ Automated migration tools
The old service/ folder can now be safely removed or kept as backup.