Omthakur1394/self-aware-rag
0
Self-Aware-Rag𦾠: Production-Grade Agentic Research Assistant
self-aware-rag is a highly scalable, layout-aware Retrieval-Augmented Generation (RAG) system built to process and query dense academic research. It leverages an agentic architecture with built-in self-reflection to accurately retrieve information from complex documents β including tables, mathematical formulas, and appendices.
π Key Features
- Massive Scale Processing β Successfully ingested a corpus of 249 dense AI research papers (including cutting-edge 2025 releases like DeepSeek-R1), resulting in a high-density index of 42,385 vectors.
- Layout-Aware Parsing β Utilizes
Doclingto intelligently parse PDFs. Unlike standard parsers, it preserves the semantic structure of data tables, equations, and appendices for highly accurate retrieval. - Agentic Self-Reflection Loop β Powered by
LangGraphand the Groq API, the system uses a stateful agent that critiques its own answers, rewrites search queries, and retries β all autonomously. - Multi-Source Retrieval β Queries Pinecone (local vector store), Wikipedia, and arXiv simultaneously to maximize answer coverage.
- Cloud-Native Vector Storage β Embeddings generated via HuggingFace (
google/embeddinggemma-300m) and stored in a serverless Pinecone vector database for millisecond-latency semantic search. - Production-Ready Backend β Decoupled, asynchronous FastAPI backend with CORS support, fully containerized with Docker.
π§ How the Reflection Loop Works
User Query
β
βΌ
βββββββββββββββ
β Retriever β βββββββββββββββββββββββββββββββ
ββββββββ¬βββββββ β
β (Pinecone + Wikipedia + arXiv) β
βΌ β
βββββββββββββββ β
β Responder β β
ββββββββ¬βββββββ β
β (Cited answer via Groq LLM) β
βΌ β
βββββββββββββββ β
β Reflector β β
ββββββββ¬βββββββ β
β β
βββββ΄βββββ β
β β β
YES NO (attempts < 2) β
β β β
βΌ βΌ β
ββββββββ βββββββββββ β
β Done β βRewriter ββββββββββββββββββββββββββββ
ββββββββ βββββββββββ (refined search query)- Retrieve β Pulls documents from Pinecone, Wikipedia, and arXiv.
- Generate β Produces a cited answer with mandatory inline citations (
[0],[1], ...). - Reflect β The LLM evaluates whether the answer is complete, accurate, and properly cited.
- Branch:
YESorattempts >= 2β answer is finalized and returned.NOβ query is rewritten based on the reflection feedback and the loop retries.
π οΈ Technology Stack
π Project Structure
.
βββ src/
β βββ agent.py # RAG state, retrieval, generation, reflection, rewriting
β βββ graph.py # LangGraph state machine definition
β βββ vector_store.py # Pinecone vector store + HuggingFace embeddings
β βββ cofig.py # LLM (Groq) configuration
β βββ cli.py # Interactive CLI entrypoint
βββ main.py # FastAPI server
βββ requirements.txt
βββ Dockerfile
βββ .env # API keys (not committed)
βββ README.mdβοΈ Quick Start (Local Development)
1. Set up the environment
cd bionic-rag
python -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
pip install -r requirements.txt2. Configure environment variables
Create a .env file in the project root:
GROQ_API_KEY=your_groq_api_key
PINECONE_API_KEY=your_pinecone_api_key3. Pinecone index
Make sure your Pinecone index named bionic-rag-cloud exists and is populated before running. The system expects embeddings generated by google/embeddinggemma-300m.
4. Run the API server
python main.pyServer starts at http://127.0.0.1:8000.
5. Run the interactive CLI
python -m src.cliπ³ Docker
docker build -t bionic-rag .
docker run -p 8000:8000 --env-file .env bionic-ragπ‘ API Reference
POST /chat
Request body:
{
"chat": "Explain the key findings of DeepSeek-R1",
"thread_id": "session_abc"
}Response:
{
"res": "DeepSeek-R1 introduces... [0] ... [1]",
"sources": ["arxiv", "wikipedia", "bionic-rag-cloud"]
}π© State Schema
class RAGReflectionState(BaseModel):
question: str # Original user question
search_query: str # Current search query (may be rewritten)
retrieved_docs: list # Documents from all sources
answer: str # Generated answer
reflection: str # LLM self-critique output
revised: bool # Whether a retry is needed
attempts: int # Number of generation attempts madeβοΈ Configuration
Change the LLM β edit src/cofig.py:
llm = ChatGroq(model="openai/gpt-oss-120b", temperature=0.2)Change the embedding model or Pinecone index β edit src/vector_store.py:
embeddings = HuggingFaceEmbeddings(model="google/embeddinggemma-300m")
PineconeVectorStore(index_name="bionic-rag-cloud", ...)π License
MIT
