A notebook-based Self-RAG pipeline over internal company PDFs, with retrieval, relevance filtering, support verification, and usefulness checks.
- Loads internal PDFs from
./documents/ - Splits and embeds text chunks into a FAISS vector store
- Routes each question through a LangGraph workflow:
- Decide whether retrieval is needed
- Retrieve + relevance filter
- Generate answer from context
- Verify grounding (
IsSUP) - Revise answer if not fully supported
- Check usefulness (
IsUSE) - Rewrite retrieval query and retry if needed
Main implementation: self_rag_step7.ipynb
├── self_rag.ipynb
├── SRAG.png
└── documents/
├── Company_Policies.pdf
├── Company_Profile.pdf
└── Product_and_Pricing.pdf
- Python 3.10+
- OpenAI API key
Install dependencies:
pip install -U \
jupyter \
python-dotenv \
pydantic \
langgraph \
langchain-core \
langchain-community \
langchain-openai \
langchain-text-splitters \
faiss-cpu \
pypdfCreate a .env file in the project root:
OPENAI_API_KEY=your_openai_api_keyjupyter notebook self_rag_step7.ipynbThen run cells top-to-bottom.
The notebook builds a StateGraph with these key nodes:
decide_retrievalretrieveis_relevantgenerate_from_contextis_sup->revise_answerloopis_use->rewrite_questionretry loopno_answer_found
This design reduces hallucination by forcing answer support checks before final output.
- Retrieval source files are currently the PDFs in
./documents/. - Default embedding model:
text-embedding-3-large - Default chat model:
gpt-4o-mini - Tune
MAX_RETRIESandMAX_REWRITE_TRIESin the notebook for stricter/looser behavior.
