Build Your Own RAG App in 55 Lines of Python. No LangChain, No Cloud, No Bill

One file, two models, and a chatbot that answers from your documents instead of its training data.
What you end up with
- A file called rag.py, about 55 lines of Python, that answers questions about your own documents
- Everything runs on your machine through Ollama: no LangChain, no external vector database, no paid API
- The demo document is a fictional coffee shop employee handbook: opening hours, Wi-Fi, pet policy, staff rules
- The pipeline has three moves: retrieve the relevant paragraphs, add them to the prompt, generate the answer
- The model is never retrained. It gets better context, and that is the whole trick
The stack is five pieces, none of them a framework
- Python, plus Requests to talk to Ollama and NumPy for the embedding maths
- Ollama running locally, with two models pulled down before you start
- nomic-embed-text for embeddings, 768-dimensional vectors
- qwen3:4b-instruct, a 4 billion parameter chat model, for the answers
- uv for project and dependency management
Pull the models first:
ollama pull nomic-embed-textollama pull qwen3:4b-instructCheck they landed:
ollama listThen set up the project:
uv inituv add requests numpyPoint the chat at the right model:
export OLLAMA_CHAT_MODEL=qwen3:4b-instructTwo helper functions do all the talking
- One embedding function sends text to Ollama's embedding endpoint and returns the vectors as a NumPy array
- One chat function sends a prompt to the chat model with streaming disabled, so the complete response comes back in one go
- Both talk to the Ollama server on its default port, 11434
- A quick test question checks the chat connection before anything else runs, so a dead server fails fast instead of failing halfway through retrieval
Chunking is where most RAG builds quietly fail
- Documents live as Markdown files in a docs/ folder, and the code finds every one of them
- The splitter cuts on blank lines, so each paragraph becomes one chunk, and empty sections are dropped
- Every chunk is embedded with nomic-embed-text, and each vector keeps the same position as its source paragraph
- That position is what lets the code recover the original text later. The vector finds the match, the index finds the words
- Paragraphs about similar things end up with similar vectors. A paragraph about dogs and a question about bringing pets into a cafe sit close together, even though they share almost no words
Retrieval is just cosine similarity
- A retrieval function takes a question and a k, starting at 3, and embeds the question with the same model
- It compares the question vector against every document vector with cosine similarity through NumPy
- A score closer to 1 means more similar. The function sorts, takes the top k, and returns the original text of those chunks
- The test question is "are dogs allowed inside the cafe?" It finds the pet policy, which proves the match is semantic, not keyword based
- Nothing here is a database. It is a list of vectors and a sort, which is exactly why the whole app fits in 55 lines
The prompt is the guardrail
- The retrieved paragraphs are combined into one context block, then built into a prompt with three parts: instructions, the retrieved context, and the user's question
- The instructions tell the model to answer using the provided information, and to say so when the answer is not in it
- That second half is the part people skip, and it is the only thing standing between a retrieval app and a confident hallucination
- The finished prompt goes to the chat function, and the answer comes back from the local model
- Run it with:
uv run rag.pyTest it with a question it cannot answer
- The demo runs three questions: when the coffee shop opens, whether pets are allowed inside, and one the handbook does not answer at all
- The third question is the test that matters. A RAG app that invents an answer to a question its documents cannot answer is worse than a search box
- Test with your own unanswerable questions before you trust it with real ones, and watch what it does when the context comes back empty
- If it answers confidently with no retrieved text behind it, the prompt needs work, not the model
Before you point it at real documents
- Embeddings are regenerated on every start. Fine for a handbook, slow for a real corpus, so persistent storage is the first upgrade
- Paragraph splitting breaks on tables, code, and complex formatting. A document with structure needs a chunking strategy that respects it
- There is no reranking step, so the top 3 chunks are whatever cosine similarity says, with no second pass to check them
- There is no evaluation set, so you cannot tell whether a change made retrieval better or worse
- A 4 billion parameter local model is capable, but it is not a frontier model
Our verdict
- The design is the right shape: small code, local models, no framework, and a prompt that admits ignorance
- The weak points are all at the edges: no persistent storage, naive chunking, no reranker, no evaluation
- It fits a personal knowledge base, a team handbook, or a prototype you can show in an afternoon
- It does not fit a production support bot until the embeddings are stored, the chunking handles real documents, and someone has built an eval set
Build it in an afternoon, run it against your own documents for a week, and only then decide whether it deserves a database.
Source: Build RAG from Scratch in Python (Local AI, No LangChain)
Get Daily AI Trends
Weekly breakdowns of autonomous agents and live benchmarks.
Blockframe Labs Content Team
The content team at BlockFrame Labs writes about AI systems and services we actually ship: automation pipelines, agent infrastructure, and the web engineering behind them. Every guide comes from a system running in production.
Work with us
This blog runs itself. Our Blog OS publishes daily from Notion with zero manual edits, and we build the same system for clients.