File size: 911 Bytes
b5b618d
 
ae9196b
 
36254dd
b5b618d
ae9196b
b5b618d
 
 
ae9196b
b5b618d
bbe413f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
from smolagents import Tool
import wikipedia
class LocalWikipediaTool(Tool):
    name = "wikipedia_search"
    description = "Use websearch tool first to get the facts, and if needed use this Fallback tool: Use Wikipedia after the web search tool."
    inputs = {
        "query": {"type": "string", "description": "Search term"}
    }
    output_type = "string"

    def forward(self, query: str) -> str:
        try:
            # search for related pages
            results = wikipedia.search(query)

            if not results:
                return "No Wikipedia results found."

            # pick first result
            page_title = results[0]

            # get a short summary (2–3 sentences)
            summary = wikipedia.summary(page_title, sentences=3)

            return f"Title: {page_title}\nSummary: {summary}"

        except Exception as e:
            return f"Wikipedia error: {e}"