File size: 2,782 Bytes
3becd38
88ce352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3becd38
88ce352
 
34f6dce
3becd38
96ba707
 
 
 
3becd38
710c4a0
34f6dce
88ce352
3becd38
96ba707
 
 
 
3becd38
710c4a0
34f6dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9546a6
fdb2b63
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from smolagents import Tool
import requests

def fetch_wikipedia_article(query: str) -> str:
    url = "https://en.wikipedia.org/w/api.php"
    params = {
        "action": "query",
        "prop": "extracts",
        "explaintext": True,
        "format": "json",
        "titles": query,
    }
    response = requests.get(url, params=params)
    data = response.json()
    pages = data.get("query", {}).get("pages", {})
    page = next(iter(pages.values()))
    return page.get("extract", "No content found for this query.")

def search_wikipedia(query: str) -> str:
    url = "https://en.wikipedia.org/w/api.php"
    params = {
        "action": "query",
        "list": "search",
        "srsearch": query,
        "format": "json",
    }
    response = requests.get(url, params=params)
    data = response.json()
    results = data.get("query", {}).get("search", [])
    if results:
        return "\n".join([r['title'] for r in results])
    return "No search results found."

# ✅ Actual tools expecting dict inputs
class WikipediaTool(Tool):
    name = "WikipediaTool"
    description = "Fetches plain text from a Wikipedia article. Input should be the article title."
    inputs = {"query": "string"}
    output_type = "string"

    def __call__(self, inputs: dict) -> str:
        return fetch_wikipedia_article(inputs["query"])

class WikipediaSearchTool(Tool):
    name = "WikipediaSearchTool"
    description = "Searches Wikipedia for article titles related to a query. Input should be a question or topic."
    inputs = {"query": "string"}
    output_type = "string"

    def __call__(self, inputs: dict) -> str:
        return search_wikipedia(inputs["query"])

# ✅ Wrapper Tools (these are called by CodeAgent)
class WikipediaToolWrapper(Tool):
    name = "WikipediaToolWrapper"
    description = WikipediaTool.description
    inputs = {"query": "string"}
    output_type = "string"

    def __init__(self):
        self.tool = WikipediaTool()

    def __call__(self, inputs: dict) -> str:
        if isinstance(inputs, str):
            inputs = {"query": inputs}
        elif isinstance(inputs, dict) and "query" not in inputs:
            inputs = {"query": next(iter(inputs.values()))}
        return self.tool(inputs)

class WikipediaSearchToolWrapper(Tool):
    name = "WikipediaSearchToolWrapper"
    description = WikipediaSearchTool.description
    inputs = {"query": "string"}
    output_type = "string"

    def __init__(self):
        self.tool = WikipediaSearchTool()

    def __call__(self, inputs: dict) -> str:
        if isinstance(inputs, str):
            inputs = {"query": inputs}
        elif isinstance(inputs, dict) and "query" not in inputs:
            inputs = {"query": next(iter(inputs.values()))}
        return self.tool(inputs)