alisamak commited on
Commit
4f3474d
·
verified ·
1 Parent(s): 7b4d911

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +82 -60
tools.py CHANGED
@@ -32,8 +32,30 @@ WIKIPEDIA_PAGES = {
32
  # ➕ add more GAIA topics here
33
  }
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  @tool
36
  def resolve_wikipedia_url(question: str) -> Optional[str]:
 
 
 
37
  q = question.lower()
38
  for key, url in WIKIPEDIA_PAGES.items():
39
  if key in q:
@@ -50,12 +72,12 @@ def route_question(question: str) -> str:
50
  """
51
  q = question.lower()
52
 
53
- if any(keyword in q for keyword in ["how many", "list", "albums", "awards", "published", "release"]):
54
- return "search_web"
55
-
56
- if any(keyword in q for keyword in ["table", "section", "discography", "infobox", "html"]):
57
  return "extract_structured_facts_from_url"
58
 
 
 
 
59
  # Default to internal logic (math, logic puzzles, wordplay)
60
  return "use_internal_logic"
61
 
@@ -171,53 +193,54 @@ def categorize_grocery_items(items: list[str]) -> dict:
171
  return result
172
 
173
 
174
- # @tool
175
- # def search_featured_articles_by_date_range(start_date: str, end_date: str) -> list[str]:
176
- # """
177
- # Searches the English Wikipedia featured article archive and returns article titles
178
- # promoted between start_date and end_date.
179
- # Args:
180
- # start_date (str): Start date in YYYY-MM-DD format (e.g. '2016-11-01')
181
- # end_date (str): End date in YYYY-MM-DD format (e.g. '2016-11-30')
182
- # Returns:
183
- # list[str]: A list of article titles promoted as Featured Articles during that period.
184
- # """
185
- # print(f"🛠️ search_featured_articles_by_date_range called with: {start_date} , {end_date}")
186
- # try:
187
- # base_url = "https://en.wikipedia.org/wiki/Wikipedia:Featured_articles"
188
- # archive_url = "https://en.wikipedia.org/wiki/Wikipedia:Featured_articles_by_year"
189
-
190
- # start = datetime.strptime(start_date, "%Y-%m-%d")
191
- # end = datetime.strptime(end_date, "%Y-%m-%d")
192
-
193
- # # We'll collect year-specific pages
194
- # result_titles = []
195
-
196
- # for year in range(start.year, end.year + 1):
197
- # url = f"https://en.wikipedia.org/wiki/Wikipedia:Featured_articles_{year}"
198
- # response = requests.get(url)
199
- # if response.status_code != 200:
200
- # continue
201
-
202
- # soup = BeautifulSoup(response.text, "html.parser")
203
- # for li in soup.select("li"):
204
- # text = li.get_text()
205
- # date_matches = date_matches = re.findall(r"\b(19\d{2}|20\d{2})-\d{2}-\d{2}\b", text)
206
- # print("🔍 Date matches:", date_matches)
207
 
208
- # for match in date_matches:
209
- # try:
210
- # d = datetime.strptime(match, "%Y-%m-%d")
211
- # if start <= d <= end:
212
- # a_tag = li.find("a")
213
- # if a_tag:
214
- # result_titles.append(a_tag.get_text(strip=True))
215
- # except:
216
- # continue
217
-
218
- # return sorted(set(result_titles))
219
- # except Exception as e:
220
- # return [f"Error: {str(e)}"]
 
221
  @tool
222
  def detect_non_commutative_subset(table_text: str) -> str:
223
  """
@@ -382,14 +405,13 @@ def filter_vegetables(items: list[str]) -> list[str]:
382
 
383
  # List of all tools
384
  all_tools = [
385
- resolve_wikipedia_url,
386
- route_question,
387
- extract_structured_facts_from_url,
388
- search_web,
389
- # search_featured_articles_by_date_range,
390
- categorize_grocery_items,
391
- detect_non_commutative_subset,
392
- reverse_sentence,
393
- filter_vegetables,
394
- # extract_wikipedia_section_html
395
  ]
 
32
  # ➕ add more GAIA topics here
33
  }
34
 
35
+ @tool
36
+ def handle_question(question: str) -> str:
37
+ """
38
+ Dynamically handle a question by routing to appropriate tools and combining results.
39
+ """
40
+ strategy = route_question.run(question)
41
+
42
+ if strategy == "extract_structured_facts_from_url":
43
+ wiki_url = resolve_wikipedia_url.run(question)
44
+ if not wiki_url:
45
+ return "❌ Could not find Wikipedia page."
46
+ return extract_structured_facts_from_url.run(wiki_url)
47
+
48
+ if strategy == "search_featured_articles_by_date_range":
49
+ return search_featured_articles_by_date_range.run("2016-11-01", "2016-11-30")
50
+
51
+ return "🤔 I will use internal reasoning."
52
+
53
+
54
  @tool
55
  def resolve_wikipedia_url(question: str) -> Optional[str]:
56
+ """
57
+ Returns a known Wikipedia URL if the question contains a known entity.
58
+ """
59
  q = question.lower()
60
  for key, url in WIKIPEDIA_PAGES.items():
61
  if key in q:
 
72
  """
73
  q = question.lower()
74
 
75
+ if "wikipedia" in q and any(k in q for k in ["how many", "list", "albums", "awards", "release"]):
 
 
 
76
  return "extract_structured_facts_from_url"
77
 
78
+ if "featured article" in q and any(k in q for k in ["promoted", "in", "nominated"]):
79
+ return "search_featured_articles_by_date_range"
80
+
81
  # Default to internal logic (math, logic puzzles, wordplay)
82
  return "use_internal_logic"
83
 
 
193
  return result
194
 
195
 
196
+ @tool
197
+ def search_featured_articles_by_date_range(start_date: str, end_date: str) -> list[str]:
198
+ """
199
+ Searches the English Wikipedia featured article archive and returns article titles
200
+ promoted between start_date and end_date.
201
+ Args:
202
+ start_date (str): Start date in YYYY-MM-DD format (e.g. '2016-11-01')
203
+ end_date (str): End date in YYYY-MM-DD format (e.g. '2016-11-30')
204
+ Returns:
205
+ list[str]: A list of article titles promoted as Featured Articles during that period.
206
+ """
207
+ print(f"🛠️ search_featured_articles_by_date_range called with: {start_date} , {end_date}")
208
+ try:
209
+ base_url = "https://en.wikipedia.org/wiki/Wikipedia:Featured_articles"
210
+ archive_url = "https://en.wikipedia.org/wiki/Wikipedia:Featured_articles_by_year"
211
+
212
+ start = datetime.strptime(start_date, "%Y-%m-%d")
213
+ end = datetime.strptime(end_date, "%Y-%m-%d")
214
+
215
+ # We'll collect year-specific pages
216
+ result_titles = []
217
+
218
+ for year in range(start.year, end.year + 1):
219
+ url = f"https://en.wikipedia.org/wiki/Wikipedia:Featured_articles_{year}"
220
+ response = requests.get(url)
221
+ if response.status_code != 200:
222
+ continue
223
+
224
+ soup = BeautifulSoup(response.text, "html.parser")
225
+ for li in soup.select("li"):
226
+ text = li.get_text()
227
+ date_matches = date_matches = re.findall(r"\b(19\d{2}|20\d{2})-\d{2}-\d{2}\b", text)
228
+ print("🔍 Date matches:", date_matches)
229
 
230
+ for match in date_matches:
231
+ try:
232
+ d = datetime.strptime(match, "%Y-%m-%d")
233
+ if start <= d <= end:
234
+ a_tag = li.find("a")
235
+ if a_tag:
236
+ result_titles.append(a_tag.get_text(strip=True))
237
+ except:
238
+ continue
239
+
240
+ return sorted(set(result_titles))
241
+ except Exception as e:
242
+ return [f"Error: {str(e)}"]
243
+
244
  @tool
245
  def detect_non_commutative_subset(table_text: str) -> str:
246
  """
 
405
 
406
  # List of all tools
407
  all_tools = [
408
+ handle_question, # Master router for high-level logic
409
+ resolve_wikipedia_url, # Used internally by handle_question
410
+ extract_structured_facts_from_url, # For Wikipedia fact extraction (e.g., albums)
411
+ search_featured_articles_by_date_range, # For Featured Article date lookups (e.g., dinosaur)
412
+ search_web, # Fallback if Wikipedia fails
413
+ detect_non_commutative_subset, # For math table puzzles
414
+ reverse_sentence, # For reverse decoding puzzles
415
+ categorize_grocery_items, # For classification questions
416
+ filter_vegetables # For food-type filtering questions
 
417
  ]