# packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python architecture

> Architecture diagram of the GitHub repository packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python
- Repository: https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python

## Overview

##### Project type and purpose
- This is a Python-based set of RAG (Retrieval-Augmented Generation) example scripts focused on “Harnessing Ollama” to build secure, local LLM solutions.
- There is no separate frontend/backend in the traditional sense; instead, each script (CLI, Streamlit, voice-enabled) is a different “UI” into the same core pipeline.

##### File structure
- Top-level scripts:
- start-1.py, start-2.py – minimal RAG prototypes
- pdf-rag.py, pdf-rag-clean.py – PDF ingestion + QA
- pdf-rag-streamlit.py – adds a Streamlit UI
- final-rag-voice.py – adds voice in/out
- function-calling.py – demonstrates LLM function calling
- categorizer.py – document categorization helper
- data/ – sample docs (PDF, text)
- commands_and_scripts.md – usage recipes
- requirements.txt – dependencies (Ollama Python client, PyPDF, Streamlit, TTS/STT libs)

##### Key insights from README
- Purpose: show modular, local LLM pipelines using Ollama
- Use cases: build chatbots, Q&A from docs, voice assistants, function-enabled agents
- Community-focused: examples are meant to be extended

##### Main components
a. Data Layer
- Source files (data/BOI.pdf, grocery_list.txt)
- Loader/cleaner (pdf-rag-clean.py)
b. Preprocessing & Retrieval Layer
- Text extraction (PyPDF2/pdfminer)
- Tokenization & embeddings (Ollama embeddings API)
- Vector store (in-memory or FAISS, if the user adds it)
- Categorizer module (categorizer.py)
c. LLM Interface Layer
- Ollama local LLM server (via Python client)
- Function-calling logic (function-calling.py)
d. Application (UI) Layer
- CLI scripts (start-*.py, pdf-rag.py)
- Web UI (pdf-rag-streamlit.py + Streamlit)
- Voice UI (final-rag-voice.py + STT/TTS libs)
e. Orchestration & Configuration
- requirements.txt, Modelfile (specifies which local model to spin up)
- commands_and_scripts.md – run/playbook

##### Relationships & data flow
1. User invokes a script (CLI, Streamlit, or voice)
2. Script calls Data Loader → extracts raw text
3. Preprocessor cleans and optionally categorizes text
4. Embedding component (Ollama embeddings API) builds or queries vector store
5. Retriever returns top-k passages
6. LLM call (via Ollama) with prompt + retrieved context (or function definitions)
7. LLM returns answer → script formats output (text, JSON, voice) → user

##### Architectural patterns & design principles
- Modular pipeline: separate loader, cleaner, retriever, LLM client, UI
- Single-responsibility scripts: each file demonstrates one integration point
- Config-driven model selection (Modelfile)

##### What
- Clear labels: “Data Loader,” “Preprocessor,” “Retriever,” “Ollama LLM API,” “CLI,” “Streamlit UI,” “Voice UI,” “Vector Store (in-mem or FAISS)”
- Data → Preprocessor → Retriever → LLM → UI → User
- UI → Orchestration/Config (modelfile, requirements)
- Data components (blue rectangles)
- Processing components (green rectangles)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "Application (UI) Layer"
        CLI1["start-1.py"]:::ui
        CLI2["start-2.py"]:::ui
        PDFCLI["pdf-rag.py"]:::ui
        Streamlit["pdf-rag-streamlit.py"]:::ui
        Voice["final-rag-voice.py"]:::ui
    end

    %% Core Pipeline
    subgraph "Core Pipeline"
        Loader["Data Loader\n(pdf-rag-clean.py)"]:::process
        Preprocessor["Preprocessor"]:::process
        Categorizer["Categorizer\n(categorizer.py)"]:::process
        Retriever["Retriever\n(Vector Store)"]:::process
        LLM["Ollama LLM API"]:::external
        FunctionCall["Function Calling\n(function-calling.py)"]:::process
    end

    %% Data Layer
    subgraph "Data Layer"
        BOI["BOI.pdf"]:::data
        Grocery["grocery_list.txt"]:::data
    end

    %% Orchestration & Config
    subgraph "Orchestration & Configuration"
        Requirements["requirements.txt"]:::config
        Modelfile["Modelfile"]:::config
        Commands["commands_and_scripts.md"]:::config
    end

    %% Data Flow Connections
    BOI -->|source| Loader
    Grocery -->|source| Loader
    CLI1 -->|invokes| Loader
    CLI2 -->|invokes| Loader
    PDFCLI -->|invokes| Loader
    Streamlit -->|invokes| Loader
    Voice -->|invokes| Loader

    Loader -->|extract| Preprocessor
    Preprocessor -->|clean| Retriever
    Preprocessor -->|optional categorize| Categorizer
    Categorizer -->|categorized data| Retriever
    Retriever -->|retrieve top-k| LLM
    FunctionCall -->|function-call| LLM

    %% UI to Config
    CLI1 -->|uses config| Modelfile
    CLI2 -->|uses config| Modelfile
    PDFCLI -->|uses config| Modelfile
    Streamlit -->|uses config| Modelfile
    Voice -->|uses config| Modelfile

    CLI1 -->|reads| Requirements
    CLI2 -->|reads| Requirements
    PDFCLI -->|reads| Requirements
    Streamlit -->|reads| Requirements
    Voice -->|reads| Requirements

    %% Click Events
    click BOI "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/data/BOI.pdf"
    click Grocery "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/data/grocery_list.txt"
    click Loader "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/pdf-rag-clean.py"
    click Categorizer "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/categorizer.py"
    click FunctionCall "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/function-calling.py"
    click CLI1 "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/start-1.py"
    click CLI2 "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/start-2.py"
    click PDFCLI "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/pdf-rag.py"
    click Streamlit "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/pdf-rag-streamlit.py"
    click Voice "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/final-rag-voice.py"
    click Requirements "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/requirements.txt"
    click Modelfile "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/tree/main/Modelfile"
    click Commands "https://github.com/packtpublishing/harnessing-ollama---create-secure-local-llm-solutions-with-python/blob/main/commands_and_scripts.md"

    %% Styles
    classDef data fill:#D0E6FF,stroke:#000,color:#000
    classDef process fill:#D4FFD4,stroke:#000,color:#000
    classDef ui fill:#FFF3C4,stroke:#000,color:#000,stroke-dasharray: 5 5
    classDef external fill:#FFD8B4,stroke:#000,color:#000
    classDef config fill:#E0D4FF,stroke:#000,color:#000
```
