explaingit

canwhite/microrag

Analysis updated 2026-05-18

112PythonAudience · researcherSetup · easy

TLDR

MicroRAG is a minimal retrieval-augmented generation system built entirely in plain Python from scratch, with no AI libraries, to teach how RAG works.

Mindmap

A visual breakdown will appear here once this repo is fully enriched.

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Learn how a retriever and generator work together in a RAG pipeline without relying on a framework.

USE CASE 2

Study a hand-written implementation of gradient math and contrastive learning in plain Python.

USE CASE 3

Run the included training and inference pipeline to see retrieval and generation work end to end.

What is it built with?

Python

How does it compare?

canwhite/microragexploitbench/exploitbenchsupperzl/ita
Stars112112112
LanguagePythonPythonPython
Setup difficultyeasyhardmoderate
Complexity4/5
Audienceresearcherresearcherops devops

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

Runs with only Python's standard library, no external ML dependencies.

In plain English

MicroRAG is a minimal working example of a Retrieval-Augmented Generation system, written entirely in plain Python without any AI or machine learning libraries. RAG is an approach where an AI first searches a knowledge base to find relevant information, then uses that information to generate an answer. This project builds both halves from scratch to show how the technique works at a low level. The retriever half converts questions and stored documents into 32-dimensional numerical vectors using a small embedding matrix, then finds the closest match by cosine similarity. It is trained using a technique called contrastive learning, where matching question-document pairs are pushed together and non-matching pairs are pushed apart. The generator half is a single-layer self-attention transformer that takes a question combined with the retrieved document text and produces a character-by-character response. All the gradient mathematics that normally come from a library like PyTorch are implemented by hand using only Python's standard library. The project includes a complete training and inference pipeline that you can run with a single command. In the example output shown in the README, the system correctly retrieves a relevant document about the sun and then generates an answer stating the surface temperature and core temperature. The README explicitly describes the scope limitations: the knowledge base contains only 8 fixed question-answer pairs, the tokenizer works at the character level with a vocabulary of around 60 symbols, the transformer has just one layer, and the maximum text length is 64 characters before position encodings wrap. This is described as a learning and demonstration project, not a production system. It is suited for someone who wants to understand how retrieval and generation fit together without the abstraction of a full-sized framework.

Copy-paste prompts

Prompt 1
Walk me through how MicroRAG's retriever converts questions and documents into vectors using cosine similarity.
Prompt 2
Explain how contrastive learning trains MicroRAG's embedding matrix from scratch.
Prompt 3
Show me how MicroRAG's single-layer self-attention transformer generates a character-by-character answer.
Prompt 4
What would I need to change in MicroRAG to scale beyond its 8 fixed question-answer pairs?

Frequently asked questions

What is microrag?

MicroRAG is a minimal retrieval-augmented generation system built entirely in plain Python from scratch, with no AI libraries, to teach how RAG works.

What language is microrag written in?

Mainly Python. The stack also includes Python.

How hard is microrag to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is microrag for?

Mainly researcher.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.