# dontizi/rlama architecture

> Architecture diagram of the GitHub repository dontizi/rlama, 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/dontizi/rlama
- Repository: https://github.com/dontizi/rlama

## Overview

##### Project Type & Purpose
- RLAMA is a CLI-based, AI-driven question-answering tool designed to build and manage local Retrieval-Augmented Generation (RAG) systems. Its purpose is to process documents, generate embeddings via an external Ollama LLM (and optionally OpenAI/Hugging Face models), and enable users to query content effectively.
- The README confirms that RLAMA is built with Go and follows a clean architecture pattern with clear separation between CLI commands, business logic, and data persistence.

##### Analyzing the File Structure
- The top-level directories and files indicate major components:
- The “cmd” directory holds various CLI commands (e.g., rag.go, run.go, wizard.go) using Cobra to define the application’s command structure.
- The “internal” directory is organized into sub-packages like:
° client: Contains external API clients for integrating with Ollama, OpenAI, etc.
° config: For configuration management.
° crawler: Implements web crawling logic.
° domain: Defines core domain entities (documents, RAG systems, etc.).
° repository: Handles data persistence for profiles and RAGs.
° server: Hosts the API server endpoints.
° service: Contains business logic for document processing, chunking, embedding, and interactions with external models.
- The “pkg” directory provides shared utilities such as vector operations (e.g. HNSW vector store).
- Additional directories (docs, tests, scripts) indicate supporting documentation, end-to-end testing, and build/deployment scripts.
- This structure indicates a clean separation of concerns, matching a layered (or hexagonal) architectural pattern.

##### Examining the README for Architectural Insights
- The README explicitly outlines the architecture:
- It shows the separation between document processing, embedding generation, vector search, and context building.
- It provides a visual representation of data flow from input documents through processing channels to interaction with the LLM (Ollama) for final responses.
- Key technologies and patterns noted include:
- Go for the core implementation.
- Cobra for the CLI.
- Local filesystem-based data storage.
- Custom vector retrieval (cosine similarity) and support for multiple document formats.
- External integration with Ollama (and optional OpenAI/Hugging Face).

a. Main Components:
– CLI Interface: Representing the “cmd” package with its variety of commands (rag, run, wizard, etc.)
– Business/Application Layer: Encompassing “internal/service”, “internal/crawler”, and “internal/server” that implement RAG system functionality, document chunking, embedding, and query processing.
– Domain Models and Persistence: The “internal/domain” and “internal/repository” packages that maintain core entities (RAG system, document chunks) and storage to filesystem (e.g., in ~/.rlama).
– External Integrations: Components for interacting with external LLMs (Ollama API client in “internal/client”), and additional modules for OpenAI or Hugging Face integrations.
– Utility & Shared Services: The “pkg/vector” components handling vector stores and similarity search.

b. Relationships & Interactions:
– The service layer should show interactions with domain models and repository for persisting/loading data.
– Also, represent the vector store (in pkg/vector) as a component that the service queries during context retrieval.
– For the API server (internal/server), show how external HTTP requests are processed, invoking similar services as the CLI does.

c. Architectural Patterns & Principles:
– Emphasize the Clean/Hexagonal Architecture: Show separation between “drivers” (CLI/API) and the “core” (domain, service) with adapters (repository, client).
– Indicate modularity through encapsulated components and their interactions.
– Represent integration with external services like Ollama using distinct connectors (client components) to underline dependency inversion.

d. Relevant Technologies & Tools:
– Mark that the core language is Go.
– Indicate the CLI framework (Cobra) in the CLI block.
– Showcase that the vector search uses cosine similarity implemented in the “pkg/vector” module.
– Note dependencies such as Ollama API, OpenAI, and Hugging Face integration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Interface Layer
    subgraph "Interface Layer"
        CLI_RAG["rag.go (CLI)"]:::cli
        CLI_RUN["run.go (CLI)"]:::cli
        CLI_WIZ["wizard.go (CLI)"]:::cli
        API_Server_Interface["API Server"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        Embedding_Service["Embedding Service"]:::service
        RAG_Service["RAG Service"]:::service
        Chunker_Service["Chunker Service"]:::service
        Crawler_Service["Crawler"]:::service
    end

    %% Domain & Persistence
    subgraph "Domain & Persistence"
        Document_Domain["Document Domain"]:::domain
        RAG_Domain["RAG Domain"]:::domain
        RAG_Repository["RAG Repository"]:::domain
        Profile_Repository["Profile Repository"]:::domain
    end

    %% External Integrations
    subgraph "External Integrations"
        Ollama_Client["Ollama Client"]:::external
        OpenAI_Client["OpenAI Client"]:::external
    end

    %% Utility & Shared Services
    subgraph "Utility & Shared Services"
        Vector_Store["Vector Store"]:::utility
        Vector_Store_Utils["Vector Store Utils"]:::utility
    end

    %% Connections from Interface Layer to Service Layer
    CLI_RAG -->|"triggers"| RAG_Service
    CLI_RUN -->|"triggers"| RAG_Service
    CLI_WIZ -->|"triggers"| RAG_Service
    API_Server_Interface -->|"triggers"| RAG_Service

    %% Service Layer interactions
    RAG_Service -->|"uses"| Embedding_Service
    RAG_Service -->|"queries"| Document_Domain
    RAG_Service -->|"persists_to"| RAG_Repository
    RAG_Service -->|"persists_to"| Profile_Repository
    Embedding_Service -->|"calls"| Ollama_Client
    Embedding_Service -->|"calls"| OpenAI_Client
    Embedding_Service -->|"queries"| Vector_Store
    Chunker_Service -->|"processes"| Document_Domain
    Crawler_Service -->|"ingests"| Document_Domain

    %% Class Definitions for coloring
    classDef cli fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef api fill:#ffcccb,stroke:#000,stroke-width:2px;
    classDef service fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef domain fill:#ffd700,stroke:#000,stroke-width:2px;
    classDef external fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef utility fill:#dda0dd,stroke:#000,stroke-width:2px;

    %% Click Events for CLI Interface
    click CLI_RAG "https://github.com/dontizi/rlama/blob/main/cmd/rag.go"
    click CLI_RUN "https://github.com/dontizi/rlama/blob/main/cmd/run.go"
    click CLI_WIZ "https://github.com/dontizi/rlama/blob/main/cmd/wizard.go"
    click API_Server_Interface "https://github.com/dontizi/rlama/blob/main/internal/server/server.go"

    %% Click Events for Business/Application Layer
    click Embedding_Service "https://github.com/dontizi/rlama/blob/main/internal/service/embedding_service.go"
    click RAG_Service "https://github.com/dontizi/rlama/blob/main/internal/service/rag_service.go"
    click Chunker_Service "https://github.com/dontizi/rlama/blob/main/internal/service/chunker_service.go"
    click Crawler_Service "https://github.com/dontizi/rlama/blob/main/internal/crawler/crawler.go"

    %% Click Events for Domain Models and Persistence
    click Document_Domain "https://github.com/dontizi/rlama/blob/main/internal/domain/document.go"
    click RAG_Domain "https://github.com/dontizi/rlama/blob/main/internal/domain/rag.go"
    click RAG_Repository "https://github.com/dontizi/rlama/blob/main/internal/repository/rag_repository.go"
    click Profile_Repository "https://github.com/dontizi/rlama/blob/main/internal/repository/profile_repository.go"

    %% Click Events for External Integrations
    click Ollama_Client "https://github.com/dontizi/rlama/blob/main/internal/client/ollama_client.go"
    click OpenAI_Client "https://github.com/dontizi/rlama/blob/main/internal/client/openai_client.go"

    %% Click Events for Utility & Shared Services
    click Vector_Store "https://github.com/dontizi/rlama/blob/main/pkg/vector/hnsw_vector_store.go"
    click Vector_Store_Utils "https://github.com/dontizi/rlama/blob/main/pkg/vector/store.go"
```
