# myeolinmalchi/bge-m3-fastapi architecture

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

- Interactive diagram: https://gitdiagram.com/myeolinmalchi/bge-m3-fastapi
- Repository: https://github.com/myeolinmalchi/bge-m3-fastapi

## Overview

##### Project type and purpose
- This is a Python-based backend service built with FastAPI (“main.py”) that exposes an embedding API. It uses ONNX Runtime (CPU or CUDA) or llama-cpp under the hood to compute text embeddings. It can be driven via a CLI (“cli.py”) or run as a Docker container via Docker Compose.

##### File structure
- Top-level scripts:
- main.py – starts the FastAPI app and wires up command-line args for device and batch size.
- cli.py – optional command-line client to call the embedding endpoint (or directly run embedding).
- models/ – directory where you place your model.onnx and model.onnx.data files.
- schemas/ – Pydantic request/response models (schemas/embed.py).
- utils/embed/ – “strategy” packages for different embedders:
- onnx/ (cpu.py, cuda.py inheriting from base)
- llama_cpp/ (embedder.py, session.py)
- base.py for shared interface.
- utils/preprocess/ – text and HTML cleaning modules.
- utils/logger.py and utils/mixins/singleton.py – cross-cutting utilities.
- test.py – simple smoke/test script.

##### README for insights
- Confirms use of Python ≥3.10, Poetry for dependency management.
- Instructs placement of ONNX model files in models/ and how to run locally or via Docker Compose.

##### Main components
a. Client Layer
- CLI client (cli.py)
- HTTP clients (curl, Postman, other services calling the FastAPI endpoint)
b. API Layer (FastAPI)
- main.py – entrypoint, argument parsing, mounting routes
- schemas/embed.py – request/response validation
c. Preprocessing Layer
- utils/preprocess/text.py and html.py – prepare raw input
d. Embedding Layer (Strategy Pattern)
- utils/embed/base.py – abstract interface
- utils/embed/onnx/{cpu.py, cuda.py} – ONNX Runtime implementations
- utils/embed/llama_cpp/embedder.py & session.py – llama-cpp implementation
e. Model Store
- models/ – ONNX model files on disk
f. Infrastructure & Cross-Cutting
- utils/logger.py – logging
- utils/mixins/singleton.py – session or resource pooling
- Docker (Dockerfile + docker-compose)
- GitHub Actions workflow (.github/workflows/llama-cpp.yaml)

##### Relationships and data flows
1. Client → FastAPI HTTP POST /embed (or CLI)
2. API Layer validates via Pydantic schemas
3. Raw text → Preprocessor → cleaned text
4. Cleaned text → Embedding Layer
– Strategy chooses ONNX or llama-cpp based on config
– Embedding Layer loads model from models/ and runs inference
5. Embedding vector returned to API Layer
6. API Layer serializes response → Client

##### Key architectural patterns & principles
- Strategy Pattern for pluggable embedders (base.py + onnx vs llama-cpp)
- Singleton or session pooling for ONNX or llama-cpp sessions
- Clear separation of concerns: API, preprocessing, embedding, logging
- Containerization for environment consistency and scaling

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Client Layer"
        CLI["CLI Client"]:::external
        HTTP["HTTP Clients\n(curl, Postman)"]:::external
    end

    subgraph "API Layer"
        API["main.py\n(FastAPI Entrypoint)"]:::api
        Schema["schemas/embed.py\n(Request/Response)"]:::api
    end

    subgraph "Preprocessing Layer"
        TextPrep["utils/preprocess/text.py"]:::processing
        HTMLPrep["utils/preprocess/html.py"]:::processing
    end

    subgraph "Embedding Layer"
        Base["utils/embed/base.py\n(Strategy Interface)"]:::embedding
        subgraph "ONNX Runtime"
            ONNXBase["utils/embed/onnx/base.py"]:::embedding
            CPU["utils/embed/onnx/cpu.py\n(CPU Embedder)"]:::embedding
            CUDA["utils/embed/onnx/cuda.py\n(CUDA Embedder)"]:::embedding
        end
        subgraph "llama-cpp"
            LlamaEmbed["utils/embed/llama_cpp/embedder.py"]:::embedding
            LlamaSession["utils/embed/llama_cpp/session.py"]:::embedding
        end
    end

    ModelStore["models/"]:::storage

    subgraph "Cross-Cutting Utilities"
        Logger["utils/logger.py"]:::infra
        Singleton["utils/mixins/singleton.py"]:::infra
    end

    subgraph "Infrastructure & Deployment"
        Dockerfile["Dockerfile"]:::infra
        CI[".github/workflows/llama-cpp.yaml"]:::infra
    end

    Test["test.py"]:::infra

    CLI -->|"executes"| API
    HTTP -->|"POST /embed"| API
    API -->|"validate request"| Schema
    API -->|"raw text"| TextPrep
    API -->|"raw text"| HTMLPrep
    TextPrep -->|"cleaned text"| Base
    HTMLPrep -->|"cleaned text"| Base
    Base -->|"use ONNX"| ONNXBase
    Base -->|"use llama-cpp"| LlamaEmbed
    ONNXBase -->|"inherits to"| CPU
    ONNXBase -->|"inherits to"| CUDA
    CPU -->|"load model & infer"| ModelStore
    CUDA -->|"load model & infer"| ModelStore
    LlamaEmbed -->|"manage session"| LlamaSession
    LlamaSession -->|"load model"| ModelStore
    CPU -->|"embedding vector"| API
    CUDA -->|"embedding vector"| API
    LlamaEmbed -->|"embedding vector"| API
    API -->|"response"| HTTP
    API -->|"response"| CLI
    CLI -->|"invoke embedding"| Base
    Test -->|"smoke test"| API

    Logger -.-> API
    Logger -.-> TextPrep
    Logger -.-> Base
    Singleton -.-> CPU
    Singleton -.-> CUDA
    Singleton -.-> LlamaSession

    Dockerfile --> API
    Dockerfile --> CLI
    CI --> Dockerfile

    click CLI "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/cli.py"
    click API "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/main.py"
    click Schema "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/schemas/embed.py"
    click TextPrep "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/preprocess/text.py"
    click HTMLPrep "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/preprocess/html.py"
    click Base "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/embed/base.py"
    click ONNXBase "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/embed/onnx/base.py"
    click CPU "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/embed/onnx/cpu.py"
    click CUDA "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/embed/onnx/cuda.py"
    click LlamaEmbed "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/embed/llama_cpp/embedder.py"
    click LlamaSession "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/embed/llama_cpp/session.py"
    click ModelStore "https://github.com/myeolinmalchi/bge-m3-fastapi/tree/main/models/"
    click Logger "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/logger.py"
    click Singleton "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/utils/mixins/singleton.py"
    click Dockerfile "https://github.com/myeolinmalchi/bge-m3-fastapi/tree/main/Dockerfile"
    click CI "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/.github/workflows/llama-cpp.yaml"
    click Test "https://github.com/myeolinmalchi/bge-m3-fastapi/blob/main/test.py"

    classDef external fill:#dde2e6,stroke:#888
    classDef api fill:#cce5ff,stroke:#337ab7
    classDef processing fill:#dff0d8,stroke:#3c763d
    classDef embedding fill:#e6f7ff,stroke:#5bc0de
    classDef storage fill:#fcf8e3,stroke:#8a6d3b
    classDef infra fill:#fcf8e3,stroke:#8a6d3b
```
