Andrei Sferdian commited on
Commit ·
ba0cb90
1
Parent(s): afc4478
Fix issue with fetching wiki section content
Browse files- agent.py +2 -0
- app.py +19 -28
- tools/wikipedia.py +21 -17
agent.py
CHANGED
|
@@ -29,6 +29,8 @@ prompt = (
|
|
| 29 |
" The response will be a JSON string with the page's summary and sections.\n"
|
| 30 |
" b. If the page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
|
| 31 |
"If the user asks for a YouTube transcript, use the GetYouTubeTranscript tool.\n"
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
agent = create_react_agent(
|
|
|
|
| 29 |
" The response will be a JSON string with the page's summary and sections.\n"
|
| 30 |
" b. If the page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
|
| 31 |
"If the user asks for a YouTube transcript, use the GetYouTubeTranscript tool.\n"
|
| 32 |
+
"If the question specifies a response format, respect it. Otherwise use the short answer style. I.E. What is the capital of France? Answer: Paris.\n"
|
| 33 |
+
"For questions that require enumerating items, use a numbered list format.\n"
|
| 34 |
)
|
| 35 |
|
| 36 |
agent = create_react_agent(
|
app.py
CHANGED
|
@@ -44,6 +44,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 44 |
|
| 45 |
# 1. Fetch Tasks
|
| 46 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 47 |
try:
|
| 48 |
response = requests.get(questions_url, timeout=15)
|
| 49 |
response.raise_for_status()
|
|
@@ -52,13 +53,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 52 |
print("Fetched questions list is empty.")
|
| 53 |
return "Fetched questions list is empty or invalid format.", None
|
| 54 |
print(f"Fetched {len(questions_data)} questions.")
|
| 55 |
-
except requests.exceptions.RequestException as e:
|
| 56 |
-
print(f"Error fetching questions: {e}")
|
| 57 |
-
return f"Error fetching questions: {e}", None
|
| 58 |
except requests.exceptions.JSONDecodeError as e:
|
| 59 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 60 |
-
|
|
|
|
| 61 |
return f"Error decoding server response for questions: {e}", None
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 64 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -262,32 +264,21 @@ def run_locally():
|
|
| 262 |
# print("No questions available to run.")
|
| 263 |
# return
|
| 264 |
questions = [
|
| 265 |
-
# Question(
|
| 266 |
-
# **{
|
| 267 |
-
# "task_id": "8e867cd7-cff9-4e6c-867a-ff5ddc2550be",
|
| 268 |
-
# "question": "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.",
|
| 269 |
-
# "file_name": "",
|
| 270 |
-
# }
|
| 271 |
-
# ),
|
| 272 |
-
# Question(
|
| 273 |
-
# **{
|
| 274 |
-
# "task_id": "cf106601-ab4f-4af9-b045-5295fe67b37d",
|
| 275 |
-
# "question": "What country had the least number of athletes at the 1928 Summer Olympics? If there's a tie for a number of athletes, return the first in alphabetical order. Give the IOC country code as your answer.",
|
| 276 |
-
# "file_name": "",
|
| 277 |
-
# }
|
| 278 |
-
# ),
|
| 279 |
-
# Question(
|
| 280 |
-
# **{
|
| 281 |
-
# "task_id": "9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
| 282 |
-
# "question": "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\"",
|
| 283 |
-
# "file_name": "",
|
| 284 |
-
# }
|
| 285 |
-
# ),
|
| 286 |
{
|
| 287 |
-
"task_id": "
|
| 288 |
-
"question": "
|
| 289 |
"file_name": "",
|
| 290 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
]
|
| 292 |
|
| 293 |
max_concurrency = int(os.getenv("MAX_CONCURRENCY", 1))
|
|
|
|
| 44 |
|
| 45 |
# 1. Fetch Tasks
|
| 46 |
print(f"Fetching questions from: {questions_url}")
|
| 47 |
+
response = None
|
| 48 |
try:
|
| 49 |
response = requests.get(questions_url, timeout=15)
|
| 50 |
response.raise_for_status()
|
|
|
|
| 53 |
print("Fetched questions list is empty.")
|
| 54 |
return "Fetched questions list is empty or invalid format.", None
|
| 55 |
print(f"Fetched {len(questions_data)} questions.")
|
|
|
|
|
|
|
|
|
|
| 56 |
except requests.exceptions.JSONDecodeError as e:
|
| 57 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 58 |
+
if response:
|
| 59 |
+
print(f"Response text: {response.text[:500]}")
|
| 60 |
return f"Error decoding server response for questions: {e}", None
|
| 61 |
+
except requests.exceptions.RequestException as e:
|
| 62 |
+
print(f"Error fetching questions: {e}")
|
| 63 |
+
return f"Error fetching questions: {e}", None
|
| 64 |
except Exception as e:
|
| 65 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 66 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 264 |
# print("No questions available to run.")
|
| 265 |
# return
|
| 266 |
questions = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
{
|
| 268 |
+
"task_id": "8e867cd7-cff9-4e6c-867a-ff5ddc2550be",
|
| 269 |
+
"question": "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.",
|
| 270 |
"file_name": "",
|
| 271 |
+
},
|
| 272 |
+
# {
|
| 273 |
+
# "task_id": "cf106601-ab4f-4af9-b045-5295fe67b37d",
|
| 274 |
+
# "question": "What country had the least number of athletes at the 1928 Summer Olympics? If there's a tie for a number of athletes, return the first in alphabetical order. Give the IOC country code as your answer.",
|
| 275 |
+
# "file_name": "",
|
| 276 |
+
# },
|
| 277 |
+
# {
|
| 278 |
+
# "task_id": "9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
| 279 |
+
# "question": "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\"",
|
| 280 |
+
# "file_name": "",
|
| 281 |
+
# },
|
| 282 |
]
|
| 283 |
|
| 284 |
max_concurrency = int(os.getenv("MAX_CONCURRENCY", 1))
|
tools/wikipedia.py
CHANGED
|
@@ -116,17 +116,21 @@ def _compare_heading_levels(heading1: str | Tag, heading2: str | Tag) -> int:
|
|
| 116 |
return 0
|
| 117 |
|
| 118 |
|
| 119 |
-
@tool(
|
| 120 |
-
description=(
|
| 121 |
-
"Get the content of a specific section by title from a Wikipedia page.\n"
|
| 122 |
-
"Input: Two strings - the first is the title of the Wikipedia page (e.g., 'Python (programming language)'), "
|
| 123 |
-
"and the second is the title of the section (e.g., 'History').\n"
|
| 124 |
-
"Output: A string containing the content of the specified section in Markdown format, or None if the section does not exist."
|
| 125 |
-
)
|
| 126 |
-
)
|
| 127 |
def get_wiki_page_section_by_title(page_title: str, section_title: str) -> str | None:
|
| 128 |
"""
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
"""
|
| 131 |
print(
|
| 132 |
f"get_wiki_page_section_by_title called with page_title: {page_title}, section_title: {section_title}"
|
|
@@ -161,18 +165,18 @@ def get_wiki_page_section_by_title(page_title: str, section_title: str) -> str |
|
|
| 161 |
return None
|
| 162 |
|
| 163 |
# Navigate to the parent div with class 'mw-heading'
|
| 164 |
-
|
| 165 |
-
while
|
| 166 |
-
if
|
| 167 |
-
if any(cls.startswith("mw-heading") for cls in
|
| 168 |
break
|
| 169 |
-
|
| 170 |
-
if not
|
| 171 |
print(f"Section '{section_title}' not found.")
|
| 172 |
return None
|
| 173 |
|
| 174 |
# Extract all elements until the next section heading
|
| 175 |
-
for element in
|
| 176 |
if not isinstance(element, Tag):
|
| 177 |
continue
|
| 178 |
|
|
@@ -184,7 +188,7 @@ def get_wiki_page_section_by_title(page_title: str, section_title: str) -> str |
|
|
| 184 |
continue
|
| 185 |
|
| 186 |
if element.name == "div" and element.has_attr("class"):
|
| 187 |
-
if _compare_heading_levels(element,
|
| 188 |
break
|
| 189 |
|
| 190 |
section_elements.append(element)
|
|
|
|
| 116 |
return 0
|
| 117 |
|
| 118 |
|
| 119 |
+
@tool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
def get_wiki_page_section_by_title(page_title: str, section_title: str) -> str | None:
|
| 121 |
"""
|
| 122 |
+
Retrieve the content of a specific section from a Wikipedia page.
|
| 123 |
+
|
| 124 |
+
This function fetches a Wikipedia page by its title, then extracts the content
|
| 125 |
+
of a specific section within that page based on the section title provided.
|
| 126 |
+
The section content is converted from HTML to Markdown format.
|
| 127 |
+
|
| 128 |
+
Args:
|
| 129 |
+
page_title (str): The title of the Wikipedia page to fetch.
|
| 130 |
+
section_title (str): The title of the section to extract from the page.
|
| 131 |
+
|
| 132 |
+
Returns:
|
| 133 |
+
str or None: The section content in Markdown format if found, otherwise None.
|
| 134 |
"""
|
| 135 |
print(
|
| 136 |
f"get_wiki_page_section_by_title called with page_title: {page_title}, section_title: {section_title}"
|
|
|
|
| 165 |
return None
|
| 166 |
|
| 167 |
# Navigate to the parent div with class 'mw-heading'
|
| 168 |
+
section_div = section_heading.parent
|
| 169 |
+
while section_div:
|
| 170 |
+
if section_div.has_attr("class"):
|
| 171 |
+
if any(cls.startswith("mw-heading") for cls in section_div["class"]):
|
| 172 |
break
|
| 173 |
+
section_div = section_div.parent
|
| 174 |
+
if not section_div:
|
| 175 |
print(f"Section '{section_title}' not found.")
|
| 176 |
return None
|
| 177 |
|
| 178 |
# Extract all elements until the next section heading
|
| 179 |
+
for element in section_div.find_next_siblings():
|
| 180 |
if not isinstance(element, Tag):
|
| 181 |
continue
|
| 182 |
|
|
|
|
| 188 |
continue
|
| 189 |
|
| 190 |
if element.name == "div" and element.has_attr("class"):
|
| 191 |
+
if _compare_heading_levels(element, section_div) <= 0:
|
| 192 |
break
|
| 193 |
|
| 194 |
section_elements.append(element)
|