feat: add conversation history support to chat service and update Gradio UI for interactive sessions
491caa2 | title: RAG Knowledge Assistant | |
| emoji: π¨ | |
| colorFrom: indigo | |
| colorTo: yellow | |
| sdk: gradio | |
| sdk_version: 6.15.2 | |
| python_version: '3.13' | |
| app_file: app.py | |
| pinned: false | |
| license: mit | |
| # π€ RAG Knowledge Assistant | |
| Welcome to the **RAG Knowledge Assistant**βa high-performance, modular Retrieval-Augmented Generation (RAG) system tailored for the Playmobil Toy Shop. It utilizes vector search in **Qdrant** and inference with the **Groq LLM API** to answer user queries with precise store policies and product descriptions, now enhanced with full conversation history and interactive session state. | |
| --- | |
| ## π Key Features | |
| 1. **Modular Clean Architecture**: Complete separation of the presentation layer (Gradio UI), data orchestration, vector databases, and modular RAG services. | |
| 2. **Conversational Session State & History**: Automatically retains preceding user and assistant messages so the virtual toy assistant (Maya) can understand and answer context-aware follow-up queries. | |
| 3. **Dynamic AI Query Routing**: Automatically routes questions to the correct context category (`policy`, `product`, or `none`) using high-speed classification. | |
| 4. **Optimized RAG Pipeline**: Skips expensive database query retrievals for general conversational questions (`none` classification) to improve latency and reduce cost. | |
| 5. **Metadata Filtered Retrieval**: Restricts vector search using Qdrant index payload checks for precise target matching. | |
| 6. **Semantic Document Chunking**: Implements a robust text splitting algorithm using recursive character chunking with overlap to preserve semantic context across chunk lines. | |
| 7. **100% Offline Testing Suite**: Includes **29 fast unit tests** that fully mock external database connections and model downloads. | |
| --- | |
| ## π Project Structure | |
| ```bash | |
| βββ app.py # Declarative entrypoint establishing the Gradio interface | |
| βββ config.py # Central configuration & env variable loader | |
| βββ requirements.txt # Python package dependencies | |
| βββ data/ # Raw dataset files | |
| β βββ policies.json # Playmobil shop return, safety, and shipping policies | |
| β βββ products.json # Catalog of toy packages, contents, and pricing | |
| βββ db/ # Database connection & provisioning | |
| β βββ bootstrap.py # Entry for schema setups on startup | |
| β βββ qdrant_client.py # Connection setup & index creations | |
| β βββ vectorstore.py # HuggingFace Embeddings & LangChain vector wrappers | |
| βββ rag/ # Core retrieval and prompt structures | |
| β βββ prompts.py # LLM system/context templates | |
| β βββ retriever.py # Similarity search & metadata filtering | |
| β βββ router.py # AI Query categorization prompt & logic | |
| βββ services/ # High-level business logic coordinators | |
| β βββ chat_service.py # Coordinating Chat RAG pipeline flow | |
| β βββ ingestion_service.py # Document loading, tagging, chunking, and ingesting | |
| β βββ ui_handlers.py # Gradio callback handlers keeping app.py completely declarative | |
| β βββ llm.py # Groq API client provisioning | |
| βββ tests/ # Automated test suite | |
| βββ conftest.py # 100% Offline mocking layer intercepting API clients | |
| βββ test_chat_service.py # Unit tests for the main Chat service & history flow | |
| βββ test_ingestion_service.py # Unit tests for parsing, chunking, and db loader | |
| βββ test_retriever.py # Unit tests for vector similarities search | |
| βββ test_router.py # Unit tests for query routing classifications | |
| ``` | |
| --- | |
| ## βοΈ Setup Instructions | |
| ### 1. Prerequisites | |
| - Python 3.13 or newer installed. | |
| - A Qdrant Cluster (Cloud or Local instance). | |
| - A Groq Cloud API Key. | |
| ### 2. Install Dependencies | |
| Create a virtual environment and install the required modules: | |
| ```bash | |
| python3 -m venv .venv | |
| source .venv/bin/activate | |
| pip install -r requirements.txt | |
| ``` | |
| ### 3. Environment Variables | |
| Create a `.env` file in the root directory: | |
| ```env | |
| GROQ_API_KEY=your_groq_api_key_here | |
| QDRANT_URL=https://your-qdrant-endpoint-here.aws.cloud.qdrant.io | |
| QDRANT_API_KEY=your_qdrant_api_key_here | |
| STORE_NAME="Playmobil Toy Shop" | |
| STORE_DESCRIPTION="A premium online store selling high-quality Playmobil toys." | |
| ``` | |
| --- | |
| ## π Running the Application | |
| To run the application locally: | |
| ```bash | |
| python app.py | |
| ``` | |
| This will: | |
| 1. Bootstrap the Qdrant collections and payload keyword indices. | |
| 2. Launch the **Gradio Chat UI** on `http://localhost:7860`. | |
| ### Redesigned Chat Interface | |
| * **Interactive Chat Column**: Embeds a native `gr.Chatbot` utilizing active session state. Clear your text input field and post updates instantly. Supports both hitting **Enter** and clicking **Send π**. | |
| * **Sidebar Controls & Utilities**: Allows toggling filter modes, checking background document indexing triggers via real-time status updates, or clicking **ποΈ Clear History** to start a new chat session. | |
| --- | |
| ## π§ͺ Testing Suite | |
| The testing suite contains **29 unit tests** covering every functional capability of the assistant. | |
| ### Offline Testing Architecture | |
| To guarantee fast, deterministic execution and eliminate API cost: | |
| * Tests run **100% offline** and do not require internet access, model downloads, or API keys. | |
| * A custom mocking layer (`tests/conftest.py`) intercepts imports of `groq`, `qdrant_client`, and `langchain` modules using dynamic Python `sys.modules` patching. | |
| * No raw Hugging Face embedding model is ever loaded or downloaded. | |
| ### Running the Tests | |
| To run the test suite, run `pytest` from the root directory: | |
| ```bash | |
| # Run all tests | |
| pytest -v | |
| # Run with output capture | |
| pytest -s | |
| ``` | |
| All tests should pass in under 3 seconds! |