From a7c4e53bffe77e9b84f6a029df943cf0774539c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B8=D1=8F=20=D0=91=D0=B5=D1=80=D0=B4?= =?UTF-8?q?=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=D0=B0?= Date: Wed, 27 May 2026 07:01:11 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20solutions/6a02e23da6fe2e4ac16acf65=5F=D0=90=D0=B3?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=5F=D1=81=5FRAG=5F=D0=BF=D0=B0=D0=BC=D1=8F?= =?UTF-8?q?=D1=82=D1=8C=D1=8E/rag=5Ftools.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rag_tools.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 solutions/6a02e23da6fe2e4ac16acf65_Агент_с_RAG_памятью/rag_tools.py diff --git a/solutions/6a02e23da6fe2e4ac16acf65_Агент_с_RAG_памятью/rag_tools.py b/solutions/6a02e23da6fe2e4ac16acf65_Агент_с_RAG_памятью/rag_tools.py new file mode 100644 index 0000000..468a1ab --- /dev/null +++ b/solutions/6a02e23da6fe2e4ac16acf65_Агент_с_RAG_памятью/rag_tools.py @@ -0,0 +1,41 @@ +# rag_tools.py +""" +Инструменты для LangChain-агента. Используют QdrantClient из qdrant_client.py. +""" +from langchain.tools import tool +from qdrant_client import QdrantClient + +_store = QdrantClient() + +@tool +def search_knowledge_base(query: str, max_results: int = 5) -> list: + """ + Семантический поиск в базе знаний. + + Args: + query: поисковый запрос. + max_results: максимальное количество результатов. + + Returns: + Список найденных фрагментов с оценкой релевантности. + """ + results = _store.search(query, limit=max_results) + return [ + {"title": r["metadata"].get("title", ""), "content": r["text"], "score": r["score"]} + for r in results + ] + +@tool +def add_to_knowledge_base(content: str, title: str) -> str: + """ + Добавляет новый документ в базу знаний. + + Args: + content: полный текст документа. + title: заголовок документа. + + Returns: + Сообщение об успехе. + """ + _store.add_documents([content], [title]) + return f"Документ '{title}' успешно добавлен в базу знаний." \ No newline at end of file