📚 RAG in ~10 Lines — No Vector Database, No pip install
Semantic search and retrieval-augmented generation without a vector DB, a framework, or a single dependency.
> Part of the *Pipe in 30 Lines* series: Self-healing code · Parallel LLM calls · Your first MCP server
RAG usually means a stack: a vector database, an embedding SDK, a retrieval library, and glue code to hold it together. Pipe collapses that into language primitives. embed_batch vectorizes your documents, nearest finds the top matches by *meaning*, and ask answers with the retrieved context — all in one binary, zero imports.
ai_provider "deepseek"
docs: ["Pipe is a pipeline-native language where data flows top to bottom.", "The bytecode VM runs compiled programs about 7x faster than the tree-walker.", "Built-in MCP lets Pipe expose its own tools or consume any stdio MCP server.", "Sandbox profiles restrict exec, write_file, and http_get in one declarative block.", "One ~7 MB binary, zero dependencies, on Linux, macOS, Windows, or the browser."]
vectors: embed_batch docs -- 1. vectorize once
question: "What makes the VM fast?"
q_vec: embed question -- 2. embed the query
top: nearest q_vec vectors 2 -- 3. top-k by similarity
context: ""
for idx in top
context: context ++ (at docs idx) ++ "\n---\n"
ask ("Context:\n" ++ context ++ "\nQuestion: " ++ question)
> print
What happens here:
embed_batchturns every document into a vector in one call — no vector DB to install, no index to configure.nearestreturns the *indices* of the most relevant documents by cosine similarity. Meaning, not keywords.- The top matches are stitched into a context block, and
askanswers grounded in it — classic RAG.
In Python + LangChain the same pipeline is roughly 80 lines across SDKs. In Pipe it's a dozen, and it works with every provider: swap "deepseek" for "openai", "anthropic", or "ollama" and nothing else changes.
Want the full version with a web UI, SQLite persistence, and a /search API? See examples/rag_knowledge_base.pipe — or run the minimal version above with export DEEPSEEK_API_KEY=... && pipe examples/blog_rag.pipe.
📚 RAG in ~10 Zeilen — ohne Vektor-DB, ohne pip install
Semantische Suche und RAG ohne Vektor-DB, ohne Framework und ohne eine einzige Abhängigkeit.
> Teil der Serie *Pipe in 30 Lines*: Selbstheilender Code · Parallele LLM-Calls · Dein erster MCP-Server
RAG bedeutet sonst einen ganzen Stack: eine Vektor-DB, ein Embedding-SDK, eine Retrieval-Bibliothek und Kleber-Code. Pipe reduziert das auf Sprach-Primitives. embed_batch vektorisiert deine Dokumente, nearest findet die besten Treffer *nach Bedeutung*, und ask antwortet mit dem gelieferten Kontext — alles in einer Binary, null Imports.
ai_provider "deepseek"
docs: ["Pipe ist eine pipeline-native Sprache, in der Daten von oben nach unten fließen.", "Die Bytecode-VM führt Programme etwa 7x schneller aus als der Tree-Walker.", "Eingebautes MCP: Pipe stellt eigene Tools bereit oder nutzt jeden stdio-MCP-Server.", "Sandbox-Profile sperren exec, write_file und http_get in einem deklarativen Block.", "Eine ~7-MB-Binary, null Abhängigkeiten, auf Linux, macOS, Windows oder im Browser."]
vectors: embed_batch docs -- 1. einmal vektorisieren
question: "Was macht die VM schnell?"
q_vec: embed question -- 2. Query einbetten
top: nearest q_vec vectors 2 -- 3. Top-K nach Ähnlichkeit
context: ""
for idx in top
context: context ++ (at docs idx) ++ "\n---\n"
ask ("Context:\n" ++ context ++ "\nQuestion: " ++ question)
> print
Was hier passiert:
embed_batchverwandelt jedes Dokument in einen Vektor — keinpip install, keine Vektor-DB, keine Index-Konfiguration.nearestliefert die *Indizes* der relevantesten Dokumente per Kosinus-Ähnlichkeit. Bedeutung statt Keywords.- Die Treffer werden zu einem Kontext-Block verbunden und
askantwortet darauf — klassisches RAG.
In Python + LangChain ist dieselbe Pipeline ~80 Zeilen über mehrere SDKs. In Pipe sind es ein Dutzend — und es funktioniert mit jedem Provider: Tausche "deepseek" gegen "openai", "anthropic" oder "ollama", sonst ändert sich nichts.
Die Vollversion mit Web-UI, SQLite-Persistenz und /search-API? Siehe examples/rag_knowledge_base.pipe — oder starte die Minimalversion oben mit export DEEPSEEK_API_KEY=... && pipe examples/blog_rag.pipe.