A Retrieval-Augmented Generation (RAG) system built with FastAPI, providing document-based question answering capabilities. This system allows you to load documents (PDF and TXT files), build semantic search indices, and query an AI model with relevant context for accurate responses.
- Document Loading: Support for PDF and TXT file formats
- Intelligent Chunking: Automatic text chunking with configurable overlap for better retrieval
- Semantic Search: Uses SentenceTransformers for embedding generation and similarity search
- LLM Integration: Powered by Ollama with streaming responses
- Conversation History: Maintains chat history for contextual queries
- RESTful API: FastAPI-based endpoints for easy integration
- Flexible Configuration: Customizable chunk sizes, system prompts, and model selection
- Python 3.8+
- Ollama installed and running locally
- Pull the required model:
ollama pull mistral(or your preferred model)
- Clone the repository:
git clone https://github.com/allwin-antony/AI-Rag.git
cd ai-rag- Install dependencies:
pip install -r requirements.txtRun the FastAPI server:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn main:appThe API will be available at http://localhost:8000
Once the server is running, visit http://localhost:8000/docs for interactive API documentation.
- Load Documents:
# Load a single document
curl -X POST "http://localhost:8000/add_document" \
-H "Content-Type: application/json" \
-d '{"file_path": "/path/to/document.pdf", "chunk_size": 500, "chunk_overlap": 50}'# Load all documents from a directory
curl -X POST "http://localhost:8000/load_documents_from_directory" \
-H "Content-Type: application/json" \
-d '{"directory": "/path/to/documents/", "chunk_size": 500, "chunk_overlap": 50}'- Build Index (optional - automatically built on first query):
curl -X POST "http://localhost:8000/build_index"- Query the System:
curl -X POST "http://localhost:8000/query/" \
-H "Content-Type: application/json" \
-d '{"question": "What is the main topic of the document?", "top_k": 3}'GET /- Health check endpointPOST /add_document- Load a single documentPOST /load_documents_from_directory- Load all documents from a directoryPOST /add_documents- Add documents directly as textPOST /build_index- Manually build the search indexGET /get_document_count- Get the number of loaded document chunksPOST /clear_documents- Clear all loaded documentsPOST /clear_conversation- Clear conversation historyPOST /set_system_prompt- Update the system promptPOST /query/- Query the RAG system (streaming response)
// Request
{
"file_path": "/path/to/document.pdf",
"chunk_size": 500,
"chunk_overlap": 50
}
// Response
{
"status": "success",
"message": "/path/to/document.pdf has successfully loaded into the rag"
}// Request
{
"question": "Summarize the key points",
"top_k": 3
}
// Response (streaming)"Based on the provided documents, the key points are..."The system uses Ollama for LLM inference. You can change the model by modifying the model_name parameter in the FileBasedRAG class initialization (default: "mistral").
chunk_size: Number of words per chunk (default: 500)chunk_overlap: Number of overlapping words between chunks (default: 50)
Customize the AI behavior by setting a custom system prompt:
curl -X POST "http://localhost:8000/set_system_prompt" \
-H "Content-Type: application/json" \
-d '{"prompt": "You are a technical documentation assistant..."}'- PDF: Requires PyPDF2 (included in requirements.txt)
- TXT: Plain text files with UTF-8 encoding
Key dependencies include:
fastapi: Web frameworkollama: LLM inferencesentence-transformers: Text embeddingsPyPDF2: PDF text extractionnumpy: Numerical computationsuvicorn: ASGI server
This project is licensed under the terms specified in the LICENSE file.
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
- Ollama Connection Issues: Ensure Ollama is running locally and the specified model is pulled
- PDF Loading Errors: Verify PyPDF2 is installed and PDFs are not password-protected
- Memory Issues: For large document collections, consider adjusting chunk sizes or using a more powerful machine