alisamak commited on
Commit
a969079
·
verified ·
1 Parent(s): c2eb8df

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +48 -5
tools.py CHANGED
@@ -17,13 +17,55 @@ import re
17
  import time
18
  from typing import Optional, List, Dict, Any
19
  import re
20
-
21
- import wikipedia
22
  from langchain_core.tools import tool
23
 
24
- import wikipedia
25
- from bs4 import BeautifulSoup
26
- from langchain_core.tools import tool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  @tool
29
  def detect_non_commutative_subset(table_text: str) -> str:
@@ -157,6 +199,7 @@ def filter_vegetables(items: list[str]) -> list[str]:
157
 
158
  # List of all tools
159
  all_tools = [
 
160
  detect_non_commutative_subset,
161
  reverse_sentence,
162
  filter_vegetables,
 
17
  import time
18
  from typing import Optional, List, Dict, Any
19
  import re
20
+ from datetime import datetime, timedelta
 
21
  from langchain_core.tools import tool
22
 
23
+ @tool
24
+ def search_featured_articles_by_date_range(start_date: str, end_date: str) -> list[str]:
25
+ """
26
+ Searches the English Wikipedia featured article archive and returns article titles
27
+ promoted between start_date and end_date.
28
+
29
+ Args:
30
+ start_date (str): Start date in YYYY-MM-DD format (e.g. '2016-11-01')
31
+ end_date (str): End date in YYYY-MM-DD format (e.g. '2016-11-30')
32
+
33
+ Returns:
34
+ list[str]: A list of article titles promoted as Featured Articles during that period.
35
+ """
36
+ try:
37
+ base_url = "https://en.wikipedia.org/wiki/Wikipedia:Featured_articles"
38
+ archive_url = "https://en.wikipedia.org/wiki/Wikipedia:Featured_articles_by_year"
39
+
40
+ start = datetime.strptime(start_date, "%Y-%m-%d")
41
+ end = datetime.strptime(end_date, "%Y-%m-%d")
42
+
43
+ # We'll collect year-specific pages
44
+ result_titles = []
45
+
46
+ for year in range(start.year, end.year + 1):
47
+ url = f"https://en.wikipedia.org/wiki/Wikipedia:Featured_articles_{year}"
48
+ response = requests.get(url)
49
+ if response.status_code != 200:
50
+ continue
51
+
52
+ soup = BeautifulSoup(response.text, "html.parser")
53
+ for li in soup.select("li"):
54
+ text = li.get_text()
55
+ date_matches = re.findall(r"\b(19|20)\d{2}-\d{2}-\d{2}\b", text)
56
+ for match in date_matches:
57
+ try:
58
+ d = datetime.strptime(match, "%Y-%m-%d")
59
+ if start <= d <= end:
60
+ a_tag = li.find("a")
61
+ if a_tag:
62
+ result_titles.append(a_tag.get_text(strip=True))
63
+ except:
64
+ continue
65
+
66
+ return sorted(set(result_titles))
67
+ except Exception as e:
68
+ return [f"Error: {str(e)}"]
69
 
70
  @tool
71
  def detect_non_commutative_subset(table_text: str) -> str:
 
199
 
200
  # List of all tools
201
  all_tools = [
202
+ search_featured_articles_by_date_range,
203
  detect_non_commutative_subset,
204
  reverse_sentence,
205
  filter_vegetables,