~/projects/self-rag.md

Self-RAG

P / 02RAG

RAG that decides whether to retrieve, grades what it finds, checks its own answer, and retries when it is weak.

graph nodes
11
reflection checks
4
revision rounds and rewrites
≤ 2

Repository (opens in a new tab)All projects

How it works

relevantnone relevantnot groundedselect bestusefulnot useful decide retrievalretrieve? yes / noanswer directlyskips retrievalretrievetop-4 chunksISRELrelevant?generateper relevant chunkISSUPgrounded?revise≤ 2 roundsISUSEuseful?rewrite≤ 2 retrieslimited contexthonest fallbackANSWERROUTE · DIRECT ANSWERROUTE · RETRIEVE · CHECK · ANSWERROUTE · REVISE LOOPROUTE · REWRITE LOOPROUTE · GIVE UP
How Self-RAG works. Routes: direct answer: decide retrieval to answer directly to ISUSE to ANSWER. retrieve · check · answer: decide retrieval to retrieve to ISREL to generate to ISSUP to ISUSE to ANSWER. revise loop: decide retrieval to retrieve to ISREL to generate to ISSUP to revise to ISSUP to ISUSE to ANSWER. rewrite loop: decide retrieval to retrieve to ISREL to rewrite to retrieve to ISREL to generate to ISSUP to ISUSE to ANSWER. give up: decide retrieval to retrieve to ISREL to rewrite to retrieve to ISREL to rewrite to retrieve to ISREL to limited context to ANSWER.
  • direct answer: decide retrieval → answer directly → ISUSE → ANSWER
  • retrieve · check · answer: decide retrieval → retrieve → ISREL → generate → ISSUP → ISUSE → ANSWER
  • revise loop: decide retrieval → retrieve → ISREL → generate → ISSUP → revise → ISSUP → ISUSE → ANSWER
  • rewrite loop: decide retrieval → retrieve → ISREL → rewrite → retrieve → ISREL → generate → ISSUP → ISUSE → ANSWER
  • give up: decide retrieval → retrieve → ISREL → rewrite → retrieve → ISREL → rewrite → retrieve → ISREL → limited context → ANSWER

What it does

  • Four reflection checks, each a Pydantic schema through Groq structured output: retrieve, ISREL (relevance), ISSUP (support) and ISUSE (usefulness).
  • Bounded loops: at most 2 revisions and 2 rewrites, so the graph always terminates.
  • Honest fallback: returns the best answer found with a "limited context" note, or says it could not find it in the documents.
  • Each call tries the primary Groq model first, then a fallback model.

Stack

  • LangGraph
  • LangChain
  • Groq
  • Ollama
  • ChromaDB
  • Pydantic
  • Streamlit