Skip to content

Build a RAG API with FastAPI

Category Local LLMs (Ollama)
Status โœ… Completed
Repo nextwork-build-a-rag-api-with-fastapi
Original NextWork guide nextwork.ai
Proof Official NextWork write-up: web ยท PDF

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Build a RAG API with FastAPI guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ Local LLMs (Ollama)
Status: โœ… Completed

Proof: Official NextWork write-up: web ยท PDF

Overview

Retrieval-Augmented Generation answers questions about your documents with a model that was never trained on them. Profile text is chunked by paragraph and embedded with nomic-embed-text, and the vectors are stored in ChromaDB. On each /ask, the API retrieves the 2 most similar chunks, puts them into the prompt, and has qwen2.5:0.5b generate the answer, entirely offline. For the secret mission, anyone can POST a profile and query it in isolation (a metadata filter per user).

Summary

I built and ran the full local RAG pipeline: profile โ†’ chunks โ†’ nomic-embed-text embeddings in ChromaDB โ†’ FastAPI /ask โ†’ qwen2.5:0.5b on Ollama. I also built the secret mission's multi-user directory. The integration tests pass against the real local stack.

The checks behind this are in docs/verification.txt.

Skills & tools

RAG, Vector databases, ChromaDB, Embeddings, Ollama, FastAPI, Pydantic, Semantic search, pytest, Python

What I built

  • profile.txt: a fictional sample profile (no personal data committed).
  • embed.py: chunking and ChromaDB PersistentClient upserts with an Ollama embedding function, plus a test retrieval.
  • app.py: FastAPI with GET /ask (retrieve โ†’ augment โ†’ generate, temperature 0, 'say you don't know' grounding), plus POST /profiles, GET /users, and GET /users/{user}/ask for the secret mission.
  • test_app.py: integration tests on the live stack (grounded answers, right chunk retrieved, no cross-user leakage, input validation).

How to run

ollama pull qwen2.5:0.5b && ollama pull nomic-embed-text
python3 -m venv .venv && . .venv/bin/activate && pip install -r requirements.txt
python embed.py
uvicorn app:app --reload        # then open http://127.0.0.1:8000/docs
curl "http://127.0.0.1:8000/ask?question=What%20is%20my%20career%20goal%3F"
pytest -q -s test_app.py

Verification

I ran the verification command on 2026-12-02. The full output is in docs/verification.txt.

PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python embed.py && PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -s -p no:cacheprovider test_app.py; rc=$?; rm -rf chroma_db; exit $rc

Notes

  • Small models can still ignore context sometimes. Temperature 0 plus an explicit 'only use the context' instruction made answers reliable in testing.
  • Each user's data is isolated with a ChromaDB where={'user': ...} filter. In production you'd add auth on top.

Original project: https://nextwork.ai/projects/99ec4595-3d7b-5584-a799-aafb9c373bbb ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python embed.py && PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -s -p no:cacheprovider test_app.py; rc=$?; rm -rf chroma_db; exit $rc

Loaded 4 chunks from profile.txt
Added 4 chunks to the 'personal_profile' collection.
Test retrieval: For fun, I enjoy hiking, playing guitar, and brewing coffee.
{'question': 'What is my career goal?', 'answer': 'Based on the context provided, your career goal is to become an AI solutions engineer.', 'context_used': ["My career goal is to become an AI solutions engineer.\nI'm especially interested in automation, infrastructure as code, and multi-agent systems.", "My name is Alex Rivera (a sample profile; replace it with your own details locally and don't commit them).\nI'm currently learning about cloud computing, AI, and DevOps."]}
.{'question': 'What do I enjoy doing for fun?', 'answer': 'Based on the context provided, you enjoy hiking, playing guitar, and brewing coffee for fun.', 'context_used': ['For fun, I enjoy hiking, playing guitar, and brewing coffee.', "My name is Alex Rivera (a sample profile; replace it with your own details locally and don't commit them).\nI'm currently learning about cloud computing, AI, and DevOps."]}
.{'question': 'What does Sam study?', 'answer': 'Sam studies coral reefs.', 'context_used': ['Sam is a marine biologist who studies coral reefs.', "Sam's favourite food is ramen."]}
..
4 passed in 3.39s

[exit code 0]