Build a RAG Agent with LangChain – A Practical Lab for Retrieval‑Augmented Generation
Run `python rag_agent.py ingest` to pull your DEV.to posts, then `python rag_agent.py chat` to test the three RAG strategies; enable local embeddings with `ollama pull nomic-embed-text` to keep indexing local.
Set up a .env file with DEVTO_USERNAME, optionally LANGSMITH_API_KEY and GOOGLE_API_KEY, then run `python rag_agent.py ingest` to index your posts, and benchmark with `python rag_agent.py bench`.
Summary
The article presents a complete RAG agent built with LangChain, designed to answer questions over a user’s own DEV.to posts. It pulls posts via the DEV.to API, chunks them with markdown‑aware splitting that preserves heading metadata, and stores the vectors in a persistent Chroma database. Embeddings are generated locally with Ollama’s nomic‑embed‑text model to avoid quota limits, and the system supports both local and Google Gemini embeddings if desired. The repo includes a CLI with commands ingest, chat, and bench, and uses LangSmith for tracing and evaluation of each step.
Three retrieval strategies are benchmarked: a tool‑calling agent that decides when to retrieve, an always‑retrieve chain that fetches a passage on every turn, and a corrective RAG (CRAG) that grades the retrieved context and retries if necessary. The project also implements content‑hash dedup to skip unchanged articles during re‑ingest, persistent threads via SqliteSaver to survive restarts, and a structured repo layout with strategies/agent.py, chain.py, crag.py. Developers can run `python rag_agent.py ingest` to index their posts, then `python rag_agent.py chat` to interact with the agent, and `python rag_agent.py bench` to benchmark the strategies. The repository is hosted on GitHub and includes a detailed README, requirements.txt, and an .env.example for configuration.
Key changes
- Introduced a RAG strategy lab with tool‑calling agent, always‑retrieve chain, and corrective RAG
- Implemented markdown‑aware chunking that splits on headings and preserves metadata
- Added content‑hash dedup to skip unchanged articles during re‑ingest
- Integrated LangSmith tracing and Evaluate for ingest, chat, and benchmark runs
- Enabled persistent thread support via SqliteSaver to survive restarts
- Provided local embedding support using Ollama’s nomic‑embed‑text to avoid quota limits
- Added a CLI with commands ingest, chat, and bench
- Structured repo layout with strategies/agent.py, chain.py, crag.py