CoolFace
Apppublic

Omthakur1394/self-aware-rag

sourceHugging Faceupdated 25d agoView on Hugging Face
0likes
App README

Self-Aware-Rag🦾 : Production-Grade Agentic Research Assistant

Python FastAPI Docker Pinecone LangGraph

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 Docling to 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 LangGraph and 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)
  1. 1.Retrieve β€” Pulls documents from Pinecone, Wikipedia, and arXiv.
  2. 2.Generate β€” Produces a cited answer with mandatory inline citations ([0], [1], ...).
  3. 3.Reflect β€” The LLM evaluates whether the answer is complete, accurate, and properly cited.
  4. 4.Branch:
  5. 5.YES or attempts >= 2 β†’ answer is finalized and returned.
  6. 6.NO β†’ query is rewritten based on the reflection feedback and the loop retries.

πŸ› οΈ Technology Stack

ComponentTechnology
FrameworkFastAPI + Uvicorn
AI OrchestrationLangGraph, LangChain
LLM ProviderGroq API (openai/gpt-oss-120b)
EmbeddingsHuggingFace (google/embeddinggemma-300m)
Vector DatabasePinecone (bionic-rag-cloud)
Document ParsingDocling
External RetrievalWikipedia + arXiv
DeploymentDocker

πŸ“ 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

bash
cd bionic-rag
python -m venv venv
source venv/bin/activate        # On Windows: .\venv\Scripts\activate
pip install -r requirements.txt

2. Configure environment variables

Create a .env file in the project root:

env
GROQ_API_KEY=your_groq_api_key
PINECONE_API_KEY=your_pinecone_api_key

3. 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

bash
python main.py

Server starts at http://127.0.0.1:8000.

5. Run the interactive CLI

bash
python -m src.cli

🐳 Docker

bash
docker build -t bionic-rag .
docker run -p 8000:8000 --env-file .env bionic-rag

πŸ“‘ API Reference

POST /chat

Request body:

json
{
  "chat": "Explain the key findings of DeepSeek-R1",
  "thread_id": "session_abc"
}
FieldTypeRequiredDescription
chatstringβœ…The user's question
thread_idstring❌Session ID for memory continuity (default: "1")

Response:

json
{
  "res": "DeepSeek-R1 introduces... [0] ... [1]",
  "sources": ["arxiv", "wikipedia", "bionic-rag-cloud"]
}
FieldDescription
resFinal answer with inline source citations
sourcesSource identifiers for all retrieved documents

πŸ”© State Schema

python
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:

python
llm = ChatGroq(model="openai/gpt-oss-120b", temperature=0.2)

Change the embedding model or Pinecone index β€” edit src/vector_store.py:

python
embeddings = HuggingFaceEmbeddings(model="google/embeddinggemma-300m")
PineconeVectorStore(index_name="bionic-rag-cloud", ...)

πŸ“„ License

MIT