# allenai/infinigram-api architecture

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

- Interactive diagram: https://gitdiagram.com/allenai/infinigram-api
- Repository: https://github.com/allenai/infinigram-api

## Overview

##### Project type and purpose
- This is a microservice‐based, full‑stack “API + worker” application wrapping the Infini‑gram language model for high‑scale, asynchronous inference.
- The top‑level services are:
- API (FastAPI/Uvicorn)
- Attribution Worker (Python worker)
- Shared library (infini‑gram‑processor)
- Web Proxy (Nginx)
- Queue (Redis)
- Infini‑gram engine (external binary/service)
- Index storage (S3/Elasticsearch or custom “indexes”)

##### File structure
- “api/”: REST API, FastAPI entrypoint (app.py), routers/services for attribution and documents, Redis cache, config.
- “attribution_worker/”: standalone worker that pulls from the same Redis queue, processes “attribution” jobs, writes back.
- “packages/infini‑gram‑processor/”: Python library shared by API and worker to invoke the Infini‑gram engine, handle tokenization, models, configs.
- “proxy/”: Nginx configs (local, prod) to reverse‑proxy external traffic to the API.
- “.skiff/” & “.devcontainer/”: Kubernetes/Skiff and local dev container deployment definitions.
- “load‑test/”, “compute_stats/”, “indexing/”: auxiliary scripts and jobs for performance testing, statistics, and index management (not core runtime).
- “otel-collector/”: OpenTelemetry collector config for tracing/metrics.

##### README for architecture insights
API ⇄ Queue ⇄ Worker
API & Worker → Infini‑gram → Indexes
Proxy → API
- Deployment note: API and worker are separate k8s deployments, independently scalable.

##### Elements
- Clear labels: “API”, “Worker”, “Redis Queue”, “Infini‑gram Engine”, “Indexes”, “Proxy”, “otel-collector”.
- Rounded rectangles for services (API, Worker)

## Diagram (Mermaid)

```mermaid
flowchart TD
    direction TB

    User["Client"]:::external

    subgraph "Kubernetes Cluster"
        direction TB

        Proxy(["Nginx Proxy"]):::proxyShape
        click Proxy "https://github.com/allenai/infinigram-api/blob/main/proxy/proxy.conf"

        OTEL[/OpenTelemetry Collector/]:::otelShape
        click OTEL "https://github.com/allenai/infinigram-api/blob/main/otel-collector/otel-collector-config.yaml"

        subgraph "API Service Pod"
            direction TB
            API["API Service"]:::service
            click API "https://github.com/allenai/infinigram-api/blob/main/api/app.py"

            SharedLib["infini-gram-processor"]:::lib
            click SharedLib "https://github.com/allenai/infinigram-api/blob/main/packages/infini-gram-processor/src/infini_gram_processor/processor.py"

            RedisQueue[(Redis Queue)]:::datastore
            click RedisQueue "https://github.com/allenai/infinigram-api/blob/main/api/src/cache/redis.py"
        end

        subgraph "Worker Pod"
            direction TB
            Worker["Attribution Worker"]:::service
            click Worker "https://github.com/allenai/infinigram-api/blob/main/attribution_worker/worker.py"
        end
    end

    subgraph "External Systems"
        direction TB
        Engine[[Infini-gram Engine]]:::external
        Indexes[(Index Storage)]:::datastore
        click Indexes "https://github.com/allenai/infinigram-api/tree/main/indexing/"
    end

    User -->|"HTTP request"| Proxy
    Proxy -->|"forward request"| API

    API -->|"enqueue job"| RedisQueue
    RedisQueue -->|"job pulled"| Worker
    Worker -->|"enqueue result"| RedisQueue
    RedisQueue -->|"result fetched"| API

    API -->|"sync inference"| SharedLib
    Worker -->|"use library"| SharedLib
    SharedLib -->|"invoke model"| Engine
    Engine -->|"read/write data"| Indexes

    API -->|"HTTP response"| User

    API -->|"emit traces/metrics"| OTEL
    Worker -->|"emit traces/metrics"| OTEL
    Proxy -->|"emit traces/metrics"| OTEL
    SharedLib -->|"emit traces/metrics"| OTEL

    classDef proxyShape fill:#ffe4b5,stroke:#d2691e,stroke-width:2px;
    classDef service fill:#add8e6,stroke:#1e90ff,stroke-width:2px,stroke-dasharray: 5 5;
    classDef lib fill:#98fb98,stroke:#228b22,stroke-width:2px;
    classDef datastore fill:#ffe4e1,stroke:#dc143c,stroke-width:2px;
    classDef external fill:#f0e68c,stroke:#b8860b,stroke-width:2px,stroke-dasharray: 2 2;
    classDef otelShape fill:#e6e6fa,stroke:#9370db,stroke-width:2px,stroke-dasharray: 3 3;
    classDef external fill:#fafad2,stroke:#bdb76b,stroke-width:2px;
    classDef external fill:#fafad2,stroke:#bdb76b,stroke-width:2px;
```
