File size: 736 Bytes
6230b38
 
 
a4d9f42
6230b38
538b711
b2746c2
a4d9f42
 
b2746c2
a4d9f42
 
b2746c2
a4d9f42
b2746c2
0975dcb
538b711
0975dcb
 
a4d9f42
 
 
0975dcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from smolagents import tool
from duckduckgo_search import DDGS


@tool
def web_search(query: str) -> str:
    """
    Search the web for factual answers about the query using DuckDuckGo.

    Args:
        query (str): The question or search term to look up.

    Returns:
        str: Relevant information extracted from search results, preferably from Wikipedia.
    """
    refined = f"{query} site:en.wikipedia.org"
    with DDGS() as ddgs:
        results = ddgs.text(refined)
        for r in results[:5]:
            snippet = r.get("body") or r.get("content") or ""
            if snippet:
                return f"{snippet}\n\nSource: [{r['href']}]({r['href']})"
        return "Could not find a direct answer from Wikipedia."