A robust, developer-friendly AI agent built with FastAPI that leverages Gemini and RAG (Retrieval-Augmented Generation) to provide grounded, context-aware answers.
Interested in seeing the agent in action? Watch the technical walkthrough below:
- Semantic Search: Utilizes
sentence-transformersto retrieve highly relevant context from a local knowledge base. - Agent Reasoning: Explicit decision-making logic determines whether to explain, compare, or fallback based on retrieved data.
- FastAPI Core: High-performance, REST API.
- Gemini Powered: Uses Google's Gemini model for high-quality, grounded responses.
- Zero Hallucination: Strict prompting ensures the agent only answers from provided context.
.
├── agent/ # Core agent logic and tool definitions
│ ├── core.py # Main Agent class with decision logic and LLM integration
│ └── tools.py # Tool definitions (e.g., search_docs)
├── knowledge/ # Knowledge base and retrieval layer
│ ├── documents.json # Raw document storage (JSON)
│ └── store.py # Semantic search and embedding management
├── images/ # Documentation assets and demo video
├── tests/ # Unit tests for agent verification
├── main.py # FastAPI application and endpoint definitions
├── config.py # Global configuration settings
├── requirements.txt # Python dependencies
└── .env.example # Environment variable template
git clone https://github.com/soumyaGhoshh/mini-rag-agent
cd mini-rag-agent# Create the virtual environment
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate# Create the virtual environment
python -m venv venv
# Activate the virtual environment
venv\Scripts\activateCreate a .env file in the root directory:
GEMINI_API_KEY=your_api_key_here
LLM_MODEL=gemini-2.0-flashNote
Ensure you have a valid Gemini API key from Google AI Studio.
pip install -r requirements.txtOnce the environment is active, run the following command:
uvicorn main:app --reloadThe API will be available at http://localhost:8000. Interactive docs: http://localhost:8000/docs.
If port 8000 is occupied, you can kill the existing process:
lsof -ti:8000 | xargs kill -9Or run on a different port:
uvicorn main:app --reload --port 8001Endpoint: POST /agent/query
Request:
{
"query": "What is FastAPI?"
}Response:
{
"answer": "FastAPI is a modern, fast (high-performance), web framework for building APIs...",
"documents_used": ["FastAPI Basics"],
"agent_decision": "answered_with_explanation"
}- Documents: Stored in
knowledge/documents.json. - Embeddings: Generated on startup using
sentence-transformers/all-MiniLM-L6-v2. - Retrieval: Uses cosine similarity to find the top-k most relevant documents for a given query.
- Receive Query: The agent receives a user question.
- Tool Usage: Calls the
search_docstool to retrieve potential context. - Decision Making:
- Relevance Check: If the similarity score of the top result is below a threshold (default 0.3), the agent decides it lacks information and returns a fallback message.
- Classification: If context is found, the agent classifies the intent (Explanation vs Comparison) based on keywords (heuristic for this mini-agent, can be LLM-based).
- Generation: Constructs a prompt with the retrieved context and queries the LLM to generate the final answer.
- In-Memory Vector Store: For simplicity and speed in this "mini" scale, embeddings are stored in memory (NumPy arrays). For production with millions of docs, a dedicated Vector DB like Chroma or Pinecone is required.
- Model Loading: The embedding model is loaded on startup. This increases startup time but ensures fast query processing.
- Heuristic Decision: The decision to "explain" or "compare" is currently based on simple keyword matching for visibility. A more complex agent would use the LLM to classify intent explicitly.
- Synchronous Processing: The current implementation is largely synchronous. For high traffic, async database/vector store calls would be preferred.