umarbalak commited on
Commit
38a9d89
·
1 Parent(s): ab1a3ec
Files changed (2) hide show
  1. src/agent.py +10 -4
  2. tools/WikipediaTool.py +8 -15
src/agent.py CHANGED
@@ -37,15 +37,21 @@ class WikiTitleFinder(Tool):
37
 
38
  class WikiContentFetcher(Tool):
39
  name = "wiki_page"
40
- description = "Fetch Wikipedia page content."
41
- inputs = {"page_title": {"type": "string", "description": "Wikipedia page title."}}
42
  output_type = "string"
43
 
44
  def forward(self, page_title: str) -> str:
45
  try:
46
- return to_markdown(wiki.page(page_title).html())
 
 
47
  except wiki.exceptions.PageError:
48
- return f"'{page_title}' not found."
 
 
 
 
49
 
50
  class MathSolver(Tool):
51
  name = "math_solver"
 
37
 
38
  class WikiContentFetcher(Tool):
39
  name = "wiki_page"
40
+ description = "Fetch full content of a Wikipedia page as markdown."
41
+ inputs = {"page_title": {"type": "string", "description": "Exact Wikipedia page title."}}
42
  output_type = "string"
43
 
44
  def forward(self, page_title: str) -> str:
45
  try:
46
+ page = wiki.page(page_title, auto_suggest=False)
47
+ markdown_content = to_markdown(page.html())
48
+ return markdown_content[:15000] # Prevent token explosion
49
  except wiki.exceptions.PageError:
50
+ return f"Page '{page_title}' not found."
51
+ except wiki.exceptions.DisambiguationError as e:
52
+ return f"Multiple pages found: {e.options[:5]}"
53
+ except Exception as e:
54
+ return f"Wikipedia fetch error: {str(e)[:200]}"
55
 
56
  class MathSolver(Tool):
57
  name = "math_solver"
tools/WikipediaTool.py CHANGED
@@ -2,27 +2,20 @@ from smolagents import Tool
2
  import wikipedia
3
  class LocalWikipediaTool(Tool):
4
  name = "wikipedia_search"
5
- description = "Use websearch tool first to get the facts, and if needed use this Fallback tool: Use Wikipedia after the web search tool."
6
- inputs = {
7
- "query": {"type": "string", "description": "Search term"}
8
- }
9
  output_type = "string"
10
 
11
  def forward(self, query: str) -> str:
12
  try:
13
- # search for related pages
14
- results = wikipedia.search(query)
15
-
16
  if not results:
17
- return "No Wikipedia results found."
18
 
19
- # pick first result
20
  page_title = results[0]
21
-
22
- # get a short summary (2–3 sentences)
23
- summary = wikipedia.summary(page_title, sentences=3)
24
-
25
- return f"Title: {page_title}\nSummary: {summary}"
26
 
27
  except Exception as e:
28
- return f"Wikipedia error: {e}"
 
2
  import wikipedia
3
  class LocalWikipediaTool(Tool):
4
  name = "wikipedia_search"
5
+ description = "Search Wikipedia and return summary. Use only after web search."
6
+ inputs = {"query": {"type": "string", "description": "Search term"}}
 
 
7
  output_type = "string"
8
 
9
  def forward(self, query: str) -> str:
10
  try:
11
+ results = wikipedia.search(query, results=3)
 
 
12
  if not results:
13
+ return "No results found."
14
 
 
15
  page_title = results[0]
16
+ summary = wikipedia.summary(page_title, sentences=5, auto_suggest=False)
17
+
18
+ return f"Title: {page_title}\n\nSummary: {summary}"
 
 
19
 
20
  except Exception as e:
21
+ return f"Wikipedia error: {str(e)[:150]}"