From e34bbb8aa1a24cfc045fa420f73d9c2e7b8684c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B8=D0=BD=D0=B0=D1=80=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D0=B8=D1=82=D0=BE=D0=B2?= Date: Thu, 11 Jun 2026 09:16:23 +0000 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=20=D0=BA=20=D0=BF=D1=83?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D0=B8:=20add=20tools?= =?UTF-8?q?.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tools.py diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..b4ff16c --- /dev/null +++ b/tools.py @@ -0,0 +1,41 @@ +""" +Agent tools for local KB search and web search via Tavily. +""" + +from typing import List, Dict + +from langchain.agents import tool +from langchain_tavily import TavilySearch +from langchain.schema.document import Document + +# Local KB search tool +@tool("search_local_kb") +def search_local_kb(query: str, top_k: int = 3) -> List[Dict]: + """Semantic search in the local ChromaDB collection. + + Returns a list of dicts with keys ``text`` and ``source``. + """ + from vectorstore import create_vectorstore + + # Assume the collection is already created and persisted + store = create_vectorstore() + results = store.query(query, top_k=top_k) + return [ + {"text": doc.page_content, "source": doc.metadata.get("source", "unknown")} + for doc in results + ] + +# Web search tool using Tavily. +@tool("web_search") +def web_search(query: str) -> List[Dict]: + """Search the web with Tavily and return a list of result snippets. + + Requires environment variable TAVILY_API_KEY. + """ + tav = TavilySearch() + results = tav.search(query, max_results=5) + return [ + {"text": r["title"] + ": " + r["snippet"] if isinstance(r, dict) else str(r)} + for r in + (results if isinstance(results, list) else []) + ]