Update tools.py
This commit is contained in:
@@ -1,51 +1,47 @@
|
|||||||
"""Tools for the RAG agent.
|
"""Tool definitions for the RAG agent.
|
||||||
|
|
||||||
Two tools are defined:
|
The tools are simple wrappers around the vector store functions defined in
|
||||||
1. search_knowledge_base – performs semantic search in the vector store.
|
`vector_store.py`. They are decorated with `@tool` from LangChain so that the
|
||||||
2. add_to_knowledge_base – adds a new document to the vector store.
|
agent can call them.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import List
|
from typing import Any
|
||||||
|
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from langchain.schema import Document
|
|
||||||
|
|
||||||
from vector_store import store
|
|
||||||
|
|
||||||
|
from .vector_store import add_document, search
|
||||||
|
|
||||||
@tool("search_knowledge_base")
|
@tool("search_knowledge_base")
|
||||||
async def search_knowledge_base(query: str, max_results: int = 5) -> List[Document]:
|
def search_knowledge_base(query: str, max_results: int = 5) -> Any:
|
||||||
"""Search the knowledge base for relevant chunks.
|
"""Semantic search in the knowledge base.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
query: str
|
query: str
|
||||||
The search query.
|
The user query.
|
||||||
max_results: int
|
max_results: int, optional
|
||||||
Number of top results to return.
|
Number of top results to return. Defaults to 5.
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
List[Document]
|
|
||||||
List of documents returned by Qdrant similarity search.
|
|
||||||
"""
|
"""
|
||||||
return store.search(query, max_results)
|
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")
|
@tool("add_to_knowledge_base")
|
||||||
async def add_to_knowledge_base(content: str, title: str) -> str:
|
def add_to_knowledge_base(content: str, title: str) -> Any:
|
||||||
"""Add a new document to the knowledge base.
|
"""Add a new document to the knowledge base.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
content: str
|
content: str
|
||||||
Full text of the document.
|
The full text of the document.
|
||||||
title: str
|
title: str
|
||||||
Title or identifier for the document.
|
A short title that will be stored as metadata.
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
str
|
|
||||||
Confirmation message.
|
|
||||||
"""
|
"""
|
||||||
store.add_document(content, title)
|
add_document(content, title)
|
||||||
return f"Document '{title}' added to the knowledge base."
|
return f"Document '{title}' added to the knowledge base."
|
||||||
|
|
||||||
|
__all__ = ["search_knowledge_base", "add_to_knowledge_base"]
|
||||||
Reference in New Issue
Block a user