feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'
This commit is contained in:
@@ -1,87 +1,118 @@
|
||||
# FAQ Bot – ChromaDB + Ollama
|
||||
# FAQ Bot – QDrant Vector Store
|
||||
|
||||
This repository contains a simple FAQ chatbot that uses:
|
||||
|
||||
- **Ollama** for embeddings (`nomic-embed-text`) and text generation.
|
||||
- **ChromaDB** as the vector store.
|
||||
- **LangChain** to orchestrate the retrieval and generation pipeline.
|
||||
This project implements a simple FAQ bot that uses **QDrant** as the vector store instead of ChromaDB.
|
||||
The bot can ingest a text file containing FAQ content, embed the text using OpenAI embeddings, store the embeddings in QDrant, and answer user questions by retrieving the most relevant passages.
|
||||
|
||||
## Features
|
||||
|
||||
- Loads a small set of FAQ questions and answers.
|
||||
- Generates embeddings with the `nomic-embed-text` model.
|
||||
- Stores embeddings in a persistent ChromaDB collection.
|
||||
- Retrieves the most relevant answer to a user query.
|
||||
- Generates a natural language response using an Ollama LLM.
|
||||
- **Vector Store** – QDrant (via `qdrant-client`)
|
||||
- **Embeddings** – OpenAI `text-embedding-ada-002`
|
||||
- **CLI** – Ingest data, query the bot, delete the collection
|
||||
- **API** – `get_response(question: str, top_k: int = 5)` for integration with tools like MCP-tool
|
||||
|
||||
## Requirements
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.10+
|
||||
- Ollama server running locally (default port 11434).
|
||||
Install from https://ollama.ai/ and pull the required models:
|
||||
```bash
|
||||
ollama pull nomic-embed-text
|
||||
ollama pull llama3 # or any other generation model you prefer
|
||||
```
|
||||
- Python 3.9+
|
||||
- QDrant server running locally or accessible via network
|
||||
- OpenAI API key
|
||||
|
||||
## Installation
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/your-username/faq-bot.git
|
||||
cd faq-bot
|
||||
1. **Clone the repository**
|
||||
|
||||
# Create a virtual environment (optional but recommended)
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
||||
```bash
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-faq-bot-chromadb-odin.git
|
||||
cd povtornyy-ekzamen-faq-bot-chromadb-odin
|
||||
```
|
||||
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
2. **Create a virtual environment (optional but recommended)**
|
||||
|
||||
```bash
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||
```
|
||||
|
||||
3. **Install dependencies**
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
4. **Set environment variables**
|
||||
|
||||
Create a `.env` file in the project root or export the variables directly:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY="your-openai-api-key"
|
||||
export QDRANT_URL="http://localhost:6333" # Adjust if your QDrant instance is elsewhere
|
||||
export QDRANT_API_KEY="" # Leave empty if no auth is required
|
||||
export QDRANT_COLLECTION="faq_collection"
|
||||
```
|
||||
|
||||
If you prefer not to use a `.env` file, you can set the variables in your shell session.
|
||||
|
||||
## Usage
|
||||
|
||||
### 1. Ingest Data
|
||||
|
||||
Prepare a plain text file (`faq.txt`) containing your FAQ content. Then run:
|
||||
|
||||
```bash
|
||||
python src/main.py
|
||||
python src/index.py ingest faq.txt
|
||||
```
|
||||
|
||||
You will see a prompt:
|
||||
The script will:
|
||||
|
||||
```
|
||||
FAQ Bot is ready. Type your question (or 'exit' to quit).
|
||||
- Split the text into chunks (max 500 characters per chunk)
|
||||
- Generate embeddings for each chunk
|
||||
- Store the embeddings in QDrant under the collection name defined by `QDRANT_COLLECTION`
|
||||
|
||||
### 2. Query the Bot
|
||||
|
||||
```bash
|
||||
python src/index.py query "What is the return policy?"
|
||||
```
|
||||
|
||||
Type any of the predefined FAQ questions or any other question, and the bot will respond with the most relevant answer.
|
||||
You can adjust the number of results returned with `--top_k`:
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
faq-bot/
|
||||
├── src/
|
||||
│ └── main.py # Main application script
|
||||
├── requirements.txt # Python dependencies
|
||||
└── README.md # This file
|
||||
```bash
|
||||
python src.index.py query "What is the return policy?" --top_k 3
|
||||
```
|
||||
|
||||
## Customizing the FAQ
|
||||
### 3. Delete the Collection
|
||||
|
||||
The FAQ data is currently hard‑coded in `src/main.py`. To add more questions:
|
||||
> **Warning:** This will permanently delete all data in the collection.
|
||||
|
||||
1. Open `src/main.py`.
|
||||
2. Edit the `faq_pairs` list inside the `load_faq_data()` function.
|
||||
3. Restart the bot.
|
||||
```bash
|
||||
python src/index.py delete
|
||||
```
|
||||
|
||||
## Persistence
|
||||
### 4. Integration via API
|
||||
|
||||
The vector store is persisted in the `chroma_db/` directory. The next time you run the bot, it will reuse the existing embeddings instead of recomputing them.
|
||||
If you want to use the bot programmatically (e.g., from MCP-tool), import the `get_response` function:
|
||||
|
||||
```python
|
||||
from src.index import get_response
|
||||
|
||||
answer = get_response("How do I reset my password?")
|
||||
print(answer)
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **Ollama not found**: Ensure the Ollama server is running and accessible at `http://localhost:11434`.
|
||||
- **Embedding errors**: Verify that the `nomic-embed-text` model is pulled (`ollama list`).
|
||||
- **Vector store errors**: Delete the `chroma_db/` directory if you suspect corruption.
|
||||
- **QDrant Connection Errors**
|
||||
Ensure the QDrant server is running and reachable at the URL specified by `QDRANT_URL`. Check firewall settings if accessing remotely.
|
||||
|
||||
- **OpenAI API Errors**
|
||||
Verify that `OPENAI_API_KEY` is correct and has sufficient quota. Check the OpenAI dashboard for usage limits.
|
||||
|
||||
- **Large Documents**
|
||||
The ingestion script splits documents into 500‑character chunks. Adjust `max_chunk_size` in `split_text_into_chunks` if you need larger or smaller chunks.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
---
|
||||
This project is provided under the MIT License. Feel free to modify and extend it for your own use cases.
|
||||
|
||||
## Contact
|
||||
|
||||
For questions or support, contact Artur Kuzakhmetov at `artur@example.com`.
|
||||
Reference in New Issue
Block a user