United LLM client with search capabilities and FastAPI server
Project description
๐ United LLM
A comprehensive, production-ready LLM client with multi-provider support, web search integration, structured outputs, and comprehensive logging system.
โจ Key Features
- ๐ Multi-Provider Support: OpenAI (GPT-4o, GPT-4o-mini), Anthropic (Claude Sonnet 4), Google (Gemini 1.5), and Ollama (Qwen3)
- ๐ Advanced Web Search:
- Anthropic native web search integration
- DuckDuckGo 3-step search with intelligent query optimization
- ๐ FastAPI Admin Interface: RESTful API with automatic documentation and database management
- ๐ Structured Outputs: Pydantic models with instructor library integration
- ๐๏ธ Comprehensive Logging:
- SQLite database logging with detailed metrics
- TXT file logging with daily organization
- JSON structured logs for programmatic analysis
- Rotating application and server logs
- โ๏ธ Smart Configuration: Environment-based settings with automatic model detection and smart config merging
- ๐ง Production Ready: Rate limiting, error handling, fallback strategies
๐๏ธ Architecture
united_llm/
โโโ client.py # Enhanced LLMClient with search & logging
โโโ config/
โ โโโ settings.py # Environment-based configuration
โ โโโ bootstrap.py # Bootstrap configuration loader
โ โโโ logging_config.py # Comprehensive logging setup
โโโ search/
โ โโโ anthropic_search.py # Anthropic web search integration
โ โโโ duckduckgo_search.py # DuckDuckGo 3-step search
โโโ utils/
โ โโโ database.py # SQLite logging database
โ โโโ schema_converter.py # JSON Schema โ Pydantic conversion
โ โโโ schema_utils.py # Local schema utilities (compatibility layer)
โ โโโ model_manager.py # Model detection and management
โโโ api/
โ โโโ server.py # FastAPI server with admin interface
โ โโโ admin.py # Admin interface implementation
โ โโโ schemas.py # Request/response models
โโโ tests/ # Comprehensive test suite
๐ Quick Start
1. Installation
# Clone the repository
git clone https://github.com/xychenmsn/united_llm.git
cd united_llm
# Create virtual environment (recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Or install in development mode (recommended for development)
pip install -e .
# Optional: Install development dependencies
pip install -e ".[dev]" # Includes pytest, black, flake8, mypy
2. Configuration
Create a .env file with your API keys:
# Copy example configuration
cp env_example.txt .env
# Edit with your API keys
LLM__OPENAI_API_KEY=your_openai_key_here
LLM__ANTHROPIC_API_KEY=your_anthropic_key_here
LLM__GOOGLE_API_KEY=your_google_key_here
LLM__OLLAMA_BASE_URL=http://localhost:11434/v1
# Optional: Configure default model and logging
LLM__DEFAULT_MODEL=gpt-4o-mini
LLM__LOG_TO_DB=true
LLM__LOG_LEVEL=INFO
3. Basic Usage
from united_llm import LLMClient
from pydantic import BaseModel
# Recommended: Use automatic configuration
client = LLMClient() # Automatically loads from environment/config files
# Or override specific settings only (Smart Config Merging)
client = LLMClient(config={'log_calls': True}) # Just enable logging
# Define output structure
class ArticleSummary(BaseModel):
title: str
summary: str
key_points: list[str]
confidence: float
# Generate structured output
result = client.generate_structured(
prompt="Summarize the latest developments in AI",
output_model=ArticleSummary,
model="gpt-4o-mini"
)
print(result.title) # "Latest AI Developments"
print(result.summary) # "Recent advances include..."
4. Web Search Integration
# DuckDuckGo search with any model (3-step process)
result = client.generate_structured(
prompt="What are the latest AI breakthroughs in 2024?",
output_model=ArticleSummary,
model="gpt-4o-mini",
duckduckgo_search=True # Enables intelligent search
)
# Anthropic web search (Anthropic models only)
result = client.generate_structured(
prompt="Current AI research trends",
output_model=ArticleSummary,
model="claude-3-5-sonnet-20241022",
anthropic_web_search=True
)
๐ฏ Smart Configuration System
Key Insight: Override Only What You Need
The LLMClient features smart config merging - when you pass a config dictionary, it merges with bootstrap defaults instead of replacing them. This means you only need to specify the settings you want to override!
Before (old behavior):
# User had to specify EVERYTHING
config = {
'openai_api_key': 'your-key',
'anthropic_api_key': 'your-other-key',
'google_api_key': 'your-google-key',
'ollama_base_url': 'http://localhost:11434/v1',
'log_calls': True, # This is all they wanted to change!
'log_to_db': True,
'db_path': 'custom_path.db'
}
client = LLMClient(config=config)
After (new behavior):
# User only specifies what they want to change
client = LLMClient(config={'log_calls': True}) # That's it!
# All other settings (API keys, etc.) come from bootstrap automatically
How Config Merging Works
- Bootstrap loads first - API keys, paths, defaults from environment/config files
- User config overlays - Only the keys you specify override bootstrap
- Everything else preserved - Model lists, paths, other API keys stay intact
Common Use Cases
# Enable logging only
client = LLMClient(config={'log_calls': True})
# Test with different API key
client = LLMClient(config={'openai_api_key': 'test-key-12345'})
# Use remote Ollama
client = LLMClient(config={'ollama_base_url': 'http://remote-server:11434/v1'})
# Development vs Production
dev_client = LLMClient(config={'log_calls': True, 'log_to_db': True})
prod_client = LLMClient(config={'log_calls': False})
# Multiple overrides
client = LLMClient(config={
'openai_api_key': 'test-key',
'log_calls': True,
'log_to_db': False,
'ollama_base_url': 'http://custom-ollama:11434/v1'
})
Configuration Debugging
from united_llm import setup_environment, get_config
# ๐ DEBUGGING: Inspect zero-config configuration
setup_environment() # Optional - LLMClient does this automatically
config = get_config()
print(f"API key: {config.get('openai_api_key', '')[:20]}...")
print(f"Log calls: {config.get('log_calls')}")
print(f"All config keys: {list(config.to_dict().keys())}")
# Check specific configuration values
print(f"Default model: {config.get('default_model')}")
print(f"Database path: {config.get_db_path()}")
print(f"Data path: {config.data_path()}")
print(f"Logs path: {config.logs_path()}")
Configuration Priority Order:
.env.united_llmfile (highest priority)- Environment variables with aliases (LLM***, ADMIN**_, API___, etc.)
- Standard API key environment variables (OPENAI_API_KEY, etc.)
- TOML config files (lowest priority)
๐ FastAPI Admin Interface
Start the Server
# Development mode
source .venv/bin/activate
python -m united_llm.api.server
# Or use the console script (after installation)
united-llm-server
# Or use the restart script (kills existing processes and restarts)
./restart.sh
# Production mode
uvicorn united_llm.api.server:app --host 0.0.0.0 --port 8818 --workers 4
Admin Interface Features
Visit http://localhost:8818 for the admin interface:
- ๐ Enhanced Dashboard: Real-time statistics, provider charts, model analytics
- ๐ Request History: Comprehensive LLM call history with advanced filtering
- ๐ Export Functionality: CSV and JSON export capabilities
- ๐ Authentication: HTTP Basic Auth protection (admin:admin by default)
- ๐จ Modern UI: Responsive design with FontAwesome icons
- ๐ Search Testing: Test search capabilities with different models
- ๐ Analytics: Token usage, response times, error rates
- ๐ฏ Send Request Dialog: Interactive form with model selection, schema input, and tabbed output
- ๐ป Code Generation: Generate Python code examples for your specific prompts and schemas
- ๐ฑ Responsive Design: Works on desktop and mobile devices
API Endpoints
Core Generation Endpoints
POST /generate/dict- Generate plain dictionaries with string schemasPOST /generate/united- ๐ United schema endpoint with auto-detection (supports both JSON Schema and string schemas)
Admin & Management
GET /- Admin dashboard interface with real-time analyticsGET /admin- Redirect to main dashboardGET /admin/llm_calls- Enhanced request history with filteringGET /admin/export/csv- Export call history as CSVGET /admin/export/json- Export call history as JSONGET /admin/logout- Admin logout endpoint
System Information
GET /models- List available models and their statusGET /health- System health check with provider statusGET /stats- Usage statistics and metrics
Validation & Testing
POST /validate-schema- Validate JSON schemasPOST /schema/validate-string- Validate string schemas with optimizationPOST /test-search- ๐ Test search functionality with different providers
Authentication
POST /admin/login- Admin login (HTTP Basic Auth)
Admin Interface Usage
Dashboard: Monitor system health, view statistics, and access quick actions
Requests Page:
- View complete request history with filtering by model, provider, date range
- Click "View" to see full request details including schema and response
- Click "Send Request" to open the interactive request dialog
- Export data as CSV or JSON for analysis
Send Request Dialog:
- Model Selection: Choose from available models with intelligent defaults
- Schema Input: Use JSON Schema or string schema syntax (supports curly brace format)
- Web Search: Enable DuckDuckGo or Anthropic web search
- Tabbed Output: Switch between "Output" (LLM response) and "Code" (Python examples)
- Generate Code: Get Python code examples for your specific prompt and schema
Navigation: Use the sidebar to switch between Dashboard and Requests pages
๐ New Schema Syntax Features
United Schema API
The new /generate/united endpoint accepts either JSON Schema or string schema definitions with auto-detection:
# Works with JSON Schema
result = client.generate_structured(
prompt="Extract user info: John Doe, 30, from NYC",
schema={
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"city": {"type": "string"}
}
}
)
# Works with string schema
result = client.generate_structured(
prompt="Extract user info: John Doe, 30, from NYC",
schema="name, age:int, city"
)
Curly Brace Syntax (NEW!)
JSON-consistent syntax with {} for objects and [] for arrays:
# Simple object
result = client.generate_dict(
prompt="Create user: Alice, 25, alice@example.com",
schema="{name, age:int, email}",
model="claude-sonnet-4-20250514"
)
# Returns: {"name": "Alice", "age": 25, "email": "alice@example.com"}
# Array of objects
result = client.generate_dict(
prompt="Create contact list with 2 people",
schema="[{name, email}]",
model="claude-sonnet-4-20250514"
)
# Nested structure
team = client.generate_dict(
prompt="Engineering team with 2 developers",
schema="{team, members:[{name, role}]}",
model="claude-sonnet-4-20250514"
)
Benefits of New Syntax
- 50% less code for common use cases
- Direct JSON serialization (no
.model_dump()needed) - Perfect for web APIs and microservices
- Same validation as Pydantic models
- JSON consistency:
{}= objects,[]= arrays - 100% backward compatibility: All legacy syntax still works
๐ Comprehensive Logging System
The system provides multi-level logging for complete observability:
1. Database Logging (SQLite)
- Location:
llm_calls.db - Content: Complete request/response records with metadata
- Features: Searchable, exportable, with usage statistics
- Schema: Timestamps, models, providers, tokens, duration, errors
2. TXT File Logging
- Location:
logs/llm_calls/YYYY-MM-DD.txt - Content: Human-readable call logs organized by date
- Format: Structured text with timestamps and metadata
- Rotation: Daily log files with automatic cleanup
3. JSON Structured Logs
- Location:
logs/llm_calls/YYYY-MM-DD.json - Content: Machine-readable JSON logs for analysis
- Features: Programmatic log analysis and monitoring
- Integration: Perfect for log aggregation systems
4. Application Logs
- Location:
logs/api.log,logs/api.log.bak - Content: FastAPI server logs and application events
- Features: Automatic backup and rotation
- Levels: INFO, DEBUG, WARNING, ERROR
๐งช Examples and Testing
Examples Directory (examples/)
Learn how to use the library with organized examples:
basic_usage.py- Core functionality and getting startedweb_search.py- DuckDuckGo and Anthropic web search integrationadvanced_features.py- Configuration, fallbacks, and advanced patterns
Tests Directory (tests/)
Verify functionality with comprehensive tests:
test_integration.py- Real-world integration testingtest_comprehensive.py- Complete system test with all providers
See examples/README.md and tests/README.md for detailed descriptions.
Running Examples and Tests
# Activate virtual environment
source .venv/bin/activate
# Run examples (start here!)
python examples/basic_usage.py
python examples/web_search.py
python examples/advanced_features.py
# Run tests (verify functionality)
python tests/test_integration.py
python tests/test_comprehensive.py
๐ง Development and Production
Development Workflow
# Start development server manually
source .venv/bin/activate
python -m united_llm.api.server
# Or use the restart script
./restart.sh
# View logs in logs/ directory
tail -f logs/api.log
Production Deployment
# Production mode with uvicorn
source .venv/bin/activate
uvicorn united_llm.api.server:app --host 0.0.0.0 --port 8080 --workers 4
# Or use restart script on different port
API_PORT=8080 ./restart.sh
Environment Variables
The system supports multiple environment variable formats:
Standard Format (Recommended)
# API Keys
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key
GOOGLE_API_KEY=your_google_key
# Configuration
LLM__DEFAULT_MODEL=gpt-4o-mini
LLM__LOG_TO_DB=true
LLM__LOG_CALLS=true
Domain-Specific Format (.env.united_llm)
# Clean format without prefixes
DEFAULT_MODEL=gpt-4o-mini
LOG_TO_DB=true
LOG_CALLS=true
OPENAI_API_KEY=your_openai_key
Namespaced Format
# Full namespace
UNITED_LLM_DEFAULT_MODEL=gpt-4o-mini
UNITED_LLM_LOG_TO_DB=true
UNITED_LLM_OPENAI_API_KEY=your_openai_key
๐ Advanced Features
Chinese Text Optimization
The system includes specialized Chinese text processing:
# Automatic Chinese query optimization
result = client.generate_structured(
prompt="ๅๆๆๆฐ็ไบบๅทฅๆบ่ฝๆๆฏ่ถๅฟ",
output_model=TechTrend,
duckduckgo_search=True # Optimizes Chinese search automatically
)
Model Fallback
Automatic fallback to working models when preferred models are unavailable:
# Will try gpt-4 first, fall back to gpt-3.5-turbo if unavailable
client = LLMClient(config={
'default_model': 'gpt-4',
'fallback_models': ['gpt-3.5-turbo', 'claude-3-haiku']
})
Rate Limiting and Error Handling
- Automatic retry with exponential backoff
- Rate limit detection and handling
- Comprehensive error logging and reporting
- Graceful degradation strategies
๐ฆ Dependencies & Installation Options
Core Dependencies
Core dependencies are automatically installed with the base package:
# Core LLM libraries
instructor>=1.4.3
pydantic>=2.0.0
pydantic-settings>=2.0.0
# LLM Provider SDKs
openai>=1.12.0
anthropic>=0.21.0
google-generativeai>=0.8.0
# Web framework
fastapi>=0.104.0
uvicorn[standard]>=0.24.0
# Search and utilities
duckduckgo-search>=6.1.0
requests>=2.31.0
httpx>=0.27.0
python-dotenv>=1.0.0
Installation Options
# Basic installation
pip install -e .
# Development installation (includes testing tools)
pip install -e ".[dev]"
# Includes: pytest, pytest-asyncio, pytest-cov, black, flake8, mypy, pre-commit
# Documentation installation
pip install -e ".[docs]"
# Includes: mkdocs, mkdocs-material, mkdocstrings
# Complete installation (everything)
pip install -e ".[all]"
Console Scripts
After installation, you can use these command-line tools:
# Start the API server
united-llm-server
# CLI interface (if implemented)
united-llm --help
๐ Production Readiness
The united LLM project is production-ready with:
- ๐ Comprehensive Documentation: Complete API docs and examples
- โ๏ธ Flexible Configuration: Environment-based settings with smart merging
- ๐ง Error Handling: Robust error handling and fallbacks
- ๐ Monitoring: Statistics and logging capabilities
- ๐ API Server: Production-ready FastAPI server
- ๐ Search Integration: Both Anthropic and DuckDuckGo search
- ๐จ๐ณ Chinese Support: Specialized Chinese text processing
- ๐ Multi-Provider: Support for all major LLM providers
- ๐ก๏ธ Security: HTTP Basic Auth and input validation
- ๐ Scalability: Handles thousands of requests efficiently
๐ Recent Updates
v2.1 - Enhanced Features & Reliability (Current)
- โ Ollama Function Detection: Automatic detection of function calling capabilities
- โ
Search Testing Endpoint:
/test-searchfor validating search functionality - โ Enhanced Error Handling: Better fallback strategies and error reporting
- โ
Console Scripts:
united-llm-servercommand-line tool - โ Improved Documentation: Updated README and development guide
- โ Better Type Hints: Enhanced type safety throughout codebase
v2.0 - Schema Unification & Config Improvements
- โ United Schema API: Single endpoint handles both JSON Schema and string definitions
- โ
Curly Brace Syntax: JSON-consistent
{}and[]syntax - โ Smart Config Merging: Override only what you need
- โ Enhanced Admin Interface: Modern UI with real-time statistics
- โ
Improved Debugging:
get_effective_config()for final configuration inspection - โ
Namespace Consistency: All imports from
united_llm - โ Pydantic v2 Compatibility: Eliminated schema field conflicts
v1.5 - Admin Interface & Logging
- โ Admin Dashboard: Beautiful web interface with analytics
- โ Database Logging: SQLite with comprehensive metrics
- โ Export Functionality: CSV and JSON export capabilities
- โ Search Integration: DuckDuckGo and Anthropic web search
- โ Multi-Provider Support: OpenAI, Anthropic, Google, Ollama
๐ Support
For issues, questions, or contributions:
- Check the comprehensive test files for usage examples
- Review the admin interface for debugging tools
- Use
get_effective_config()for configuration debugging - Check logs in the
logs/directory for detailed error information
๐ก Pro Tip: Start with LLMClient() for automatic configuration, then use config={'key': 'value'} to override only specific settings you need to change!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file united_llm-0.1.2.tar.gz.
File metadata
- Download URL: united_llm-0.1.2.tar.gz
- Upload date:
- Size: 100.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50d7944d61ed9ddfd40f6a6e5f2c3d68f9ebc3da0e5bab295d93c06034edfbfb
|
|
| MD5 |
0a3d9de869fca21fdf6af7b26109406a
|
|
| BLAKE2b-256 |
9ba156148d00666bf5dcbdb70bd419348691446b7235920ce2671d236d1d59be
|
File details
Details for the file united_llm-0.1.2-py3-none-any.whl.
File metadata
- Download URL: united_llm-0.1.2-py3-none-any.whl
- Upload date:
- Size: 92.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00df85b6bc262f6c64f73e1bcb3df6ba67e0eb3b43f00d40f0b3227e80df785a
|
|
| MD5 |
d3b7cefbd3564a3e9385acdb1457c993
|
|
| BLAKE2b-256 |
de4d3d40720b9737cd62b4992ba13b85b69f25722125b45bc15bcac8b2c9e7a8
|