Skip to content

Commit

Permalink
feat: Wolfram alpha records SBSHintStep info (#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendong-Fan authored Nov 14, 2024
1 parent 260407d commit bd0ad2f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 13 deletions.
49 changes: 36 additions & 13 deletions camel/toolkits/search_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,35 @@ def _parse_wolfram_result(self, result) -> Dict[str, Any]:

# Loop through each pod to extract the details
for pod in result.get('pod', []):
# Handle the case where subpod might be a list
subpod_data = pod.get('subpod', {})
if isinstance(subpod_data, list):
# If it's a list, get the first item for 'plaintext' and 'img'
description, image_url = next(
(
(data['plaintext'], data['img'])
for data in subpod_data
if "plaintext" in data and "img" in data
),
("", ""),
)
else:
# Otherwise, handle it as a dictionary
description = subpod_data.get('plaintext', '')
image_url = subpod_data.get('img', {}).get('@src', '')

pod_info = {
"title": pod.get('@title', ''),
"description": pod.get('subpod', {}).get('plaintext', ''),
"image_url": pod.get('subpod', {})
.get('img', {})
.get('@src', ''),
"description": description,
"image_url": image_url,
}

# Add to steps list
output["pod_info"].append(pod_info)

# Get final answer
if pod.get('@primary', False):
output["final_answer"] = pod_info["description"]
output["final_answer"] = description

return output

Expand Down Expand Up @@ -368,15 +383,23 @@ def _get_wolframalpha_step_by_step_solution(
response = requests.get(url, params=params)
root = ET.fromstring(response.text)

# Extracting step-by-step hints and removing 'Hint: |'
# Extracting step-by-step steps, including 'SBSStep' and 'SBSHintStep'
steps = []
for subpod in root.findall(
".//pod[@title='Results']//subpod[stepbystepcontenttype='SBSHintStep']//plaintext"
):
if subpod.text:
step_text = subpod.text.strip()
cleaned_step = step_text.replace('Hint: |', '').strip()
steps.append(cleaned_step)
# Find all subpods within the 'Results' pod
for subpod in root.findall(".//pod[@title='Results']//subpod"):
# Check if the subpod has the desired stepbystepcontenttype
content_type = subpod.find('stepbystepcontenttype')
if content_type is not None and content_type.text in [
'SBSStep',
'SBSHintStep',
]:
plaintext = subpod.find('plaintext')
if plaintext is not None and plaintext.text:
step_text = plaintext.text.strip()
cleaned_step = step_text.replace(
'Hint: |', ''
).strip() # Remove 'Hint: |' if present
steps.append(cleaned_step)

# Structuring the steps into a dictionary
structured_steps = {}
Expand Down
57 changes: 57 additions & 0 deletions examples/toolkits/search_toolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
# Licensed under the Apache License, Version 2.0 (the “License”);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========

from camel.toolkits import SearchToolkit

res_simple = SearchToolkit().query_wolfram_alpha(
query="solve 3x-7=11", is_detailed=False
)

print(res_simple)
'''
===============================================================================
x = 6
===============================================================================
'''

res_detailed = SearchToolkit().query_wolfram_alpha(
query="solve 3x-7=11", is_detailed=True
)

print(res_detailed)
'''
===============================================================================
{'query': 'solve 3x-7=11', 'pod_info': [{'title': 'Input interpretation',
'description': 'solve 3 x - 7 = 11', 'image_url': 'https://www6b3.wolframalpha.
com/Calculate/MSP/MSP37741a3dc67f338579ff00003fih94dg39300iaf?
MSPStoreType=image/gif&s=18'}, {'title': 'Result', 'description': 'x = 6',
'image_url': 'https://www6b3.wolframalpha.com/Calculate/MSP/
MSP37751a3dc67f338579ff00001dg4gbdcd0f10i3f?MSPStoreType=image/gif&s=18'},
{'title': 'Plot', 'description': None, 'image_url': 'https://www6b3.
wolframalpha.com/Calculate/MSP/MSP37761a3dc67f338579ff0000374484g95bh3ah3e?
MSPStoreType=image/gif&s=18'}, {'title': 'Number line', 'description': None,
'image_url': 'https://www6b3.wolframalpha.com/Calculate/MSP/
MSP37771a3dc67f338579ff00005573c3a87ahg8dbc?MSPStoreType=image/gif&s=18'}],
'final_answer': 'x = 6', 'steps': {'step1': 'Isolate terms with x to the left
hand side.\nAdd 7 to both sides:\n3 x + (7 - 7) = 7 + 11', 'step2': 'Look for
the difference of two identical terms.\n7 - 7 = 0:\n3 x = 11 + 7', 'step3':
'Evaluate 11 + 7.\n11 + 7 = 18:\n3 x = 18', 'step4': 'Divide both sides by a
constant to simplify the equation.\nDivide both sides of 3 x = 18 by 3:\n(3 x)/
3 = 18/3', 'step5': 'Any nonzero number divided by itself is one.\n3/3 = 1:\nx
= 18/3', 'step6': 'Reduce 18/3 to lowest terms. Start by finding the greatest
common divisor of 18 and 3.\nThe greatest common divisor of 18 and 3 is 3, so
factor out 3 from both the numerator and denominator: 18/3 = (3x6)/(3x1) = 3/3
x 6 = 6\nAnswer: | \n | x = 6'}}
===============================================================================
'''

0 comments on commit bd0ad2f

Please sign in to comment.