mirror of
https://github.com/hanzoai/python-sdk.git
synced 2026-10-23 20:23:00 +00:00
5.1 KiB
5.1 KiB
Hanzo Memory Service - LLM Context
Project Overview
Hanzo Memory Service is a high-performance AI memory and knowledge management system built with:
- InfinityDB: Embedded vector database for fast similarity search
- FastEmbed: Local embedding generation (no API calls)
- LiteLLM: Universal LLM interface supporting 100+ providers
- FastAPI: Modern async web framework
Key Architecture Decisions
1. InfinityDB over PostgreSQL/pgvector
- Embedded database (no separate server process)
- Optimized for vector similarity search
- Lightweight deployment with file-based storage
- Direct support for multimodal embeddings
2. FastEmbed for Local Embeddings
- No dependency on external embedding APIs
- Fast local inference using ONNX runtime
- Default model: BAAI/bge-small-en-v1.5 (384 dimensions)
- Supports custom models via model registry
3. LiteLLM for LLM Flexibility
- Single interface for all LLM providers
- Supports OpenAI, Anthropic, Ollama, Azure, etc.
- Easy switching between cloud and local models
- Automatic retry and fallback handling
Core Components
Database Schema (InfinityDB Tables)
-
Projects Database
projectstable: User projects with metadata
-
Memories Database
memories_{user_id}tables: Per-user memory storage- Columns: memory_id, content, embedding, metadata, importance
-
Knowledge Database
knowledge_basestable: Knowledge base definitionsfacts_{kb_id}tables: Facts with parent-child relationships
-
Chats Database
chats_{user_id}tables: Conversation history with embeddings
Service Layer
-
EmbeddingService
- Manages FastEmbed model lifecycle
- Batch processing for efficiency
- Similarity computation (cosine, dot, euclidean)
-
LLMService
- LiteLLM integration for all LLM operations
- Summarization with knowledge extraction
- JSON-mode for structured outputs
- PII stripping capabilities
-
MemoryService
- Memory CRUD operations
- Semantic search with optional LLM filtering
- Importance scoring and metadata management
API Design Patterns
Authentication
- Bearer token or x-hanzo-api-key header
- Optional apikey field in JSON body (legacy support)
- DISABLE_AUTH environment variable for development
Request/Response Models
- Lowercase field names for compatibility (userid, messagecontent)
- Pydantic models for validation and documentation
- Consistent error responses with status codes
Unified Search
- Query embeddings generated locally
- Vector similarity search in InfinityDB
- Optional LLM re-ranking for relevance
- Project and session-based filtering
Testing Strategy
Unit Tests
- Service layer testing with mocked dependencies
- Embedding generation and similarity tests
- LLM response parsing and error handling
Integration Tests
- FastAPI TestClient for endpoint testing
- InfinityDB operations with temporary databases
- End-to-end memory storage and retrieval
Test Fixtures
- Temporary database paths
- Sample embeddings and content
- Mock LLM responses for deterministic tests
Performance Optimizations
-
Local Operations
- Embeddings generated in-process
- No network latency for vector operations
- Batch processing where possible
-
Caching Strategy
- Optional Redis integration
- Embedding cache for repeated content
- LLM response caching for common queries
-
Async Operations
- FastAPI async endpoints
- Non-blocking database operations
- Concurrent request handling
Deployment Considerations
Docker Deployment
- Multi-stage build for smaller images
- Non-root user for security
- Health checks for orchestration
- Volume mounting for data persistence
Configuration Management
- Environment variables with HANZO_ prefix
- .env file support for local development
- Sensible defaults for all settings
- Model selection via environment
Scaling Options
- Vertical Scaling: Larger instances for more memory/CPU
- Horizontal Scaling: Multiple instances with shared storage
- Edge Deployment: Fully offline operation with local models
Future Enhancements
-
MCP Server Implementation
- Full Model Context Protocol support
- Tool definitions for memory/knowledge operations
- Integration with Claude Desktop and other MCP clients
-
Advanced Features
- Multi-modal embeddings (images, audio)
- Incremental learning and memory consolidation
- Federated memory sharing between instances
- Advanced graph operations for knowledge bases
-
Performance Improvements
- GPU acceleration for embeddings
- Streaming responses for large result sets
- Distributed vector indices
Common Patterns
Adding New Endpoints
- Define Pydantic models in
models/ - Implement service logic in
services/ - Add FastAPI endpoint in
server.py - Write tests in
tests/
Extending Embedding Support
- Update FastEmbed model in config
- Adjust embedding dimensions
- Regenerate existing embeddings if needed
Custom LLM Integration
- Configure LiteLLM model string
- Set appropriate API base/key
- Adjust temperature and token limits
- Test JSON mode compatibility