A FastAPI-based service that provides document processing, question answering, and real-time chat capabilities using RAG (Retrieval Augmented Generation) techniques. The service can process PDF documents, generate FAQs, and provide interactive chat sessions with context-aware responses.
- PDF document processing and text extraction
- Document chunking and vector storage
- FAQ generation from documents
- Real-time WebSocket chat interface
- Session management for multiple chat instances
- Context-aware responses using RAG
- Interactive web interface for testing
- Python 3.8+
- Ollama (for local LLM support)
- OpenAI API key (for embeddings)
- Clone the repository:
git clone <repository-url>
cd basic-rag- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
Create a
.envfile in the root directory with:
OPENAI_API_KEY=your_openai_api_key
basic-rag/
├── src/
│ ├── api/ # API implementation
│ │ ├── core/ # Core configuration
│ │ ├── models/ # Pydantic models
│ │ ├── routes/ # API routes
│ │ └── services/ # API-specific services
│ ├── services/ # Core intelligent services
│ │ ├── chat/ # Chat functionality
│ │ ├── common/ # Shared utilities
│ │ ├── config/ # Configuration management
│ │ ├── document_processing/ # PDF and text processing
│ │ ├── faq_generation/ # FAQ generation logic
│ │ └── query_processing/ # Query handling and RAG
│ ├── static/ # Static files (HTML, CSS)
│ └── main.py # Main application entry
├── tests/ # Test files
├── data/ # Data directory for PDFs
├── requirements.txt # Main dependencies
└── requirements-test.txt # Test dependencies
- Start the FastAPI server:
# From the project root directory
python run.pyThe server will start at http://localhost:8000
- Access the API documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
- Test the WebSocket interface:
- Open
http://localhost:8000/static/test.htmlin your browser - Create a session with a PDF file
- Connect to the WebSocket
- Start chatting with the document
POST /sessions/{session_id}: Create a new chat sessionDELETE /sessions/{session_id}: Delete a chat sessionPOST /chat: Send a chat message (HTTP)POST /upload: Upload a PDF documentPOST /faq: Generate FAQs from a document
ws://localhost:8000/ws/chat/{session_id}: WebSocket connection for real-time chat
- Install test dependencies:
pip install -r tests/requirements-test.txt- Run the test suite:
python tests/test_websocket.pyTo run specific services for development or testing:
- Document Processing:
python -m src.services.document_processing.processor- FAQ Generation:
python -m src.services.faq_generation.processor- Chat Service:
python -m src.services.chat.processor- Create new models in
src/api/models/ - Add business logic in
src/services/(for core services) orsrc/api/services/(for API-specific services) - Create new routes in
src/api/routes/ - Update tests in the
tests/directory
The project follows PEP 8 guidelines. Use a code formatter like black for consistent formatting:
pip install black
black src/ tests/-
WebSocket Connection Issues
- Ensure the server is running
- Check if the session ID is valid
- Verify the PDF file exists and is accessible
-
PDF Processing Errors
- Check if the PDF file is not corrupted
- Verify file permissions
- Ensure the file path is correct
-
API Key Issues
- Verify your OpenAI API key is set in
.env - Check if the key is valid and has sufficient credits
- Verify your OpenAI API key is set in
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.