<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo" -->

---
title: How to Build RAG From Scratch in 50 Lines of Python -...
description: This tutorial builds a complete retrieval augmented generation (RAG) pipeline from scratch in under 50 lines of Python, without LangChain or a vector database....
canonical: https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Build RAG From Scratch in 50 Lines of Python - Full Tutorial | daily.dev
og:description: This tutorial builds a complete retrieval augmented generation (RAG) pipeline from scratch in under 50 lines of Python, without LangChain or a vector database....
og:url: https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo
og:image: https://api.daily.dev/og/posts/WGiHgbIwo.png
og:image:alt: How to Build RAG From Scratch in 50 Lines of Python - Full Tutorial
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Build RAG From Scratch in 50 Lines of Python - Full Tutorial

**[Tech With Tim](https://daily.dev/sources/TechWithTim)** · 26 min read · 53 upvotes · 2 comments

## Summary

This tutorial builds a complete retrieval augmented generation (RAG) pipeline from scratch in under 50 lines of Python, without LangChain or a vector database. It runs locally using Ollama with the Nomic Embed Text embedding model and Qwen 3 4B Instruct as the chat model. A sample employee handbook is split into 11 paragraph-based chunks, each embedded into a 768-number vector. User questions are embedded the same way and compared to chunks using cosine similarity, pulling the top matches into the prompt before the LLM answers. The video is sponsored by Triple Ten, which the host promotes for programs aimed at experienced engineers moving into AI systems engineering.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=f9uAG3AQGWM>

## Questions this post answers

### How do I build a basic RAG system in Python without using LangChain or a vector database?

You can build RAG using five steps: run local models via Ollama, chunk documents by paragraph, embed chunks into vectors with an embedding model, search vectors with cosine similarity, then build a prompt with the retrieved chunks and send it to a chat model. Using Nomic Embed Text for embeddings and Qwen 3 4B Instruct for chat, this entire pipeline runs locally in under 50 lines of Python using only the requests and numpy packages.

_Developers piecing together their own RAG stack can find practical build patterns like this one on daily.dev._

### What embedding model and chat model work well for running RAG locally with Ollama?

Nomic Embed Text works for generating embeddings, producing vectors of 768 numbers, and is small enough at 274 megabytes to run locally. For the chat model, Qwen 3 4B Instruct, about 2.5 GB, handles reading the prompt and generating answers and runs fine on most normal laptops with a graphics card or modern Mac.

_Those choosing local LLM pairings for a RAG project can compare options surfaced on daily.dev._

### How should I chunk a document for a simple RAG pipeline?

Splitting a document by paragraph, using blank lines as separators, works well for simple cases, turning each paragraph into its own chunk. In one example handbook, this approach produced 10 paragraph chunks plus the title, totaling 11 chunks. Larger projects may instead chunk by a fixed number of characters or by headings for more control.

_Developers tuning chunking strategy for retrieval quality can track practical approaches via daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@ajaychoudhary80** · 3 upvotes

> I actually prefer tutorials like this. Build the fundamentals first, then reach for LangChain or a vector DB when the project needs them.

**@devdavidt** · 0 upvotes

> great for basic understanding these tools we are using daily

## Similar posts on daily.dev

- [Medium](https://daily.dev/posts/medium-ollye3mqw) · Medium · 1 upvotes · 0 comments
- [Building a RAG Pipeline with LLM Embeddings and Scikit-learn](https://daily.dev/posts/building-a-rag-pipeline-with-llm-embeddings-and-scikit-learn-74cfofiyr) · Machine Learning Mastery · 0 upvotes · 0 comments
- [Build an End-to-End RAG Pipeline for LLM Applications](https://daily.dev/posts/build-an-end-to-end-rag-pipeline-for-llm-applications-qvdk8tsyf) · DigitalOcean Community · 1 upvotes · 0 comments
- [RAG Explained Simply with a Real Project](https://daily.dev/posts/rag-explained-simply-with-a-real-project-pgy15jzg7) · freeCodeCamp · 23 upvotes · 1 comments
- [An Animated Guide to RAG: How AI Finds Answers in Your Documents](https://daily.dev/posts/an-animated-guide-to-rag-how-ai-finds-answers-in-your-documents-fdfvsddsl) · Machine Learning Pills · 81 upvotes · 1 comments

---

Tags: [#python](https://daily.dev/tags/python), [#nlp](https://daily.dev/tags/nlp), [#rag](https://daily.dev/tags/rag), [#ollama](https://daily.dev/tags/ollama), [#numpy](https://daily.dev/tags/numpy)

[View this post on daily.dev](https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Build RAG From Scratch in 50 Lines of Python - Full Tutorial","url":"https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo"},"datePublished":"2026-10-30T13:24:50.156Z","dateModified":"2026-10-30T13:30:31.257Z","description":"This tutorial builds a complete retrieval augmented generation (RAG) pipeline from scratch in under 50 lines of Python, without LangChain or a vector database....","image":"https://i.ytimg.com/vi/f9uAG3AQGWM/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/f9uAG3AQGWM/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Tech With Tim","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Tech With Tim","logo":"https://media.daily.dev/image/upload/s--6vVIDfU0--/f_auto/v1711726146/logos/TechWithTim","url":"https://daily.dev/sources/TechWithTim"},"commentCount":2,"discussionUrl":"https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":53},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"python,nlp,rag,ollama,numpy","timeRequired":"PT26M","video":{"@type":"VideoObject","name":"How to Build RAG From Scratch in 50 Lines of Python - Full Tutorial","description":"This tutorial builds a complete retrieval augmented generation (RAG) pipeline from scratch in under 50 lines of Python, without LangChain or a vector database....","thumbnailUrl":"https://i.ytimg.com/vi/f9uAG3AQGWM/sddefault.jpg","uploadDate":"2026-10-30T13:24:50.156Z","duration":"PT26M","url":"https://api.daily.dev/r/WGiHgbIwo","embedUrl":"https://www.youtube.com/embed/f9uAG3AQGWM"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Tech With Tim","item":"https://daily.dev/sources/TechWithTim"},{"@type":"ListItem","position":3,"name":"How to Build RAG From Scratch in 50 Lines of Python - Full Tutorial"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo","comment":[{"@type":"Comment","text":"I actually prefer tutorials like this. Build the fundamentals first, then reach for LangChain or a vector DB when the project needs them.","datePublished":"2026-11-02T05:25:22.050Z","url":"https://daily.dev/posts/WGiHgbIwo#c-4MDzkfYNa","author":{"@type":"Person","name":"Ajay Choudhary","url":"https://daily.dev/ajaychoudhary80","image":"https://media.daily.dev/image/upload/s--WkX8wDW2--/f_auto/v1780989706/avatars/avatar_QNZlI2SgVnNSx5PSRpCVx?_a=BAMAMiWQ0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3}},{"@type":"Comment","text":"great for basic understanding these tools we are using daily","datePublished":"2026-11-02T08:02:57.827Z","url":"https://daily.dev/posts/WGiHgbIwo#c-bFwu4tZSa","author":{"@type":"Person","name":"DavidDev","url":"https://daily.dev/devdavidt","image":"https://media.daily.dev/image/upload/s--jxmKsIov--/f_auto/v1788950401/avatars/avatar_HK0Qdu9ttIpcoDHx7zrHk?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-build-rag-from-scratch-in-50-lines-of-python---full-tutorial-wgihgbiwo#faq","mainEntity":[{"@type":"Question","name":"How do I build a basic RAG system in Python without using LangChain or a vector database?","acceptedAnswer":{"@type":"Answer","text":"You can build RAG using five steps: run local models via Ollama, chunk documents by paragraph, embed chunks into vectors with an embedding model, search vectors with cosine similarity, then build a prompt with the retrieved chunks and send it to a chat model. Using Nomic Embed Text for embeddings and Qwen 3 4B Instruct for chat, this entire pipeline runs locally in under 50 lines of Python using only the requests and numpy packages. Developers piecing together their own RAG stack can find practical build patterns like this one on daily.dev."}},{"@type":"Question","name":"What embedding model and chat model work well for running RAG locally with Ollama?","acceptedAnswer":{"@type":"Answer","text":"Nomic Embed Text works for generating embeddings, producing vectors of 768 numbers, and is small enough at 274 megabytes to run locally. For the chat model, Qwen 3 4B Instruct, about 2.5 GB, handles reading the prompt and generating answers and runs fine on most normal laptops with a graphics card or modern Mac. Those choosing local LLM pairings for a RAG project can compare options surfaced on daily.dev."}},{"@type":"Question","name":"How should I chunk a document for a simple RAG pipeline?","acceptedAnswer":{"@type":"Answer","text":"Splitting a document by paragraph, using blank lines as separators, works well for simple cases, turning each paragraph into its own chunk. In one example handbook, this approach produced 10 paragraph chunks plus the title, totaling 11 chunks. Larger projects may instead chunk by a fixed number of characters or by headings for more control. Developers tuning chunking strategy for retrieval quality can track practical approaches via daily.dev."}}]}
```

