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

This commit is contained in:
2026-07-01 13:17:56 +03:00
parent ddcd1f3423
commit e7197dd952
4 changed files with 197 additions and 149 deletions
+42 -31
View File
@@ -1,43 +1,54 @@
**SOLUTION.md**
**What was implemented**
- FAQ bot that loads plaintext FAQ files, creates embeddings with **Ollama** model *nomicembedtext*, 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`.
- Switched from OpenAI embeddings/LLM to Ollamas `nomic-embed-text` for vector generation.
- Replaced the nonexistent `QdrantVectorStore` with a persistent ChromaDB store (`langchain.vectorstores.Chroma`).
- Added the missing dependencies `langchain-community` and `langchain-ollama` to `requirements.txt`.
- Updated the bot to use the Ollama model for both embeddings and text generation (`llama3`).
- Kept the interactive FAQ loop and retrievalQA chain intact.
**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 nonexistent Qdrant store.
- **Single MCPtool**: `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`.
**Why the main parts satisfy the requirements**
- **Embeddings**: `OllamaEmbeddings(model="nomic-embed-text")` guarantees the required Ollama model is used.
- **Vector store**: `Chroma` is imported from `langchain.vectorstores` and wrapped around a persistent Chroma client, fulfilling the ChromaDB constraint.
- **Dependencies**: `requirements.txt` now lists `langchain-community` and `langchain-ollama`, ensuring the environment can install the needed packages.
- **LLM**: The generation step uses `Ollama(model="llama3")`, an Ollama model, keeping the entire pipeline within the specified ecosystem.
**Key code excerpts**
**Key code excerpts**
*src/main.py imports and vector store creation*
*src/main.py embeddings and vector store*
```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
...
# 1. Set up embeddings using Ollama's "nomic-embed-text" model
embeddings = OllamaEmbeddings(model="nomic-embed-text")
vectorstore = Chroma.from_documents(
documents,
embeddings,
persist_directory=str(CHROMA_DIR),
```
```python
def create_vectorstore(embeddings, persist_directory: str = "chroma_db") -> Chroma:
...
vectorstore = Chroma(
client=client,
collection_name="faq",
embedding_function=embeddings
)
return vectorstore
```
*src/main.py retrievalQA chain*
```python
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever()
)
```
*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)
*requirements.txt* (excerpt)
```
langchain-community
langchain-ollama
```
**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 hardcoded to `chroma_db`; changing it requires editing the source.
**Limitations**
- The bot currently uses a hardcoded FAQ list; adding dynamic data sources would require further changes.
- Error handling around the vector store is minimal; in a production setting more robust checks would be advisable.
Overall, the solution meets all constraints: it uses Ollamas *nomicembedtext*, ChromaDB, a single MCPtool, and no OpenAI components.
This implementation meets all assignment constraints while keeping the original interactive FAQ functionality.