Skip to content

samagra44/knowledgeBot

Repository files navigation

knowledgeBot

Description

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.

Features

Document Management

  • 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.

Query Interface

  • 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.

Source Citation

  • 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.

User Interface

  • 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.

API Access

  • RESTful API: Programmatic access via FastAPI endpoints for document creation, listing, and querying.
  • Integration Ready: Easily integrate with other applications or automate workflows.

Technical Features

  • 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.

Architecture and Flow

System Architecture

  • 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.

Data Flow

  1. 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).
  2. 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.
  3. Response Presentation:

    • UI displays the answer with expandable sources.
    • API returns structured JSON with answer and sources.

Key Components

  • 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.

Folder Structure

.
├── .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

Installation

Prerequisites

  • Python 3.8 or higher
  • HuggingFace API key (for embeddings and LLM)
  • (Optional) Docker for containerized deployment

Local Installation

  1. Clone the Repository:

    git clone https://github.com/samagra44/knowledgeBot.git
    cd knowledgeBot
  2. Create a Virtual Environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install Dependencies:

    pip install -r requirements.txt
  4. Set Up Environment Variables: Create a .env file 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 model
    

    Note: The LLM model is hardcoded in services/llm_service.py but can be configured via environment variables if modified.

  5. Initialize the Database and Vector Store: The application automatically initializes the database and seeds sample data on startup.

  6. Run the Backend:

    python main.py

    This starts the FastAPI server on http://127.0.0.1:8000.

  7. Run the Frontend: In a separate terminal:

    python app.py

    This starts the Streamlit UI on http://localhost:8501.

  8. Access the Application:

    • UI: Open http://localhost:8501 in your browser.
    • API Docs: Visit http://127.0.0.1:8000/docs for interactive API documentation.

Docker Installation

  1. Build the Docker Image:

    docker build -t knowledgebot .
  2. Run the Container:

    docker run -p 8000:8000 -p 8501:8501 --env-file .env knowledgebot

    Ensure your .env file is in the current directory or pass environment variables accordingly.

  3. Access the Application:

    • UI: http://localhost:8501
    • API: http://localhost:8000

Usage

Using the UI

  1. Add Documents: Go to the "Manage Knowledge Base" tab, fill in the title, content, and optional source, then click "Save Document".
  2. 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.
  3. View Sources: Expand the "Sources Used" section to see referenced documents.

Using the API

  • 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}'

API Endpoints

  • POST /docs: Create a new document.

    • Body: {"title": str, "content": str, "source": str (optional)}
    • Response: {"id": int, "msg": str}
  • GET /documents: List documents.

    • Query Params: limit (int, default 100)
    • Response: [{"id": int, "title": str, "content": str, "source": str}, ...]
  • 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}, ...]}

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a feature branch.
  3. Make your changes.
  4. Add tests if applicable.
  5. Submit a pull request.

Acknowledgments

  • Built with FastAPI, Streamlit, LangChain, and HuggingFace.
  • Vector search powered by Chroma.
  • Inspired by advancements in RAG systems for knowledge management.

About

RAG based chatbot that empowers users to build a custom knowledge base

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages