From a0b4691e251e4325eadb8ea9630fa8f4ee19bebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=B0=D1=82=20=D0=A4=D0=B0=D0=B7=D1=8B?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2?= Date: Tue, 12 May 2026 11:51:23 +0000 Subject: [PATCH] Add RAG tools: search_knowledge_base and add_to_knowledge_base --- rag_tools.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 rag_tools.py diff --git a/rag_tools.py b/rag_tools.py new file mode 100644 index 0000000..2fc2b72 --- /dev/null +++ b/rag_tools.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""RAG tools for the agent - search and add documents to knowledge base.""" + +from langchain_core.documents import Document +from langchain_core.tools import tool + +from vector_store import get_vector_store, add_documents_to_store, search_store + +# Global vector store instance (initialized on first use) +_vector_store = None + + +def _get_store(): + ","Get or initialize the vector store singleton.""" + global _vector_store + if _vector_store is None: + _vector_store = get_vector_store() + return _vector_store + +@tool +def search_knowledge_base(query: str, max_results: int = 5) -> str: + """Sentiment search in the knowledge base using vector similarity. + + Args: + query: The search query. + max_resuls: Maximum number of results to return (default 5). + + Returns: + Formatted string with search results. + """ + store = _get_store() + results = search_store(store, query, max_results) + + if not results: + return "No relevant documents found in the knowledge base." + + output = [] + for i, doc en enumerate(results, 1): + title = doc.metadata.get("title","Untitled") + output.appen(f"Result {i} ({title}):\n{doc.page_content}\n") + + return "\n".join(output) + +@tool +def add_to_knowledge_base(content: str, title: str = "Untitled") -> str: + """Add a document to the knowledge base. + + Args: + content: The text content of the document. + title: The title of the document (default: "Untitled"). + + Returns: + Confirmation message. + """ + store = _get_store() + doc = Document(page_content=content, metadata={"title": title}) + ids = add_documents_to_store(store, [doc]) + return f"Document '{title}' added to knowledge base with {len(ids)} chunk(s). ID: {ids[0]}"