feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'
This commit is contained in:
@@ -1,78 +1,81 @@
|
||||
# FAQ Bot – ChromaDB + Ollama
|
||||
# FAQ Bot – ChromaDB + Ollama Embeddings
|
||||
|
||||
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.
|
||||
This project implements a simple FAQ chatbot that uses **ChromaDB** as the vector store and **Ollama** for embeddings. The chatbot answers user questions by retrieving the most relevant FAQ entries and generating a response with an OpenAI LLM.
|
||||
|
||||
## Features
|
||||
|
||||
- **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
|
||||
- **Vector Store**: ChromaDB (persistent on disk)
|
||||
- **Embeddings**: Ollama `all-MiniLM-L6-v2` (or any other Ollama model)
|
||||
- **LLM**: OpenAI GPT-3.5-turbo (configurable)
|
||||
- **API**: FastAPI with `/ask` and `/add` endpoints
|
||||
|
||||
## Setup
|
||||
|
||||
1. **Clone the repository**
|
||||
1. **Clone the repository**
|
||||
|
||||
```bash
|
||||
git clone <repo-url>
|
||||
cd <repo-directory>
|
||||
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-faq-bot-chromadb-odin.git
|
||||
cd povtornyy-ekzamen-faq-bot-chromadb-odin
|
||||
```
|
||||
|
||||
2. **Create a virtual environment** (optional but recommended)
|
||||
2. **Create a virtual environment**
|
||||
|
||||
```bash
|
||||
python -m venv venv
|
||||
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||
python -m venv .venv
|
||||
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
||||
```
|
||||
|
||||
3. **Install dependencies**
|
||||
3. **Install dependencies**
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
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
|
||||
```
|
||||
4. **Set environment variables**
|
||||
|
||||
5. **Run the bot**
|
||||
```bash
|
||||
python src/main.py
|
||||
Create a `.env` file in the project root (or export variables manually):
|
||||
|
||||
```dotenv
|
||||
# ChromaDB
|
||||
CHROMA_DB_PATH=./chroma_db
|
||||
CHROMA_COLLECTION_NAME=faq_collection
|
||||
|
||||
# Ollama
|
||||
OLLAMA_EMBED_MODEL=all-MiniLM-L6-v2
|
||||
OLLAMA_HOST=http://localhost
|
||||
OLLAMA_PORT=11434
|
||||
|
||||
# OpenAI
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
OPENAI_MODEL=gpt-3.5-turbo
|
||||
```
|
||||
|
||||
Type your question in the console. Type `exit` or `quit` to stop.
|
||||
5. **Run the server**
|
||||
|
||||
## Project Structure
|
||||
```bash
|
||||
uvicorn src.main:app --reload
|
||||
```
|
||||
|
||||
```
|
||||
.
|
||||
├── requirements.txt
|
||||
├── src
|
||||
│ └── main.py
|
||||
└── README.md
|
||||
```
|
||||
The API will be available at `http://127.0.0.1:8000`.
|
||||
|
||||
- `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.
|
||||
## API Endpoints
|
||||
|
||||
| Method | Path | Description |
|
||||
|--------|-------|-------------|
|
||||
| `POST` | `/ask` | Ask a question. Body: `{ "question": "Your question" }`. Response: `{ "answer": "..." }`. |
|
||||
| `POST` | `/add` | Add a new FAQ entry. Body: `{ "text": "...", "metadata": { ... } }`. Response: `{ "status": "added" }`. |
|
||||
|
||||
## Adding FAQ Data
|
||||
|
||||
You can add FAQ entries via the `/add` endpoint or by modifying the code to load a dataset on startup. Each entry is stored as a `Document` in ChromaDB with optional metadata.
|
||||
|
||||
## Notes
|
||||
|
||||
- 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.
|
||||
- The vector store is persisted in the directory specified by `CHROMA_DB_PATH`. Deleting this directory will remove all stored vectors.
|
||||
- Ollama must be running locally and expose the embedding endpoint on the host/port specified.
|
||||
- The OpenAI LLM requires a valid API key.
|
||||
|
||||
## Troubleshooting
|
||||
## License
|
||||
|
||||
- **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.
|
||||
|
||||
Enjoy your FAQ bot!
|
||||
MIT License
|
||||
---
|
||||
Reference in New Issue
Block a user