# hacktronaut/ollama-rag-demo architecture

> Architecture diagram of the GitHub repository hacktronaut/ollama-rag-demo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hacktronaut/ollama-rag-demo
- Repository: https://github.com/hacktronaut/ollama-rag-demo

## Overview

##### Project type and purpose
- This is a Node.js‐based command‐line Retrieval Augmented Generation (RAG) demo.
- It uses langchain.js to orchestrate retrieval + LLM calls, Ollama as the local LLM provider, and ChromaDB as the vector store.
- Purpose: ingest a corpus (utils/data.txt), index it (loadData.js), then answer user questions with context from the corpus (index.js).

##### File structure
- Top level:
- package.json / package-lock.json – Node dependencies (langchain, ollama, chroma)
- index.js – entry point for QA pipeline
- utils/
– data.txt – raw documents to index
– loadData.js – script to load documents, compute embeddings, and push to ChromaDB
- No frontend or multiple microservices—just a simple backend pipeline with two scripts.
- Configuration is embedded in code (baseUrl, model, collectionName, url).

##### README for additional insights
- Confirms the two‐step process:
a) run loadData.js to index documents
b) run index.js to start QA
- Shows how to configure OllamaEmbeddings and Ollama LLM instances, and how to connect to ChromaDB.
- Highlights dynamic prompt templates and RAG-powered QA flow.

a. Main components
1. Data Source (utils/data.txt)
2. Embedding Service (OllamaEmbeddings via ollama‐inference API)
3. Vector Store (ChromaDB instance)
4. QA Application (index.js running langchain.js chains)
5. LLM Service (Ollama LLM model via ollama‐inference API)
6. User (CLI)
b. Relationships & data flow
1. loadData.js reads data.txt → sends each doc to Embedding Service → receives embeddings → writes to Vector Store
2. index.js receives user question → Optionally uses prompt template → Vector Store searches with embeddings to retrieve top‐k docs → constructs augmented prompt → sends to LLM Service → returns answer → prints to user
c. Architectural patterns & principles
- Retrieval-Augmented Generation pipeline: separation of indexing vs. query-time retrieval
- Modular – data ingestion (utility script) is decoupled from QA runtime
- Single responsibility: index.js orchestrates only query-time flow; loadData.js handles ingestion
d. Key technologies
- Node.js (runtime)
- langchain.js (pipeline orchestration)
- OllamaEmbeddings & Ollama LLM (local LLM via HTTP)
- ChromaDB (vector store for similarity search)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Ingestion Layer
    subgraph "Ingestion Layer"
        DS["utils/data.txt"]:::data
        LD["loadData.js"]:::internal
        ES["Ollama Embeddings Service\n(http://<ollama-host>:11434/embed)"]:::external
        VS["ChromaDB Vector Store\n(http://localhost:8000"]:::external
    end

    %% Query Layer
    subgraph "Query Layer"
        USR["User (CLI)"]:::external
        QA["index.js"]:::internal
        LLM["Ollama LLM Service\n(http://<ollama-host>:11434/generate)"]:::external
    end

    %% Project Configuration
    subgraph "Project Configuration"
        PKG["package.json"]:::data
        LOCK["package-lock.json"]:::data
        RD["README.md"]:::data
    end

    %% Data Flow - Ingestion
    DS -->|"reads data"| LD
    LD -->|"POST /embed"| ES
    ES -->|"embeddings"| LD
    LD -->|"writes vector data"| VS

    %% Data Flow - Query
    USR -->|"user question"| QA
    QA -->|"similarity search"| VS
    VS -->|"top-k documents"| QA
    QA -->|"POST /generate"| LLM
    LLM -->|"generated answer"| QA
    QA -->|"displays answer"| USR

    %% Click Events
    click DS "https://github.com/hacktronaut/ollama-rag-demo/blob/main/utils/data.txt"
    click LD "https://github.com/hacktronaut/ollama-rag-demo/blob/main/utils/loadData.js"
    click QA "https://github.com/hacktronaut/ollama-rag-demo/blob/main/index.js"
    click PKG "https://github.com/hacktronaut/ollama-rag-demo/blob/main/package.json"
    click LOCK "https://github.com/hacktronaut/ollama-rag-demo/blob/main/package-lock.json"
    click RD "https://github.com/hacktronaut/ollama-rag-demo/blob/main/README.md"

    %% Styles
    classDef internal fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef external fill:#D5F5E3,stroke:#145A32,stroke-width:2px;
    classDef data fill:#FCF3CF,stroke:#7D6608,stroke-width:2px;
```
