Create tools.py
Browse files
tools.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 3 |
+
from langchain_tavily import TavilySearch
|
| 4 |
+
import asyncio
|
| 5 |
+
import os
|
| 6 |
+
#from dotenv import load_dotenv
|
| 7 |
+
#load_dotenv()
|
| 8 |
+
|
| 9 |
+
os.environ["TAVILY_API_KEY"] = os.getenv("TAVILY_API_KEY")
|
| 10 |
+
|
| 11 |
+
@tool
|
| 12 |
+
async def websearch(query: str) -> str:
|
| 13 |
+
"""
|
| 14 |
+
Perform a web search using DuckDuckGo.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
query (str): The search query string.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
str: The result of the web search as a string.
|
| 21 |
+
If an exception occurs, returns a fallback string indicating no results were found.
|
| 22 |
+
"""
|
| 23 |
+
search_engine = DuckDuckGoSearchRun()
|
| 24 |
+
try:
|
| 25 |
+
response = await search_engine.ainvoke(query)
|
| 26 |
+
return response
|
| 27 |
+
except:
|
| 28 |
+
return f"No results found on the web for this query: {query}."
|