Files
task-6a02e23da6fe2e4ac16acf65/tools.py
T
2026-06-04 20:03:08 +00:00

47 lines
1.4 KiB
Python

"""Tool definitions for the RAG agent.
The tools are simple wrappers around the vector store functions defined in
`vector_store.py`. They are decorated with `@tool` from LangChain so that the
agent can call them.
"""
from typing import Any
from langchain.tools import tool
from .vector_store import add_document, search
@tool("search_knowledge_base")
def search_knowledge_base(query: str, max_results: int = 5) -> Any:
"""Semantic search in the knowledge base.
Parameters
----------
query: str
The user query.
max_results: int, optional
Number of top results to return. Defaults to 5.
"""
results = search(query, max_results)
# Convert results to a readable string
formatted = "\n".join(
f"{i+1}. [{res['metadata'].get('title', 'Unknown')}] {res['content'][:200]}"
for i, res in enumerate(results)
)
return formatted if formatted else "No relevant documents found."
@tool("add_to_knowledge_base")
def add_to_knowledge_base(content: str, title: str) -> Any:
"""Add a new document to the knowledge base.
Parameters
----------
content: str
The full text of the document.
title: str
A short title that will be stored as metadata.
"""
add_document(content, title)
return f"Document '{title}' added to the knowledge base."
__all__ = ["search_knowledge_base", "add_to_knowledge_base"]