# noperator/raink architecture

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

- Interactive diagram: https://gitdiagram.com/noperator/raink
- Repository: https://github.com/noperator/raink

## Overview

##### Main Components
- CLI & Entry Point: Highlight main.go as the entry point that wires up the overall flow.
- Configuration & Argument Parsing: Include components that read command-line flags (e.g., -f for file input, -r for number of runs, etc.) and environment variables (like OPENAI_API_KEY).
- Input/Parsing Module:
– File Loader: Reads the input file. Note that the tool supports both plain text (each line representing a separate document) and JSON arrays.
– Template Engine: Uses Go’s template library to process each input (using the provided -template flag) so that data is properly formatted before being sent for ranking.
- Batch & Run Manager:
– Batch Processor: Groups inputs into batches (default sizes given by flags like -s and -t) and manages multiple runs (as seen from the -r parameter) with parallel processing.
– Parallelization Controller: Emphasizes that in order to use concurrency (parallelizing LLM calls), components (likely implemented with Go channels and goroutines) orchestrate API call executions.
- LLM API Connector:
– Abstraction Layer for External Calls: This module routes requests either to the OpenAI API or the Ollama API (depending on flags like -ollama-model and -ollama-url, or -openai-model).
– Supports a “dry-run” mode where calls are logged instead of executed.
- Aggregator & Ranking Engine:
– Collects responses from parallel API calls.
– Computes scores, exposures, and final rankings.
– Manages any “refinement” steps, as hinted by the –ratio flag.
- Output Formatter:
– Presents the final ranked results to standard output or, if specified, to a JSON output file.
– Ensures that the output conforms to the expected structure (with keys like "key", "value", and "object").

##### Relationships & Data Flow
- The input file is loaded by the File Loader and passed to the Template Engine. Here each document (or line) is transformed as per the provided Go template.
- The processed inputs feed into the Batch & Run Manager that splits the work into manageable groups. It then coordinates parallel API calls via the LLM API Connector.
- Individual LLM responses are sent back to the Aggregator, which consolidates and then ranks the results based on scoring logic.
- Finally, the results get formatted and emitted as output.

##### Architectural Design Choices
- Modular Separation: Each core functionality (input processing, batching, LLM integration, output formatting) is clearly separated, making the system easier to maintain and extend.
- Template-driven Input Processing: The use of Go’s template system for customizing the ranking input is an essential, flexible design choice.
- External Integration: A clear interface for LLM APIs ensures that switching between providers (OpenAI vs. Ollama) can be depicted as conditional or configurable branches.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Layer
    subgraph "CLI Layer"
        CLI["\"CLI & Config Parser\""]:::cli
    end

    %% Input Processing Layer
    subgraph "Input Processing"
        FileLoader["\"File Loader\""]:::processing
        TemplateProcessor["\"Template Processor\""]:::processing
        subgraph "Sample Input Files"
            sentences["\"sentences.txt\""]:::processing
            tlds["\"tlds.txt\""]:::processing
        end
    end

    %% Batch & Run Manager
    subgraph "Batch & Run Manager"
        BatchProcessor["\"Batch Processor\""]:::batch
        ParallelController["\"Parallelization Controller\""]:::batch
    end

    %% LLM Interaction
    subgraph "LLM Interaction"
        LLMConnector["\"LLM API Connector\""]:::connector
    end

    %% External LLM Services
    subgraph "External LLM Services"
        OpenAI["\"OpenAI API\""]:::external
        Ollama["\"Ollama API\""]:::external
    end

    %% Aggregation & Output
    subgraph "Aggregation & Output"
        Aggregator["\"Aggregator/Ranking Engine\""]:::aggregator
        OutputFormatter["\"Output Formatter\""]:::output
    end

    %% Connections
    CLI -->|"invokes"| FileLoader
    CLI -->|"invokes"| TemplateProcessor
    sentences -->|"provides"| FileLoader
    tlds -->|"provides"| FileLoader
    FileLoader -->|"loads input"| TemplateProcessor
    TemplateProcessor -->|"formats data"| BatchProcessor
    BatchProcessor -->|"splits work"| ParallelController
    ParallelController -->|"executes parallel calls"| LLMConnector
    LLMConnector -->|"calls"| OpenAI
    LLMConnector -->|"calls"| Ollama
    LLMConnector -->|"aggregates responses"| Aggregator
    Aggregator -->|"produces ranked output"| OutputFormatter

    %% Click Events
    click CLI "https://github.com/noperator/raink/blob/main/main.go"
    click FileLoader "https://github.com/noperator/raink/tree/main/testdata"
    click sentences "https://github.com/noperator/raink/blob/main/testdata/sentences.txt"
    click tlds "https://github.com/noperator/raink/blob/main/testdata/tlds.txt"

    %% Styles
    classDef cli fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef processing fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef batch fill:#FFFF99,stroke:#000,stroke-width:1px;
    classDef connector fill:#FFC0CB,stroke:#000,stroke-width:1px;
    classDef aggregator fill:#D3D3D3,stroke:#000,stroke-width:1px;
    classDef output fill:#FFA500,stroke:#000,stroke-width:1px;
    classDef external fill:#FF6347,stroke:#000,stroke-dasharray: 5,5,stroke-width:1px;
```
