55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
"""Tavily Search Tool for LangGraph.
|
|
|
|
This tool provides web search capabilities using the Tavily API.
|
|
"""
|
|
|
|
import logging
|
|
from typing import Annotated
|
|
from langchain_core.tools import tool
|
|
from modules.connectors.connectorAiTavily import ConnectorWeb
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@tool
|
|
async def tavily_search(
|
|
query: Annotated[str, "The search query to look up on the web"],
|
|
) -> str:
|
|
"""Search the web using Tavily API.
|
|
|
|
Use this tool to search for current information, news, or any web content.
|
|
The tool returns relevant search results including titles and URLs.
|
|
|
|
Args:
|
|
query: The search query string
|
|
|
|
Returns:
|
|
A formatted string containing search results with titles and URLs
|
|
"""
|
|
try:
|
|
# Create connector instance
|
|
connector = await ConnectorWeb.create()
|
|
|
|
# Perform search with default parameters
|
|
results = await connector._search(
|
|
query=query,
|
|
max_results=5,
|
|
search_depth="basic",
|
|
include_answer=True,
|
|
include_raw_content=False,
|
|
)
|
|
|
|
# Format results
|
|
if not results:
|
|
return f"No results found for query: {query}"
|
|
|
|
formatted_results = [f"Search results for '{query}':\n"]
|
|
for i, result in enumerate(results, 1):
|
|
formatted_results.append(f"{i}. {result.title}")
|
|
formatted_results.append(f" URL: {result.url}\n")
|
|
|
|
return "\n".join(formatted_results)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error in tavily_search tool: {str(e)}")
|
|
return f"Error performing search: {str(e)}"
|