AnukulChandra/Mini-AI_Assistant
title: Mini AI Assistant emoji: ๐ค colorFrom: indigo colorTo: purple sdk: docker app_port: 7860 pinned: false ---
Mini AI Assistant ๐ค
A lightweight RAG (Retrieval-Augmented Generation) API built with FastAPI. Upload documents, ask questions, and get answers powered by your preferred LLM โ all through a clean REST API.
Overview ๐
Mini AI Assistant lets you:
- Upload PDF, TXT, and Markdown documents
- Automatically chunk and index them into a FAISS vector store
- Ask questions about the uploaded documents
- Retrieve the most relevant chunks and generate answers via an LLM
- Look up orders and products from local JSON files via smart tool routing
- Keep conversation history across turns
Features โจ
Architecture ๐๏ธ
graph TD
User -->|Question| Frontend
Frontend -->|POST /chat/ask| FastAPI
FastAPI --> Memory[Conversation Memory]
Memory --> Intent[Intent Detection]
Intent -->|Knowledge| FAISS[(FAISS Vector Store)]
Intent -->|Order| Orders[(orders.json)]
Intent -->|Product| Products[(products.json)]
Intent -->|Direct| LLM[Groq LLM]
FAISS --> Context[Retrieved Context + Memory + Tool Output]
Orders --> Context
Products --> Context
Context --> Prompt[Prompt Builder]
Prompt --> LLM
LLM --> Response
Response --> FrontendDocument ingestion flow: POST /upload/document โ validate โ read โ splittext โ createvector_store โ save to disk.
AI Pipeline ๐ง
- User uploads a document โ Upload a PDF, TXT, or Markdown file via the frontend.
- Document validation โ The file extension and content are validated against allowed types.
- Text extraction โ Text is extracted from PDFs using PyPDF or read directly from plain-text files.
- Chunking โ Extracted text is split into overlapping chunks using
RecursiveCharacterTextSplitter. - Embedding generation โ Each chunk is converted into a vector embedding using
sentence-transformers/all-MiniLM-L6-v2. - FAISS indexing โ Embeddings are indexed in a FAISS vector store and persisted to disk.
- User asks a question โ The question is submitted through the chat interface.
- Load conversation memory โ Previous question-answer pairs are loaded from in-memory history.
- Intent detection โ The system classifies the question as Knowledge, Order, Product, or Direct LLM.
- Route to:
- Knowledge Retrieval โ Perform similarity search over the FAISS index for relevant chunks.
- Order Tool โ Look up order status from
orders.json. - Product Tool โ Search product details in
products.json. - Build prompt โ Retrieved context, tool output, and conversation history are assembled into a structured prompt.
- Generate answer using Groq โ The prompt is sent to the Groq LLM (
llama-3.3-70b-versatile) for response generation. - Return response โ The answer is sent back to the frontend and displayed to the user.
Prompt Design ๐
The system prompt instructs the assistant to answer only using the provided information. Conversation history is included when available so follow-up questions maintain context. Tool results (order status, product details) are injected directly into the response without querying the vector store.
- Knowledge questions โ Retrieved document chunks are placed in the prompt as context. The assistant must answer from those chunks alone.
- Memory questions โ Previous conversation turns are prepended so the assistant can reference past exchanges.
- Tool queries โ The tool result is formatted into a natural-language answer and returned immediately without LLM generation.
If the answer cannot be found in the uploaded documents, the assistant replies:
"I couldn't find that information in the uploaded documents."
Screenshots ๐ธ
Project Structure ๐
Mini-Ai-Assistant/
โโโ api/
โ โโโ chat.py # POST /chat/ask endpoint
โ โโโ upload.py # POST /upload/document endpoint
โโโ services/
โ โโโ chunking.py # Text splitting
โ โโโ embeddings.py # HuggingFace embedding model
โ โโโ ingestion.py # File validation & text extraction
โ โโโ llm.py # Multi-provider LLM dispatch
โ โโโ memory.py # In-memory conversation history
โ โโโ prompt_builder.py # RAG prompt construction
โ โโโ retrieval.py # FAISS vector search
โ โโโ tools.py # Order/product lookup tools
โ โโโ vector_store.py # FAISS create/save/load
โโโ data/
โ โโโ orders.json # Sample order data
โ โโโ products.json # Sample product data
โ โโโ vector_store/ # Persisted FAISS index
โโโ main.py # FastAPI entry point
โโโ .env.example # Environment template
โโโ requirements.txt # Python dependencies
โโโ README.md # This fileInstallation ๐ ๏ธ
Prerequisites
- Python 3.10+
- pip
Steps
# Clone the repository
git clone https://github.com/Anukul-Chandra/Mini-Ai-Assistant.git
cd Mini-Ai-Assistant
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/macOS
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txtEnvironment Variables ๐
Copy .env.example to .env and fill in your API keys:
cp .env.example .envSet LLM_PROVIDER to your chosen backend. Only the corresponding API key is required โ the rest can be left blank.Running the Project ๐
uvicorn main:app --reloadThe API will be available at http://localhost:8000.
Interactive API docs: http://localhost:8000/docs (Swagger UI)
API Endpoints ๐ก
GET /
Health check.
Response:
{
"message": "Mini AI Assistant API is running"
}POST /upload/document
Upload a document for indexing.
Response `200`:
{
"filename": "report.pdf",
"chunks": 12,
"message": "Document processed and indexed successfully."
}Response `400`: Invalid file type or unreadable document.
POST /chat/ask
Ask a question about your documents.
Request:
{
"question": "What is this document about?"
}Response `200` (RAG):
{
"question": "What is this document about?",
"answer": "The document discusses quarterly sales performance...",
"retrieved_chunks": 3
}Response `200` (Tool):
{
"source": "tool",
"answer": {
"order_id": "ORD-1001",
"status": "shipped",
"total": 245.99
}
}Response `400`: No document uploaded or LLM configuration error.
Supported LLM Providers ๐ง
Switch between them by changing the LLM_PROVIDER environment variable.
Example Requests ๐ป
Upload a document
curl -X POST http://localhost:8000/upload/document \
-F "file=@document.pdf"Ask a question (RAG)
curl -X POST http://localhost:8000/chat/ask \
-H "Content-Type: application/json" \
-d '{"question": "What are the key findings?"}'Ask about an order (tool routing)
curl -X POST http://localhost:8000/chat/ask \
-H "Content-Type: application/json" \
-d '{"question": "Show me order ORD-1001"}'Example Responses ๐ค
RAG answer:
{
"question": "What is the revenue for Q3?",
"answer": "Based on the uploaded document, the revenue for Q3 was $1.2 million, representing a 15% increase over Q2.",
"retrieved_chunks": 3
}Tool lookup:
{
"source": "tool",
"answer": {
"order_id": "ORD-1001",
"customer": "John Doe",
"status": "delivered",
"items": ["Laptop", "Mouse"],
"total": 1249.99
}
}Error:
{
"detail": "No vector store found. Please upload a document first."
}Technology Stack ๐ ๏ธ
Future Improvements ๐ง
- [ ] Authentication and API key management
- [ ] Persistent conversation storage (SQLite / PostgreSQL)
- [ ] Multi-document upload and per-document scoping
- [ ] Streaming responses via Server-Sent Events
- [ ] Document deletion and re-indexing
- [ ] Evaluation metrics for RAG quality (faithfulness, relevance)
- [ ] Docker image for one-command deployment
License ๐
This project is licensed under the MIT License.
