From 1ab4284940461de04c78d0c29d775710f7f7c0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B4=D0=B5=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A1=D0=B0?= =?UTF-8?q?=D1=82=D1=82=D0=B0=D1=80=D0=BE=D0=B2=D0=B0?= Date: Thu, 28 May 2026 14:31:26 +0000 Subject: [PATCH] add tools --- tools.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tools.py diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..07c0ec6 --- /dev/null +++ b/tools.py @@ -0,0 +1,29 @@ +from langchain.tools import tool +from qdrant_client import QdrantClient +from langchain.embeddings.ollama import OllamaEmbeddings +from langchain.text_splitter import RecursiveCharacterTextSplitter +import os + +# Initialize Qdrant client and collection +qdrant_url = os.getenv("QDRANT_URL", "http://localhost:6333") +client = QdrantClient(url=qdrant_url) +collection_name = "knowledge_base" +if not client.has_collection(collection_name): + client.create_collection(name=collection_name, vectors_config={"size": 384, "distance": "Cosine"}) + +embeddings = OllamaEmbeddings(model="nomic-embed-text") +text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) + +@tool("search_knowledge_base", description="Semantic search in knowledge base") +def search_knowledge_base(query: str, max_results: int = 5): + vector = embeddings.embed_query(query) + results = client.search(collection_name=collection_name, query_vector=vector, limit=max_results) + return [hit.payload["text"] for hit in results] + +@tool("add_to_knowledge_base", description="Add document to knowledge base") +def add_to_knowledge_base(content: str, title: str): + docs = text_splitter.split_text(content) + vectors = embeddings.embed_documents(docs) + ids = [title + f"_{i}" for i in range(len(docs))] + client.upsert(collection_name=collection_name, points=[{"id": id_, "vector": vec, "payload": {"text": doc}} for id_, vec, doc in zip(ids, vectors, docs)]) + return f"Added {len(docs)} chunks to knowledge base"