feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'

This commit is contained in:
2026-07-01 11:01:06 +03:00
parent 2cf8df92ed
commit ddcd1f3423
5 changed files with 202 additions and 126 deletions
+58 -68
View File
@@ -1,102 +1,92 @@
# FAQ Bot ChromaDB + LangChain
# FAQ Bot ChromaDB + Ollama
This project implements a simple FAQ bot that uses **ChromaDB** for vector storage and **LangChain** as the single MCPtool to retrieve and generate answers.
The bot can ingest FAQ documents, store embeddings in ChromaDB, and answer user questions via a commandline interface.
This project implements a simple FAQ bot that uses **ChromaDB** as the vector store and **Ollama** for embeddings and language generation.
The bot is built with **LangChain** and relies on a single **MCPTool** to retrieve relevant documents and generate answers.
## Features
- **Vector storage** ChromaDB (DuckDB + Parquet backend)
- **Embedding model** OpenAI embeddings (`text-embedding-3-small`)
- **LLM** OpenAI Chat (`gpt-4o-mini` by default)
- **MCPtool** LangChain (only one MCPtool used)
- **CLI** `python -m src.main ingest|ask`
- **Unit tests** `pytest`
- **Embeddings**: Uses the `nomic-embed-text` model from Ollama.
- **Vector Store**: Stores embeddings in a persistent ChromaDB collection.
- **LLM**: Generates answers with the `llama3` model from Ollama.
- **MCPTool**: A single tool that handles retrieval and generation in one step.
- **CLI**: Interactive commandline interface for quick testing.
## Setup
1. **Clone the repository**
```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
```bash
git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-faq-bot-chromadb-odin-.git
cd povtornyy-ekzamen-faq-bot-chromadb-odin-
```
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\\Scripts\\activate
2. **Create a virtual environment**
```bash
python3 -m venv .venv
source .venv/bin/activate
```
3. **Install dependencies**
```bash
pip install -r requirements.txt
```
4. **Set OpenAI API key**
```bash
export OPENAI_API_KEY="sk-..."
```
## Usage
### Ingest FAQ file
Prepare a text file with FAQ pairs in the following format:
```
Q: What is Python?
A: Python is a programming language.
Q: What is ChromaDB?
A: ChromaDB is a vector database.
# Install dependencies
pip install -r requirements.txt
```
Run:
### Data
Place your FAQ documents as plain text files (`*.txt`) in the `data/` directory.
Each file will be loaded, embedded, and stored in ChromaDB.
## Running the Bot
```bash
python -m src.main ingest path/to/faq.txt --collection faq_collection
python -m src.main
```
### Ask a question
You will see a prompt:
```bash
python -m src.main ask "What is Python?" --collection faq_collection
```
FAQ Bot powered by ChromaDB and Ollama.
Type 'exit' to quit.
Your question:
```
The bot will print the generated answer.
Type a question and press Enter. The bot will return an answer.
## Testing
Run the test suite:
Run the unit tests to verify that the bot uses the correct components:
```bash
pytest
python -m unittest discover -s tests
```
All tests should pass, confirming that:
- The embeddings are from `OllamaEmbeddings`.
- The vector store is a `Chroma` instance.
- No OpenAI modules are imported.
- Answers are returned as strings.
## Project Structure
```
src/
├── main.py # CLI entry point
├── ingest.py # Ingestion logic
└── retriever.py # Retrieval & answer generation
tests/
── test_ingest.py
── test_retrieval.py
requirements.txt
README.md
├── data/ # FAQ documents (plain text)
├── chroma_db/ # Persisted ChromaDB collection
├── src/
│ └── main.py # Bot implementation
├── tests/
│ └── test_main.py # Unit tests
── requirements.txt
└── README.md
```
## Notes
- The bot uses the default OpenAI embeddings and LLM.
If you want to change the model, edit the `OpenAIEmbeddings()` and `OpenAIChat()` calls in `src/ingest.py` and `src/retriever.py`.
- ChromaDB data is persisted in the `chromadb/` directory relative to the project root.
- The deadline for the assignment is **31.08.2026**. All code is committed to the specified Git repository.
- The bot requires an Ollama server running locally.
Ensure that the `nomic-embed-text` and `llama3` models are available:
---
```bash
ollama pull nomic-embed-text
ollama pull llama3
```
Happy coding!
- The vector store is persisted in the `chroma_db/` directory.
If you add new documents, delete this folder and rerun the bot to rebuild the index.
Enjoy building your FAQ bot!