# pvlbzn/latai architecture

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

- Interactive diagram: https://gitdiagram.com/pvlbzn/latai
- Repository: https://github.com/pvlbzn/latai

## Overview

##### Main Components
- Command Line Entry Points:
- The project includes two entry points: a top-level main.go and one in cmd/main.go. They initialize the application and set up the main loop.
- Terminal UI (TUI) Module:
- Located in internal/tui, it is responsible for rendering the UI and includes the three distinct views (Table View, Information Panel, and Events Panel).
- Prompts Manager:
- Separated in internal/prompt. It handles default prompts (stored in internal/prompt/prompts) and supports loading custom prompts from the user’s home directory (~/.latai/prompts).
- Evaluator:
- Found in internal/evaluator. This module likely orchestrates the measurement of latency by coordinating between sending prompts and receiving responses.
- Providers:
- Built-in providers (OpenAI, Groq, AWS Bedrock) reside in internal/provider. A common Provider interface (defined in provider.go) abstracts the implementations. Each provider is responsible for verifying API keys, sending requests, and measuring latency.

##### Data Flow and Interactions
- User actions (starting the TUI application) are captured by the command line entry point.
- The TUI module displays three panels:
- Table View: shows the list of AI models loaded upon verifying API keys (via provider’s VerifyAccess functions).
- Information Panel: displays real-time performance metrics (e.g., latency, jitter, etc.) provided by the evaluator.
- Events Panel: logs non-blocking events and any errors, especially during asynchronous evaluations.
- The Evaluator module leverages default or custom prompts (managed by the prompt module) and iterates through the provider modules.
- Providers perform API key validation via their VerifyAccess methods, fetch available models, and measure latency (using the Measure method). They have two levels of API calls – one for verification and one for sending latency testing prompts.
- The TUI is updated with responses and test results from the Evaluator and Providers.

##### Important Architectural/Design Patterns and Principles
- Modularity and Separation of Concerns:
- The architecture cleanly separates UI (internal/tui), evaluation logic (internal/evaluator), prompt management (internal/prompt) and provider integration (internal/provider).
- Abstraction via Interfaces:
- A common Provider interface (in provider.go) ensures each provider (OpenAI, Groq, Bedrock) can be interchanged and extended.
- Parallel Execution:
- The README mentions parallel non-blocking measurements; so envision asynchronous operations managed likely within the evaluator and then logged in the events panel.
- Configuration via Environment Variables:
- API keys and settings (like AWS_PROFILE/REGION) are managed externally as shown in the README.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Command Line Entry Points
    subgraph "Command Line Entry Points"
        A1["main.go"]:::entry
        A2["cmd/main.go"]:::entry
    end

    %% UI Layer
    subgraph "UI Layer"
        B["TUI Module"]:::ui
        B1["Table View"]:::uiPanel
        B2["Information Panel"]:::uiPanel
        B3["Events Panel"]:::uiPanel
    end

    %% Business Logic
    subgraph "Business Logic"
        C["Evaluator Module"]:::logic
        D["Prompts Manager"]:::logic
        D1["Prompts Directory"]:::logic
    end

    %% Provider API Layer
    subgraph "Provider API Layer"
        E1["Provider Interface"]:::ext
        E2["OpenAI Provider"]:::ext
        E3["Groq Provider"]:::ext
        E4["AWS Bedrock Provider"]:::ext
    end

    %% External Configuration
    F["Environment Variables"]:::env

    %% Connections from Entry Points to UI and Evaluator
    A1 -->|"initTUI"| B
    A1 -->|"initEvaluator"| C
    A2 -->|"initTUI"| B
    A2 -->|"initEvaluator"| C

    %% UI Components internal layout
    B -->|"render"| B1
    B -->|"render"| B2
    B -->|"render"| B3

    %% Evaluator interactions
    C -->|"fetchPrompts"| D
    D -->|"loadDefaults"| D1
    C -->|"updateMetrics"| B2
    C -->|"logEvents"| B3
    F -->|"configData"| C

    %% Provider API calls from Evaluator
    C -->|"apiCall"| E2
    C -->|"apiCall"| E3
    C -->|"apiCall"| E4

    %% Providers return responses and events
    E2 -->|"sendResponse"| C
    E3 -->|"sendResponse"| C
    E4 -->|"sendResponse"| C
    E2 ---|"asyncEvent"| B3
    E3 ---|"asyncEvent"| B3
    E4 ---|"asyncEvent"| B3

    %% Click Events for Component Mapping
    click A1 "https://github.com/pvlbzn/latai/blob/main/main.go"
    click A2 "https://github.com/pvlbzn/latai/blob/main/cmd/main.go"
    click B "https://github.com/pvlbzn/latai/tree/main/internal/tui"
    click D "https://github.com/pvlbzn/latai/tree/main/internal/prompt"
    click D1 "https://github.com/pvlbzn/latai/tree/main/internal/prompt/prompts"
    click C "https://github.com/pvlbzn/latai/blob/main/internal/evaluator/evaluator.go"
    click E1 "https://github.com/pvlbzn/latai/blob/main/internal/provider/provider.go"
    click E2 "https://github.com/pvlbzn/latai/blob/main/internal/provider/openai.go"
    click E3 "https://github.com/pvlbzn/latai/blob/main/internal/provider/groq.go"
    click E4 "https://github.com/pvlbzn/latai/blob/main/internal/provider/bedrock.go"

    %% Styles
    classDef entry fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef ui fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef uiPanel fill:#e6ffe6,stroke:#008000,stroke-width:1px;
    classDef logic fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef ext fill:#fff2cc,stroke:#ff9900,stroke-width:2px;
    classDef env fill:#e0e0e0,stroke:#707070,stroke-width:2px;
```
