https://github.com/mahbub2001/rag-fastapi
https://github.com/mahbub2001/rag-fastapi
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mahbub2001/rag-fastapi
- Owner: Mahbub2001
- Created: 2025-06-25T20:15:13.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-25T21:03:09.000Z (12 months ago)
- Last Synced: 2025-06-25T21:36:15.508Z (12 months ago)
- Language: Python
- Size: 10.9 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π InsightEngine β AI-Powered Document Query System
A lightweight AI micro-agent built with **FastAPI**, **FAISS**, **SentenceTransformers**, and **OpenRouter/OpenAI**, designed to retrieve insights from multiple research documents in PDF format.
---
## π Features
- π§ Embedding-based document retrieval (FAISS + all-MiniLM-L6-v2)
- π Supports querying over multiple PDF documents
- π§Ύ Structured answers with citations (`[Source: doc_X]`)
- β‘ SQLite caching for faster repeat queries
- π Streaming responses token-by-token
- π Query & response logging in SQLite
- π¦ OpenAI/OpenRouter compatible LLM backend
---
## π§° Tech Stack
- Python, FastAPI
- FAISS (vector search)
- sentence-transformers (`all-MiniLM-L6-v2`)
- SQLite3 for caching/logging
- OpenAI/OpenRouter LLM API
- PyMuPDF (`fitz`) for PDF text extraction
---
## π Project Structure
```plaintext
βββ data/ # Folder containing your PDF documents
βββ main.py # FastAPI app with endpoints
βββ retriever.py # FAISS-based document chunking & search
βββ llm_agent.py # LLM prompt creation and streaming
βββ db.py # SQLite caching & query logging
βββ requirements.txt # Python dependencies
βββ .env # API keys and environment variables
```
---
## βοΈ Setup Instructions
### 1. Clone this repo
```bash
git clone https://github.com/your-username/insightengine
cd insightengine
```
### 2. Install dependencies
```bash
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
pip install -r requirements.txt
```
### 3. Add PDFs
Place at least 10 short .pdf documents inside the data/ folder.
### 4. Create .env
OPENROUTER_API_KEY=your_api_key_here
### 5. Run the API
uvicorn main:app --reload
π API Usage
π POST /query
Returns cached or newly generated answer.
Request:
```bash
{
"query": "What is the conclusion of document 3?"
}
```
Response:
```bash
{
"answer": "- The study highlights that... [Source: doc_03]",
"sources": ["doc_03_chunk_1", "doc_01_chunk_0", "doc_02_chunk_2"]
}
```
π POST /query_stream
Streams the answer token-by-token (for faster UI feedback).
πΎ Caching & Logging
SQLite Cache: Stores previous queries and responses in cache.db
Logged Items:
--Query string
--Prompt content
--Final answer
--Source chunks used
β
Requirements
Python 3.8+
No Redis required β uses SQLite-based caching and logging
π Security
All model answers are grounded in retrieved documents
Prompts explicitly tell the LLM to avoid hallucinations
Example instruction:
βOnly use the following context to answer. Do not make up facts. Always cite the source as [Source: doc_X].β
π¦ Models & LLMs
You can use any OpenRouter-supported model, for example:
qwen/qwen-2.5-coder-32b-instruct:free
mistralai/mixtral-8x7b-instruct
meta-llama/llama-3-70b-instruct
Set the model in llm_agent.py.
πββοΈ Author
Mahbub Ahmed Turza
π License
MIT License β free to use, modify, and distribute.