How tovbinz/unifiedai works
Main components
a. User Application
– Your client code that imports and uses UnifiedAI (e.g., scripts, notebooks).
b. UnifiedAI Library
– Core modules:
• unified.py – The façade that exposes the AI and Batch classes.
• api.py – BaseClient or abstract layer defining common methods and data structures (set_instructions, get_response, usage tracking).
• Provider modules (claude.py, gemini.py, gpt.py, ollama.py) – Concrete adapters implementing BaseClient for each AI provider.
c. External AI Services
– OpenAI, Anthropic (Claude), Google (Gemini), Ollama – accessed via HTTP/HTTPS.
d. Configuration / Credentials management
– API keys passed into the AI constructor, possibly environment variables or secrets manager.
Relationships and data flow
– User Application → UnifiedAI.unified.AI (or Batch)
– AI class → Base API layer (api.py)
• set_instructions, set_max_tokens, add_context stored in AI instance.
– AI instance → Provider Adapter
• Each API call (get_response) routes through unified.py into the corresponding provider module.
– Provider Adapter → External Service Endpoint
– External Service → Provider Adapter
• HTTP response containing tokens and content.
– Provider Adapter → AI instance
• Response mapped into AI.history and AI.usage.
– Batch → Fan-out to multiple AI instances in parallel
Architectural patterns
– Adapter Pattern
• Provider modules implement a uniform interface defined by api.py.
– Façade Pattern
• unified.py exposes a simplified interface (AI, Batch) hiding the complexity of multiple APIs.
– Dependency Injection
• API key, model name injected into AI constructor.
– Single Responsibility
• Each provider module handles only its own protocol.
Relevant technologies/libraries
– Python language & package structure
– HTTP client (e.g., requests or httpx) used under the hood in provider adapters
– JSON serialization/deserialization for requests/responses
– Environment variables or config files for API key management