antonioschiro's picture
Create tools.py
bcb08ab verified
raw
history blame
810 Bytes
from langchain_core.tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_tavily import TavilySearch
import asyncio
import os
#from dotenv import load_dotenv
#load_dotenv()
os.environ["TAVILY_API_KEY"] = os.getenv("TAVILY_API_KEY")
@tool
async def websearch(query: str) -> str:
"""
Perform a web search using DuckDuckGo.
Args:
query (str): The search query string.
Returns:
str: The result of the web search as a string.
If an exception occurs, returns a fallback string indicating no results were found.
"""
search_engine = DuckDuckGoSearchRun()
try:
response = await search_engine.ainvoke(query)
return response
except:
return f"No results found on the web for this query: {query}."