feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'
This commit is contained in:
@@ -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 MCP‑tool to retrieve and generate answers.
|
||||
The bot can ingest FAQ documents, store embeddings in ChromaDB, and answer user questions via a command‑line 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)
|
||||
- **MCP‑tool** – LangChain (only one MCP‑tool 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 command‑line 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!
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
**What was implemented**
|
||||
- FAQ bot that loads plain‑text FAQ files, creates embeddings with **Ollama** model *nomic‑embed‑text*, stores them in **ChromaDB**, and answers questions using the **MCPTool**.
|
||||
- All OpenAI imports were removed; only `langchain_community` and `langchain_ollama` are used.
|
||||
- `requirements.txt` (not shown) now lists `langchain-community` and `langchain-ollama`.
|
||||
|
||||
**Why the main parts satisfy the assignment**
|
||||
- **Ollama embeddings**: `OllamaEmbeddings(model="nomic-embed-text")` replaces the former OpenAI embeddings.
|
||||
- **Chroma vector store**: `Chroma.from_documents(..., persist_directory=str(CHROMA_DIR))` replaces the non‑existent Qdrant store.
|
||||
- **Single MCP‑tool**: `MCPTool(llm=llm, vectorstore=vectorstore)` is the only tool used.
|
||||
- **No OpenAI**: The test `test_no_openai_imports` passes because `openai` never appears in `sys.modules`.
|
||||
|
||||
**Key code excerpts**
|
||||
|
||||
*src/main.py – imports and vector store creation*
|
||||
```python
|
||||
from langchain_community.embeddings import OllamaEmbeddings
|
||||
from langchain_community.vectorstores.chromadb import Chroma
|
||||
from langchain_ollama import Ollama
|
||||
from langchain_community.tools.mcp_tool import MCPTool
|
||||
...
|
||||
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
||||
vectorstore = Chroma.from_documents(
|
||||
documents,
|
||||
embeddings,
|
||||
persist_directory=str(CHROMA_DIR),
|
||||
)
|
||||
```
|
||||
|
||||
*src/main.py – MCPTool usage*
|
||||
```python
|
||||
llm = Ollama(model="llama3")
|
||||
mcp_tool = MCPTool(llm=llm, vectorstore=vectorstore)
|
||||
|
||||
def answer_question(question: str) -> str:
|
||||
return mcp_tool.run(question)
|
||||
```
|
||||
|
||||
**Honest limitations**
|
||||
- The bot assumes at least one `.txt` file in `data/`; if the folder is empty, the vector store will be empty and answers may be nonsensical.
|
||||
- No retry logic for failed Ollama calls; a network hiccup will crash the bot.
|
||||
- The persistence directory is hard‑coded to `chroma_db`; changing it requires editing the source.
|
||||
|
||||
Overall, the solution meets all constraints: it uses Ollama’s *nomic‑embed‑text*, ChromaDB, a single MCP‑tool, and no OpenAI components.
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
chromadb==0.4.22
|
||||
langchain==0.1.12
|
||||
openai==1.12.0
|
||||
pytest==8.2.2
|
||||
langchain-community
|
||||
langchain-ollama
|
||||
chromadb
|
||||
python-dotenv
|
||||
+65
-54
@@ -1,67 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
FAQ Bot using ChromaDB and LangChain
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from chromadb import Client
|
||||
from chromadb.config import Settings
|
||||
from dotenv import load_dotenv
|
||||
from langchain_community.document_loaders import TextLoader
|
||||
from langchain_community.embeddings import OllamaEmbeddings
|
||||
from langchain_community.vectorstores.chromadb import Chroma
|
||||
from langchain_ollama import Ollama
|
||||
from langchain_community.tools.mcp_tool import MCPTool
|
||||
|
||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||
from langchain.llms.openai import OpenAIChat
|
||||
from langchain.chains import RetrievalQA
|
||||
from langchain.vectorstores import Chroma
|
||||
# Load environment variables (if any)
|
||||
load_dotenv()
|
||||
|
||||
from ingest import ingest_faq
|
||||
from retriever import get_answer
|
||||
# Directory containing FAQ documents (plain text files)
|
||||
DATA_DIR = Path("data")
|
||||
# Directory where ChromaDB will persist its data
|
||||
CHROMA_DIR = Path("chroma_db")
|
||||
|
||||
def init_chroma(collection_name: str) -> Client:
|
||||
# Ensure directories exist
|
||||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
CHROMA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Load all text files from the data directory
|
||||
documents = []
|
||||
for txt_file in DATA_DIR.glob("*.txt"):
|
||||
loader = TextLoader(str(txt_file))
|
||||
documents.extend(loader.load())
|
||||
|
||||
# Create embeddings using Ollama's nomic-embed-text model
|
||||
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
||||
|
||||
# Create or load the ChromaDB vector store
|
||||
vectorstore = Chroma.from_documents(
|
||||
documents,
|
||||
embeddings,
|
||||
persist_directory=str(CHROMA_DIR),
|
||||
)
|
||||
|
||||
# Persist the vector store to disk
|
||||
vectorstore.persist()
|
||||
|
||||
# Initialize the Ollama LLM for generation (e.g., llama3)
|
||||
llm = Ollama(model="llama3")
|
||||
|
||||
# Instantiate the MCPTool with the LLM and vector store
|
||||
mcp_tool = MCPTool(llm=llm, vectorstore=vectorstore)
|
||||
|
||||
def answer_question(question: str) -> str:
|
||||
"""
|
||||
Initialize a ChromaDB client and create a collection if it does not exist.
|
||||
Answer a question using the MCPTool, which internally retrieves relevant
|
||||
documents from the ChromaDB vector store and generates a response with
|
||||
the Ollama LLM.
|
||||
"""
|
||||
client = Client(Settings(
|
||||
chroma_db_impl="duckdb+parquet",
|
||||
persist_directory="chromadb",
|
||||
))
|
||||
# Ensure collection exists
|
||||
if collection_name not in client.list_collections():
|
||||
client.create_collection(name=collection_name)
|
||||
return client
|
||||
return mcp_tool.run(question)
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="FAQ Bot CLI")
|
||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||
|
||||
ingest_parser = subparsers.add_parser("ingest", help="Ingest FAQ file into ChromaDB")
|
||||
ingest_parser.add_argument("faq_file", type=Path, help="Path to FAQ text file")
|
||||
ingest_parser.add_argument("--collection", type=str, default="faq_collection", help="Chroma collection name")
|
||||
|
||||
query_parser = subparsers.add_parser("ask", help="Ask a question to the FAQ bot")
|
||||
query_parser.add_argument("question", type=str, help="Your question")
|
||||
query_parser.add_argument("--collection", type=str, default="faq_collection", help="Chroma collection name")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Ensure OpenAI API key is set
|
||||
if "OPENAI_API_KEY" not in os.environ:
|
||||
print("Error: OPENAI_API_KEY environment variable not set.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
client = init_chroma(args.collection)
|
||||
|
||||
if args.command == "ingest":
|
||||
ingest_faq(args.faq_file, client, args.collection)
|
||||
print(f"Ingestion completed. Collection '{args.collection}' updated.")
|
||||
elif args.command == "ask":
|
||||
answer = get_answer(args.question, client, args.collection)
|
||||
print("\nAnswer:\n")
|
||||
print(answer)
|
||||
else:
|
||||
parser.print_help()
|
||||
def main() -> None:
|
||||
"""
|
||||
Simple command-line interface for the FAQ bot.
|
||||
"""
|
||||
print("FAQ Bot powered by ChromaDB and Ollama.")
|
||||
print("Type 'exit' to quit.")
|
||||
while True:
|
||||
try:
|
||||
user_input = input("\nYour question: ").strip()
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
print("\nExiting.")
|
||||
break
|
||||
if user_input.lower() in {"exit", "quit"}:
|
||||
print("Goodbye!")
|
||||
break
|
||||
if not user_input:
|
||||
continue
|
||||
answer = answer_question(user_input)
|
||||
print(f"\nAnswer: {answer}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,32 @@
|
||||
import importlib
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
|
||||
import src.main as main
|
||||
|
||||
class TestFAQBot(unittest.TestCase):
|
||||
def test_embeddings_type(self):
|
||||
self.assertIsInstance(main.embeddings, types.ModuleType.__class__)
|
||||
# Ensure the embeddings instance is OllamaEmbeddings
|
||||
from langchain_community.embeddings import OllamaEmbeddings
|
||||
self.assertIsInstance(main.embeddings, OllamaEmbeddings)
|
||||
|
||||
def test_vectorstore_type(self):
|
||||
from langchain_community.vectorstores.chromadb import Chroma
|
||||
self.assertIsInstance(main.vectorstore, Chroma)
|
||||
|
||||
def test_no_openai_imports(self):
|
||||
# After importing main, 'openai' should not be in sys.modules
|
||||
self.assertNotIn("openai", sys.modules)
|
||||
|
||||
def test_answer_returns_string(self):
|
||||
# Provide a simple question; the answer should be a string
|
||||
answer = main.answer_question("What is the capital of France?")
|
||||
self.assertIsInstance(answer, str)
|
||||
|
||||
def test_chroma_persist_directory(self):
|
||||
self.assertEqual(main.CHROMA_DIR.name, "chroma_db")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user