feat: solution for 'Агент с RAG-памятью'
CI / build (push) Has been cancelled

This commit is contained in:
2026-07-01 14:05:59 +03:00
parent bd49075b6e
commit 39d55136ad
14 changed files with 332 additions and 406 deletions
+54 -47
View File
@@ -1,63 +1,70 @@
**What was implemented**
- Switched the vector store from FAISS to Qdrant using the `langchain_qdrant` wrapper.
- Replaced `OpenAIEmbeddings` with `OllamaEmbeddings` from `langchain_ollama`.
- Updated the agent to use Ollama for both embeddings and the LLM.
- Added `langchain-qdrant` and `langchain-ollama` to `requirements.txt`.
- Adjusted configuration to point to a local Qdrant instance and an Ollama model.
**What was implemented**
**Why the main parts satisfy the requirements**
- `src/vector_store.py` now imports `langchain_qdrant.Qdrant` and passes the Ollama embeddings, fulfilling the “use langchainqdrant” constraint.
- `src/agent.py` constructs the RetrievalQA chain with an Ollama LLM and the Qdrant retriever, meeting the “use Ollama embeddings” and “Qdrant as RAG memory” constraints.
- `config.py` centralises Qdrant and Ollama settings, so the rest of the code stays clean and configurable.
- `requirements.txt` lists both `langchain-qdrant` and `langchain-ollama`, removing any OpenAI/FAISS dependencies.
- Switched the embedding provider from `OpenAIEmbeddings` to `OllamaEmbeddings` (langchaincommunity).
- Replaced the FAISS vector store with a Qdrant store.
- Updated all imports, configuration, and helper functions to use the new stack.
- Added the required dependencies (`langchain-community`, `qdrant-client`) to `requirements.txt`.
- Kept the LLM (`OpenAI`), prompt templates, chain structure, and memory unchanged.
- Provided localhost configuration for both Ollama and Qdrant in `config.py`.
**Why the main parts satisfy the requirements**
- **Embeddings** `embeddings.py` now returns an `OllamaEmbeddings` instance that talks to a local Ollama server (`base_url=f"{OLLAMA_HOST}:{OLLAMA_PORT}"`).
- **Vector store** `vector_store.py` creates a `QdrantClient`, ensures the collection exists, and returns a `Qdrant` vector store wired to the Ollama embeddings.
- **Agent** `agent.py` builds a `RetrievalQA` chain that uses the Qdrant retriever, the same OpenAI LLM, and a conversation buffer memory.
- **Configuration** `config.py` exposes host/port for both services, so the agent can connect to local instances without hardcoding URLs.
- **Dependencies** `requirements.txt` now lists `langchain-community` and `qdrant-client`, satisfying the “add dependencies” requirement.
**Key code excerpts**
`config.py` Qdrant & Ollama settings
```python
# Qdrant settings
QDRANT_HOST = "localhost"
QDRANT_PORT = 6333
QDRANT_API_KEY = None
QDRANT_COLLECTION = "rag_collection"
`embeddings.py` Ollama embeddings
# Ollama settings
OLLAMA_MODEL = "llama3"
```python
from langchain_community.embeddings import OllamaEmbeddings
...
return OllamaEmbeddings(
model=OLLAMA_EMBEDDING_MODEL,
base_url=f"{OLLAMA_HOST}:{OLLAMA_PORT}"
)
```
`src/vector_store.py` Qdrant wrapper
`vector_store.py` Qdrant store
```python
class QdrantVectorStore:
def __init__(self, embeddings, collection_name: str = None):
self.qdrant = Qdrant(
url=f"http://{config.QDRANT_HOST}:{config.QDRANT_PORT}",
api_key=config.QDRANT_API_KEY,
collection_name=self.collection_name,
embeddings=embeddings,
)
from qdrant_client import QdrantClient
...
return Qdrant(
client=client,
collection_name=QDRANT_COLLECTION_NAME,
embeddings=embeddings
)
```
`src/agent.py` RetrievalQA with Ollama
`agent.py` RetrievalQA chain unchanged except for the retriever
```python
def create_agent(vector_store: QdrantVectorStore):
embeddings = OllamaEmbeddings(model=config.OLLAMA_MODEL)
llm = Ollama(model=config.OLLAMA_MODEL)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vector_store.get_retriever(),
)
return qa_chain
vector_store: Qdrant = get_vector_store()
retriever = vector_store.as_retriever(search_kwargs={"k": 5})
...
chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
memory=memory
)
```
`src/main.py` initialization and sample run
`config.py` localhost settings
```python
vector_store = QdrantVectorStore(embeddings)
agent = create_agent(vector_store)
result = agent.run("What is LangChain?")
OLLAMA_HOST: str = "http://localhost"
OLLAMA_PORT: int = 11434
QDRANT_HOST: str = "http://localhost"
QDRANT_PORT: int = 6333
```
**Honest limitations**
- The solution assumes a running Qdrant instance on `localhost:6333` and an Ollama model named `llama3` available locally.
- No error handling for connection failures is added; in production youd want to wrap Qdrant/ollama calls in try/except blocks.
- The sample documents are added only if the collection is empty; this logic is simplistic but sufficient for demonstration.
**Honest limitations**
- The solution assumes a running local Ollama server exposing the chosen embedding model (`llama2`) and a Qdrant instance listening on the default ports.
- The vector size is hardcoded to 768; if the chosen Ollama model uses a different dimensionality, the collection creation will need adjustment.
- No automated tests were executed; the changes are based on the provided project structure and should satisfy the functional requirements.