knowledgeBot is a Retrieval-Augmented Generation (RAG) chatbot that empowers users to build a custom knowledge base by adding documents. Users can then query this knowledge base to receive AI-generated answers based on the most relevant documents, with sources cited for transparency and verification. The system combines a FastAPI backend for robust API services, a Streamlit frontend for an intuitive user interface, and leverages HuggingFace models for embeddings and language generation, along with Chroma for efficient vector-based document retrieval.
The application is designed to handle document ingestion, storage, and querying in a scalable manner, making it suitable for personal knowledge management, educational tools, or enterprise knowledge bases.
- Add Documents: Users can add documents to the knowledge base by providing a title, content, and optional source URL/reference. Documents are stored in a SQLite database and indexed in a FAISS vector store for fast retrieval.
- List Documents: View all documents in the knowledge base, including their titles and sources.
- Automatic Embedding: Upon addition, documents are automatically embedded using HuggingFace's embedding models to enable semantic search.
- Natural Language Queries: Ask questions in natural language, and the system retrieves the most relevant documents to generate accurate answers.
- Configurable Retrieval: Adjust the number of top documents (top_k) to retrieve for answer generation, allowing users to balance between speed and comprehensiveness.
- AI-Generated Answers: Answers are generated by a large language model (LLM) using the retrieved documents as context, ensuring responses are grounded in the provided knowledge base.
- Transparent Sources: Every answer includes citations to the source documents used, with expandable sections showing document titles, sources, and content snippets.
- Metadata Tracking: Each document retains metadata such as ID, title, and source for easy reference.
- Streamlit Frontend: A clean, web-based interface with tabs for chatting and managing the knowledge base.
- Chat History: Persistent chat history within sessions, with options to clear history.
- Real-time Feedback: Loading indicators and error messages for a smooth user experience.
- RESTful API: Programmatic access via FastAPI endpoints for document creation, listing, and querying.
- Integration Ready: Easily integrate with other applications or automate workflows.
- Vector Store: Utilizes Chroma for efficient similarity search over document embeddings.
- Embeddings Service: Powered by HuggingFace Endpoint Embeddings for high-quality text representations.
- LLM Service: Integrates HuggingFace models for answer generation, with configurable parameters like temperature and token limits.
- Database: SQLite-based storage for documents, with async support for performance.
- Logging: Comprehensive logging for debugging and monitoring.
- Containerization: Docker support for easy deployment.
- Backend: FastAPI application handling API requests, database operations, and vector store management.
- Frontend: Streamlit application providing the user interface.
- Database: SQLite for document storage.
- Vector Store: Chroma for embedding storage and retrieval.
- Services: Modular services for embeddings, LLM, and logging.
-
Document Ingestion:
- User submits a document (title, content, source) via UI or API.
- Document is saved to the SQLite database.
- Content is embedded using the HuggingFace embeddings service.
- Embedding is added to the FAISS vector store with metadata (ID, title, source).
-
Query Processing:
- User submits a query via UI or API.
- Query is embedded using the same embeddings model.
- Top-k similar documents are retrieved from the FAISS vector store.
- Retrieved documents are passed as context to the LLM along with the query.
- LLM generates an answer based on the context.
- Answer and source metadata are returned to the user.
-
Response Presentation:
- UI displays the answer with expandable sources.
- API returns structured JSON with answer and sources.
- Embeddings: Convert text to vectors for semantic similarity.
- Retriever: FAISS-based retrieval of relevant documents.
- Generator: LLM for answer synthesis.
- Prompt Engineering: Custom prompts to guide the LLM for accurate, sourced answers.
.
├── .dockerignore # Docker ignore file
├── .gitignore # Git ignore file
├── app.py # Entry point for Streamlit UI
├── main.py # FastAPI application entry point
├── Dockerfile # Docker configuration
├── requirements.txt # Python dependencies
├── setup.py # Setup script
├── template.py # Template file
├── api/ # API endpoints
│ ├── __init__.py
│ └── v1/
│ ├── __init__.py
│ └── api.py # FastAPI router with endpoints
├── backend/ # Database and initialization
│ ├── __init__.py
│ └── load_database.py # DB setup, seeding, and async session
├── config/ # Configuration management
│ ├── __init__.py
│ └── configurations.py # Environment variables and settings
├── data/ # Data directory (created at runtime)
├── frontend/ # Frontend UI
│ └── ui_setup.py # Streamlit UI setup
├── helper/ # Helper utilities
│ ├── __init__.py
│ └── load_samples.py # Sample data for seeding
├── loggers/ # Logging configuration
│ ├── __init__.py
│ └── logging_config.py # Logger setup
├── prompts/ # Prompt templates
│ ├── __init__.py
│ └── prompt_templates.py # RAG prompt template
├── schemas/ # Data models
│ ├── __init__.py
│ ├── request_models.py # Pydantic/SQLModel classes for requests
│ └── response_models.py # Response models
├── services/ # Core services
│ ├── __init__.py
│ ├── embedding_service.py # HuggingFace embeddings
│ └── llm_service.py # HuggingFace LLM
├── tests/ # Test suites
│ ├── __init__.py
│ ├── test_llm.py # LLM tests
│ ├── integration/ # Integration tests
│ │ └── __init__.py
│ └── unit/ # Unit tests
│ ├── __init__.py
│ └── test_logger.py # Logger tests
├── utils/ # Utilities
│ ├── __init__.py
│ └── load_crud.py # CRUD operations for documents
└── vectorstore/ # Vector store management
└── load_vector_store.py # Chroma vector store loading and addition
- Python 3.8 or higher
- HuggingFace API key (for embeddings and LLM)
- (Optional) Docker for containerized deployment
-
Clone the Repository:
git clone https://github.com/samagra44/knowledgeBot.git cd knowledgeBot -
Create 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 the following variables:HUGGINGFACE_API_KEY=your_huggingface_api_key HUGGINGFACE_TASK=feature-extraction HUGGINGFACE_API_MODEL=sentence-transformers/all-MiniLM-L6-v2 # Or your preferred embedding modelNote: The LLM model is hardcoded in
services/llm_service.pybut can be configured via environment variables if modified. -
Initialize the Database and Vector Store: The application automatically initializes the database and seeds sample data on startup.
-
Run the Backend:
python main.py
This starts the FastAPI server on
http://127.0.0.1:8000. -
Run the Frontend: In a separate terminal:
python app.py
This starts the Streamlit UI on
http://localhost:8501. -
Access the Application:
- UI: Open
http://localhost:8501in your browser. - API Docs: Visit
http://127.0.0.1:8000/docsfor interactive API documentation.
- UI: Open
-
Build the Docker Image:
docker build -t knowledgebot . -
Run the Container:
docker run -p 8000:8000 -p 8501:8501 --env-file .env knowledgebot
Ensure your
.envfile is in the current directory or pass environment variables accordingly. -
Access the Application:
- UI:
http://localhost:8501 - API:
http://localhost:8000
- UI:
- Add Documents: Go to the "Manage Knowledge Base" tab, fill in the title, content, and optional source, then click "Save Document".
- Query the Knowledge Base: In the "Chat Interface" tab, type your question and adjust settings if needed. The AI will respond with an answer and sources.
- View Sources: Expand the "Sources Used" section to see referenced documents.
- Add Document:
curl -X POST "http://127.0.0.1:8000/docs" \ -H "Content-Type: application/json" \ -d '{"title": "Sample Title", "content": "Sample content", "source": "http://example.com"}'
- List Documents:
curl "http://127.0.0.1:8000/documents?limit=10" - Query:
curl -X POST "http://127.0.0.1:8000/query" \ -H "Content-Type: application/json" \ -d '{"query": "What is machine learning?", "top_k": 3}'
-
POST /docs: Create a new document.- Body:
{"title": str, "content": str, "source": str (optional)} - Response:
{"id": int, "msg": str}
- Body:
-
GET /documents: List documents.- Query Params:
limit(int, default 100) - Response:
[{"id": int, "title": str, "content": str, "source": str}, ...]
- Query Params:
-
POST /query: Query the knowledge base.- Body:
{"query": str, "top_k": int (optional, default 5)} - Response:
{"query": str, "answer": str, "sources": [{"id": int, "title": str, "source": str}, ...]}
- Body:
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch.
- Make your changes.
- Add tests if applicable.
- Submit a pull request.
- Built with FastAPI, Streamlit, LangChain, and HuggingFace.
- Vector search powered by Chroma.
- Inspired by advancements in RAG systems for knowledge management.