From 61170d52342e02df1055242ddf3b155ffb250a48 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: Sun, 31 May 2026 16:21:17 +0000 Subject: [PATCH] Add tools.py --- tools.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tools.py diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..bc83a41 --- /dev/null +++ b/tools.py @@ -0,0 +1,37 @@ +""" +Tools for the RAG agent. + +- search_local_kb(query, top_k) +- web_search(query) +""" +import os +from typing import List + +from langchain_ollama import OllamaEmbeddings +from langchain_chroma import Chroma +from langchain_tavily import TavilySearchResults +from langchain_core.documents import Document + +# Global vectorstore – will be set in init_db or main +vectorstore: Chroma = None + +def search_local_kb(query: str, top_k: int = 3) -> List[Document]: + """Semantic search in the local ChromaDB.""" + if vectorstore is None: + raise RuntimeError("Vectorstore not initialized") + return vectorstore.similarity_search_with_score(query, k=top_k) + +# Tavily client – API key from env +from tavily import TavilyClient +import os +TAVILY_API_KEY = os.getenv("TAVILY_API_KEY", "") +client = TavilyClient(api_key=TAVILY_API_KEY) + +def web_search(query: str, max_results: int = 3) -> List[Document]: + """Search the web via Tavily and return Documents.""" + results = client.search(query=query, max_results=max_results) + docs = [] + for r in results: + content = f"{r.title}\n\n{r.content}" + docs.append(Document(page_content=content, metadata={"source": "tavily", "url": r.url})) + return docs