DEV Community

RafterKode
RafterKode

Posted on

My first RAG project

I have been interested in LLMs that can be installed locally, with the goal of controlling the data provided and its propagation outside the private network. ⁠
Here is a presentation of the choices I made and how I implemented them.

LLM Installation

curl -fsSL https://ollama.com/install.sh | sh
Enter fullscreen mode Exit fullscreen mode
>>> Installing ollama to /usr/local
>>> Downloading ollama-linux-amd64.tar.zst
######################################################################## 100,0%
>>> Creating ollama user...
>>> Adding ollama user to render group...
>>> Adding ollama user to video group...
>>> Adding current user to ollama group...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
Created symlink /etc/systemd/system/default.target.wants/ollama.service → /etc/systemd/system/ollama.service.
>>> The Ollama API is now available at 127.0.0.1:11434.
>>> Install complete. Run "ollama" from the command line.
WARNING: No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode.
Enter fullscreen mode Exit fullscreen mode

My PC does not have an active GPU, so my LLM will run exclusively on the CPU.

Post-installation Verification

ollama --version
systemctl status ollama
Enter fullscreen mode Exit fullscreen mode
ollama version is 0.40.16

● ollama.service - Ollama Service
     Loaded: loaded (/etc/systemd/system/ollama.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2026-11-18 13:28:54 CET; 3min 58s ago
   Main PID: 4228 (ollama)
      Tasks: 13 (limit: 18737)
     Memory: 11.6M
        CPU: 559ms
     CGroup: /system.slice/ollama.service
             └─4228 /usr/local/bin/ollama serve
Enter fullscreen mode Exit fullscreen mode

Downloading the LLM Model for Response Generation

ollama pull qwen2.5:1.5b
Enter fullscreen mode Exit fullscreen mode
pulling manifest
pulling 183715c43589: 100% ▕██985 MB/986 MB   78 MB/s  0s
pulling 377ac4d7aeef: 100% ▕██ 487 B
verifying sha256 digest
writing manifest
success
Enter fullscreen mode Exit fullscreen mode

I chose Qwen2.5:1.5b, with a download size of approximately 986 MB. It is a lightweight model, and since I only need French and English, it is more than sufficient for my requirements.

RAM Configuration and Service Restart

sudo systemctl edit ollama
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

[Service]
Environment="OLLAMA_MAX_LOADED_MODELS=1"
Environment="OLLAMA_NUM_PARALLEL=1"
Environment="OLLAMA_CONTEXT_LENGTH=2048"
Environment="OLLAMA_KEEP_ALIVE=0"
Enter fullscreen mode Exit fullscreen mode

Then reload the systemd configuration and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart ollama
Enter fullscreen mode Exit fullscreen mode

We have now optimized our model so that only one model instance can be loaded at a time, with no parallel processing. The model will also be unloaded after use.

The context length is limited to a maximum of 2,048 tokens.

Project Structure

local-rag/
│
├── documents/
│   ├── document1.pdf
│   └── document2.pdf
│
├── database/
│
├── app/
│   ├── ingest.py
│   └── api.py
│
└── requirements.txt
Enter fullscreen mode Exit fullscreen mode

Installing Dependencies

sudo apt update
sudo apt install python3 python3-venv python3-pip
Enter fullscreen mode Exit fullscreen mode

Creating an Isolated Environment

mkdir local-rag
cd local-rag

python3 -m venv venv
source venv/bin/activate

mkdir documents database app
Enter fullscreen mode Exit fullscreen mode

requirements.txt Content

We do not pin dependency versions because this is a local environment, not a production environment.

fastapi
uvicorn
chromadb
pymupdf
requests
Enter fullscreen mode Exit fullscreen mode

Installing Python Dependencies

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Running Our Python Scripts

app/ingest.py

This script indexes the PDF files, splits their content into chunks, and stores them locally in a database to avoid having to reload and process them every time the application starts.

Indexing process:

PDF → Text → Chunks → Embeddings → ChromaDB

app/api.py

This file contains our API, which allows users to search for the requested information within the indexed documents.

After placing the PDF file in the documents directory, simply run the indexing script:

python app/ingest.py
Enter fullscreen mode Exit fullscreen mode

Starting the API and Making It Available to the User

uvicorn app.api:app --host 127.0.0.1 --port 8000
Enter fullscreen mode Exit fullscreen mode

The associated endpoint will then be available at:

http://127.0.0.1:8000/ask
Enter fullscreen mode Exit fullscreen mode

Testing Using a Browser or the Command Line

curl -X POST http://127.0.0.1:8000/ask \
  -H "Content-Type: application/json" \
  -d '{
    "question": "What are the key points of the document?"
  }'
Enter fullscreen mode Exit fullscreen mode

You will receive a JSON response containing:

  • The question.
  • The generated answer.
  • The source documents.
  • The page numbers.

🌐 rafterkode.github.io

Top comments (0)