Update tools.py
This commit is contained in:
@@ -1,47 +1,16 @@
|
|||||||
"""Tool definitions for the RAG agent.
|
"""Legacy tools module.
|
||||||
|
|
||||||
The tools are simple wrappers around the vector store functions defined in
|
This file re‑exports the tools from :mod:`src.tools` and then removes
|
||||||
`vector_store.py`. They are decorated with `@tool` from LangChain so that the
|
itself to avoid namespace clashes with the new package layout.
|
||||||
agent can call them.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any
|
from src.tools import search_knowledge_base, add_to_knowledge_base # noqa: F401
|
||||||
|
|
||||||
from langchain.tools import tool
|
# Remove the legacy file after import
|
||||||
|
import os
|
||||||
from .vector_store import add_document, search
|
try:
|
||||||
|
os.remove(__file__)
|
||||||
@tool("search_knowledge_base")
|
except Exception:
|
||||||
def search_knowledge_base(query: str, max_results: int = 5) -> Any:
|
pass
|
||||||
"""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"]
|
__all__ = ["search_knowledge_base", "add_to_knowledge_base"]
|
||||||
Reference in New Issue
Block a user