A modern web application featuring an AI-powered chatbot with RAG capabilities. Upload documents to create a knowledge base, and the chatbot will use them to provide contextual, accurate responses.
- AI-Powered Chatbot: Leverages OpenAI's GPT models for intelligent conversations
- RAG System: Uses ChromaDB vector database for document retrieval
- Document Upload: Support for PDF and TXT files
- Semantic Search: Finds relevant context using sentence embeddings
- Modern UI: Clean, responsive interface built with vanilla JavaScript
- Toggle RAG: Switch between RAG-enhanced and standard chatbot modes
- Real-time Updates: Live document count and system status
- FastAPI: Modern Python web framework
- ChromaDB: Vector database for document storage and retrieval
- Sentence Transformers: For creating document embeddings
- OpenAI API: Powers the chatbot responses
- HTML/CSS/JavaScript: No framework dependencies
- Responsive Design: Works on desktop and mobile
- Real-time Communication: Async API calls
- Python 3.8 or higher
- OpenAI API key
- pip (Python package manager)
-
Clone the repository:
git clone <your-repo-url> cd Contracting-Government-Expert
-
Install Python dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp .env.example .env
-
Edit
.envfile and add your OpenAI API key:OPENAI_API_KEY=sk-your-api-key-here
Run the application using Python:
python -m backend.mainOr use the run script:
python run.pyThe server will start at http://localhost:8000
-
Open your browser and navigate to
http://localhost:8000 -
Upload Documents:
- Click "Choose File" in the sidebar
- Select a .txt or .pdf file
- Click "Upload"
- The document will be processed and added to the knowledge base
-
Chat with the AI:
- Type your message in the text area at the bottom
- Press Enter or click "Send"
- The AI will respond using context from your uploaded documents (if RAG is enabled)
-
Toggle RAG:
- Use the toggle switch to enable/disable RAG
- When disabled, the chatbot responds without document context
-
Clear Documents:
- Click "Clear All Documents" to remove all uploaded documents
- This will reset the knowledge base
GET /api/health
Returns system status and document count.
POST /api/chat
Body: {
"message": "your question",
"use_rag": true
}
Send a message and get an AI response.
POST /api/upload
Body: FormData with file
Upload a document to the knowledge base.
GET /api/documents/count
Get the number of document chunks in the system.
DELETE /api/documents
Remove all documents from the knowledge base.
Edit the .env file to customize:
| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
Your OpenAI API key | Required |
HOST |
Server host | 0.0.0.0 |
PORT |
Server port | 8000 |
COLLECTION_NAME |
ChromaDB collection name | documents |
EMBEDDING_MODEL |
Sentence transformer model | sentence-transformers/all-MiniLM-L6-v2 |
CHUNK_SIZE |
Document chunk size | 1000 |
CHUNK_OVERLAP |
Chunk overlap | 200 |
Contracting-Government-Expert/
├── backend/
│ ├── models/
│ │ └── schemas.py # Pydantic models
│ ├── routes/
│ │ └── api.py # API endpoints
│ ├── services/
│ │ ├── chat_service.py # AI chat logic
│ │ └── rag_service.py # RAG implementation
│ └── main.py # FastAPI application
├── frontend/
│ ├── static/
│ │ ├── css/
│ │ │ └── styles.css # Styling
│ │ └── js/
│ │ └── app.js # Frontend logic
│ └── index.html # Main page
├── documents/ # Place for sample documents
├── .env # Environment variables
├── .env.example # Example environment file
├── requirements.txt # Python dependencies
├── run.py # Run script
└── README.md # This file
- Document Upload: When you upload a document, it's split into chunks
- Embedding: Each chunk is converted to a vector embedding
- Storage: Embeddings are stored in ChromaDB
- Query: When you ask a question, it's also converted to an embedding
- Retrieval: Similar document chunks are retrieved using vector similarity
- Generation: Retrieved chunks are sent to the AI as context
- Response: The AI generates a response using the context
- Make sure your API key is correct in
.env - Check you have credits in your OpenAI account
- Verify the API key has proper permissions
- Change the PORT in
.envto a different value - Or kill the process using port 8000
- Ensure all dependencies are installed:
pip install -r requirements.txt - Make sure you're running from the project root directory
- Check file format (only .txt and .pdf supported)
- Ensure file size is reasonable
- Check server logs for errors
- Backend: Add new routes in
backend/routes/api.py - Services: Add business logic in
backend/services/ - Frontend: Update
frontend/static/js/app.jsfor new UI features
Test the API using curl:
# Health check
curl http://localhost:8000/api/health
# Send a chat message
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"message":"Hello", "use_rag":true}'- FastAPI: Modern, fast web framework
- ChromaDB: Vector database
- OpenAI: GPT models
- Sentence Transformers: Document embeddings
- PyPDF: PDF processing
- Uvicorn: ASGI server
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues and questions, please open an issue on the GitHub repository.