File size: 783 Bytes
bcb08ab
 
bf37e54
bcb08ab
 
 
 
 
 
 
c87b65c
bcb08ab
 
 
 
 
 
 
 
 
 
 
 
319a6d6
bcb08ab
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from langchain_core.tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
#from langchain_tavily import TavilySearch
import os
#from dotenv import load_dotenv
#load_dotenv()

os.environ["TAVILY_API_KEY"] = os.getenv("TAVILY_API_KEY")

@tool
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 = search_engine.invoke(query)
        return response
    except:
        return f"No results found on the web for this query: {query}."