# tovbinz/unifiedai architecture

> Architecture diagram of the GitHub repository tovbinz/unifiedai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tovbinz/unifiedai
- Repository: https://github.com/tovbinz/unifiedai

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    User["User Code\n(scripts/notebooks)"]:::user

    subgraph "UnifiedAI Library"
        direction TB
        Init["__init__.py"]:::library
        Unified["unified.py\n(AI & Batch)"]:::library
        API["api.py\n(BaseClient)"]:::library
        Claude["claude.py\n(Anthropic Adapter)"]:::library
        Gemini["gemini.py\n(Google Adapter)"]:::library
        GPT["gpt.py\n(OpenAI Adapter)"]:::library
        Ollama["ollama.py\n(Ollama Adapter)"]:::library
    end

    subgraph "External AI Services"
        direction TB
        Anthropic["api.anthropic.com"]:::ext
        GoogleGemini["api.generativeai.googleapis.com"]:::ext
        OpenAI["api.openai.com"]:::ext
        OllamaServer["Ollama Server"]:::ext
    end

    User -->|"instantiate AI()/Batch()"| Unified
    Unified -->|"uses interface"| API
    API -->|"get_response(request_payload)"| Claude
    API -->|"get_response(request_payload)"| Gemini
    API -->|"get_response(request_payload)"| GPT
    API -->|"get_response(request_payload)"| Ollama

    Claude -->|"HTTP request/response"| Anthropic
    Gemini -->|"HTTP request/response"| GoogleGemini
    GPT -->|"HTTP request/response"| OpenAI
    Ollama -->|"HTTP request/response"| OllamaServer

    Anthropic -->|"response JSON"| Claude
    GoogleGemini -->|"response JSON"| Gemini
    OpenAI -->|"response JSON"| GPT
    OllamaServer -->|"response JSON"| Ollama

    click Init "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/__init__.py"
    click Unified "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/unified.py"
    click API "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/api.py"
    click Claude "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/claude.py"
    click Gemini "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/gemini.py"
    click GPT "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/gpt.py"
    click Ollama "https://github.com/tovbinz/unifiedai/blob/main/UnifiedAI/ollama.py"

    classDef library fill:#D6EAF8,stroke:#1B4F72,color:#1B2631
    classDef ext fill:#D5F5E3,stroke:#196F3D,color:#145A32
    classDef user fill:#FCF3CF,stroke:#B7950B,color:#7D6608
```
