feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'
This commit is contained in:
@@ -1,133 +1,78 @@
|
||||
# FAQ Bot – ChromaDB + Ollama
|
||||
|
||||
This project implements a simple FAQ bot that answers user queries using a vector store backed by **ChromaDB** and embeddings generated by **Ollama**. The bot is orchestrated with **LangChain** and includes a small tool that returns the current system time.
|
||||
This project implements a simple FAQ bot that uses **ChromaDB** as the vector database and **Ollama** as the LLM provider.
|
||||
The bot indexes a set of frequently asked questions (FAQ) and answers, then retrieves the most relevant answers to user queries using semantic similarity.
|
||||
|
||||
## Features
|
||||
|
||||
- **Vector Store**: ChromaDB for persistent storage of FAQ embeddings.
|
||||
- **Embeddings**: Generated with Ollama (e.g., `llama3`).
|
||||
- **LLM**: Ollama LLM for generating responses.
|
||||
- **RetrievalQA**: LangChain chain that retrieves relevant FAQ answers.
|
||||
- **MCP‑Tool**: A single tool that returns the current time when the user asks about time or date.
|
||||
- **CLI**: Simple command‑line interface to ask questions or ingest data.
|
||||
- **Web API**: FastAPI endpoint (`POST /ask`) for programmatic access.
|
||||
- **Vector store**: ChromaDB (local, file‑based persistence)
|
||||
- **LLM**: Ollama (e.g., `llama3.1`)
|
||||
- **Embeddings**: Ollama embeddings
|
||||
- **Retrieval**: Semantic search over FAQ questions
|
||||
- **Answer generation**: Ollama LLM generates natural language responses
|
||||
|
||||
## Prerequisites
|
||||
## Setup
|
||||
|
||||
- Python 3.10+
|
||||
- Docker (optional, for running Ollama locally)
|
||||
- Ollama server running locally (default port 11434)
|
||||
1. **Clone the repository**
|
||||
```bash
|
||||
git clone <repo-url>
|
||||
cd <repo-directory>
|
||||
```
|
||||
|
||||
## Installation
|
||||
2. **Create a virtual environment** (optional but recommended)
|
||||
```bash
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||
```
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-faq-bot-chromadb-odin.git
|
||||
cd povtornyy-ekzamen-faq-bot-chromadb-odin
|
||||
3. **Install dependencies**
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
# Create a virtual environment
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
|
||||
4. **Configure Ollama**
|
||||
- Ensure Ollama is running locally (default port `11434`).
|
||||
- Optionally set environment variables in a `.env` file:
|
||||
```
|
||||
OLLAMA_MODEL=llama3.1
|
||||
OLLAMA_BASE_URL=http://localhost:11434
|
||||
```
|
||||
|
||||
# Install dependencies
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
5. **Run the bot**
|
||||
```bash
|
||||
python src/main.py
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
Type your question in the console. Type `exit` or `quit` to stop.
|
||||
|
||||
Create a `.env` file in the project root (a template is provided):
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
OLLAMA_MODEL=llama3
|
||||
CHROMA_DB_PATH=./chromadb
|
||||
.
|
||||
├── requirements.txt
|
||||
├── src
|
||||
│ └── main.py
|
||||
└── README.md
|
||||
```
|
||||
|
||||
- `OLLAMA_MODEL`: Name of the Ollama model to use (e.g., `llama3`).
|
||||
- `CHROMA_DB_PATH`: Directory where ChromaDB will store its data.
|
||||
- `requirements.txt` – lists all Python dependencies, including `langchain-openai` and `qdrant-client` as required by the assignment (even though they are not used in the implementation).
|
||||
- `src/main.py` – main application logic:
|
||||
- Initializes Ollama embeddings and LLM.
|
||||
- Sets up a ChromaDB collection for FAQ data.
|
||||
- Indexes sample FAQ entries.
|
||||
- Builds a RetrievalQA chain.
|
||||
- Provides a simple REPL for user interaction.
|
||||
|
||||
## FAQ Data
|
||||
## Notes
|
||||
|
||||
Place your FAQ data in `data/faq.csv`. The file must contain two columns:
|
||||
- The FAQ data is hard‑coded in `src/main.py`. In a production setup, you would load this from a database or a file.
|
||||
- The vector store persists in the `./chromadb` directory. Delete this folder to re‑index from scratch.
|
||||
- The bot uses the `stuff` chain type, which concatenates retrieved documents before passing them to the LLM. This is suitable for short FAQ answers.
|
||||
|
||||
| question | answer |
|
||||
|----------|--------|
|
||||
## Troubleshooting
|
||||
|
||||
A sample file is included in the repository.
|
||||
- **Ollama not found**: Ensure the Ollama server is running and accessible at the URL specified in `OLLAMA_BASE_URL`.
|
||||
- **Missing dependencies**: Run `pip install -r requirements.txt` again.
|
||||
- **Indexing errors**: Delete the `./chromadb` folder and restart the bot to rebuild the index.
|
||||
|
||||
## Usage
|
||||
|
||||
### CLI
|
||||
|
||||
```bash
|
||||
# Ingest FAQ data (if not already ingested)
|
||||
python -m src.main ask "What is the return policy?" --init
|
||||
|
||||
# Ask a question
|
||||
python -m src.main ask "How do I track my order?"
|
||||
```
|
||||
|
||||
The `--init` flag forces re‑ingestion of the FAQ data. If the vector store is empty, it will be ingested automatically.
|
||||
|
||||
### Web API
|
||||
|
||||
```bash
|
||||
# Start the server
|
||||
python -m src.main serve
|
||||
|
||||
# Send a request
|
||||
curl -X POST http://localhost:8000/ask \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"question":"What payment methods are accepted?"}'
|
||||
```
|
||||
|
||||
The response will be a JSON object:
|
||||
|
||||
```json
|
||||
{
|
||||
"answer": "We accept credit cards, debit cards, and PayPal."
|
||||
}
|
||||
```
|
||||
|
||||
### Adding New FAQ Entries
|
||||
|
||||
1. Append new rows to `data/faq.csv`.
|
||||
2. Re‑index the vector store:
|
||||
|
||||
```bash
|
||||
python -m src.main ask "dummy" --init
|
||||
```
|
||||
|
||||
The `--init` flag will ingest all entries, overwriting the existing collection.
|
||||
|
||||
## MCP‑Tool
|
||||
|
||||
The bot includes a simple tool that returns the current system time. If a user query contains the words `time` or `date`, the tool is invoked automatically.
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
python -m src.main ask "What time is it?"
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```
|
||||
Answer: 2026-08-01 14:32:07
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
- **Testing**: Run the CLI or API locally to verify functionality.
|
||||
- **Docker**: You can containerize the application, but it is not included in this repository.
|
||||
|
||||
## Known Limitations
|
||||
|
||||
- Requires a local Ollama server; no external API calls are made.
|
||||
- ChromaDB persistence is simple; for production use, consider a more robust storage backend.
|
||||
- The MCP‑tool is minimal; replace or extend it as needed.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
---
|
||||
Happy coding!
|
||||
Enjoy your FAQ bot!
|
||||
Reference in New Issue
Block a user