From ed4b998eb0f509ee15d60b4ce516841235718394 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Fri, 12 Jul 2024 00:25:13 +0800 Subject: [PATCH 01/15] added wolfram alpha apis as service function --- .../service/reasoning/wolfarm_alpha.py | 224 ++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 src/agentscope/service/reasoning/wolfarm_alpha.py diff --git a/src/agentscope/service/reasoning/wolfarm_alpha.py b/src/agentscope/service/reasoning/wolfarm_alpha.py new file mode 100644 index 000000000..1042dd0b5 --- /dev/null +++ b/src/agentscope/service/reasoning/wolfarm_alpha.py @@ -0,0 +1,224 @@ +# -*- coding: utf-8 -*- +"""Query Wolfram Alpha API""" +import requests +import json +from typing import Optional +from agentscope.service.service_response import ServiceResponse, ServiceExecStatus +from loguru import logger + + +def query_wolfram_alpha_short_answers(api_key: str, query: str) -> ServiceResponse: + """ + Query the Wolfram Alpha Short Answers API. + + Args: + api_key (`str`): + Your Wolfram Alpha API key. + query (`str`): + The query string to search. + + Returns: + `ServiceResponse`: A dictionary with two variables: `status` and + `content`. The `status` variable is from the ServiceExecStatus enum, + and `content` is a dictionary containing the result or error information, + which depends on the `status` variable. + + Example: + .. code-block:: python + + result = query_wolfram_alpha_short_answers( + "your_api_key", + "What is the capital of France?" + ) + if result.status == ServiceExecStatus.SUCCESS: + print(result.content['result']) + """ + url = "http://api.wolframalpha.com/v1/result" + params = {"i": query, "appid": api_key} + + try: + response = requests.get(url, params=params) + if response.status_code == 200: + return ServiceResponse( + status=ServiceExecStatus.SUCCESS, + content={"result": response.text} + ) + else: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": f"HTTP Error: {response.status_code} {response.text}"} + ) + except Exception as e: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": str(e)} + ) + +# Define the Wolfram Alpha Simple query function +def query_wolfram_alpha_simple(api_key: str, query: str) -> ServiceResponse: + """ + Query the Wolfram Alpha Simple API. + + Args: + api_key (`str`): + Your Wolfram Alpha API key. + query (`str`): + The query string to search. + + Returns: + `ServiceResponse`: A dictionary with two variables: `status` and + `content`. The `status` variable is from the ServiceExecStatus enum, + and `content` is a dictionary containing the result or error information, + which depends on the `status` variable. + + Example: + .. code-block:: python + + result = query_wolfram_alpha_simple( + "your_api_key", + "Plot sin(x)" + ) + if result.status == ServiceExecStatus.SUCCESS: + print("Result saved as 'wolfram_alpha_result.png'") + """ + url = "http://api.wolframalpha.com/v1/simple" + params = {"i": query, "appid": api_key} + + try: + response = requests.get(url, params=params) + if response.status_code == 200: + with open('wolfram_alpha_result.png', 'wb') as file: + file.write(response.content) + print("Result saved as 'wolfram_alpha_result.png'") + return ServiceResponse( + status=ServiceExecStatus.SUCCESS, + content={"result": response.text} + ) + else: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": f"HTTP Error: {response.status_code} {response.text}"} + ) + except Exception as e: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": str(e)} + ) + +# Define the Wolfram Alpha Show Steps query function +def query_wolfram_alpha_show_steps(api_key: str, query: str) -> ServiceResponse: + """ + Query the Wolfram Alpha Show Steps API. + + Args: + api_key (`str`): + Your Wolfram Alpha API key. + query (`str`): + The query string to search. + + Returns: + `ServiceResponse`: A dictionary with two variables: `status` and + `content`. The `status` variable is from the ServiceExecStatus enum, + and `content` is a dictionary containing the result or error information, + which depends on the `status` variable. + + Example: + .. code-block:: python + + result = query_wolfram_alpha_show_steps( + "your_api_key", + "Solve x^2 + 2x + 1 = 0" + ) + if result.status == ServiceExecStatus.SUCCESS: + print(result.content['result']) + """ + url = "http://api.wolframalpha.com/v2/query" + params = { + "input": query, + "appid": api_key, + "output": "JSON", + "podstate": "Step-by-step solution" + } + + try: + response = requests.get(url, params=params) + if response.status_code == 200: + data = json.loads(response.text) + steps = [] + logger.info(f"Show Steps API response: {data}") + for pod in data['queryresult'].get('pods', []): + logger.info(f"Processing pod: {pod.get('title')}") + for subpod in pod.get('subpods', []): + logger.info(f"Processing subpod: {subpod.get('title')}") + plaintext = subpod.get('plaintext', '').strip() + if plaintext: + steps.append(plaintext) + if not steps: + logger.warning("No step-by-step solution found in the response.") + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": "No step-by-step solution found in the response."} + ) + formatted_steps = "\n".join(steps) + print(formatted_steps) + return ServiceResponse( + status=ServiceExecStatus.SUCCESS, + content={"result": formatted_steps} + ) + else: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": f"HTTP Error: {response.status_code} {response.text}"} + ) + except Exception as e: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": str(e)} + ) + +def query_wolfram_alpha_llm(api_key: str, query: str) -> ServiceResponse: + """ + Query the Wolfram Alpha LLM API. + + Args: + api_key (`str`): + Your Wolfram Alpha API key. + query (`str`): + The query string to search. + + Returns: + `ServiceResponse`: A dictionary with two variables: `status` and + `content`. The `status` variable is from the ServiceExecStatus enum, + and `content` is a dictionary containing the result or error information, + which depends on the `status` variable. + + Example: + .. code-block:: python + + result = query_wolfram_alpha_llm( + "your_api_key", + "Explain quantum entanglement" + ) + if result.status == ServiceExecStatus.SUCCESS: + print(result.content['result']) + """ + url = "https://www.wolframalpha.com/api/v1/llm-api" + params = {"input": query, "appid": api_key} + + try: + response = requests.get(url, params=params) + if response.status_code == 200: + return ServiceResponse( + status=ServiceExecStatus.SUCCESS, + content={"result": response.text} + ) + else: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": f"HTTP Error: {response.status_code} {response.text}"} + ) + except Exception as e: + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={"error": str(e)} + ) \ No newline at end of file From c17900ceb360373710e64089c9254031b19bede8 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Fri, 12 Jul 2024 02:55:04 +0800 Subject: [PATCH 02/15] add wolfram alpha APIs as service function --- src/agentscope/service/__init__.py | 10 ++ .../{wolfarm_alpha.py => wolfram_alpha.py} | 143 +++++++++++------- 2 files changed, 97 insertions(+), 56 deletions(-) rename src/agentscope/service/reasoning/{wolfarm_alpha.py => wolfram_alpha.py} (63%) diff --git a/src/agentscope/service/__init__.py b/src/agentscope/service/__init__.py index 2a0ba3e53..3a86db8fd 100644 --- a/src/agentscope/service/__init__.py +++ b/src/agentscope/service/__init__.py @@ -26,6 +26,12 @@ dblp_search_authors, dblp_search_venues, ) +from .reasoning.wolfram_alpha import ( + query_wolfram_alpha_short_answers, + query_wolfram_alpha_simple, + query_wolfram_alpha_show_steps, + query_wolfram_alpha_llm, +) from .multi_modality.dashscope_services import ( dashscope_image_to_text, dashscope_text_to_image, @@ -101,6 +107,10 @@ def get_help() -> None: "openai_image_to_text", "openai_edit_image", "openai_create_image_variation", + "query_wolfram_alpha_short_answers", + "query_wolfram_alpha_simple", + "query_wolfram_alpha_show_steps", + "query_wolfram_alpha_llm", # to be deprecated "ServiceFactory", ] diff --git a/src/agentscope/service/reasoning/wolfarm_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py similarity index 63% rename from src/agentscope/service/reasoning/wolfarm_alpha.py rename to src/agentscope/service/reasoning/wolfram_alpha.py index 1042dd0b5..c9d008115 100644 --- a/src/agentscope/service/reasoning/wolfarm_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -1,13 +1,18 @@ # -*- coding: utf-8 -*- """Query Wolfram Alpha API""" -import requests -import json -from typing import Optional -from agentscope.service.service_response import ServiceResponse, ServiceExecStatus -from loguru import logger - - -def query_wolfram_alpha_short_answers(api_key: str, query: str) -> ServiceResponse: +from typing import Any, Dict # Corrected import order +import requests # Corrected import order +from loguru import logger # Corrected import order +from agentscope.service.service_response import ( + ServiceResponse, + ServiceExecStatus, +) + + +def query_wolfram_alpha_short_answers( + api_key: str, + query: str, +) -> ServiceResponse: """ Query the Wolfram Alpha Short Answers API. @@ -20,7 +25,8 @@ def query_wolfram_alpha_short_answers(api_key: str, query: str) -> ServiceRespon Returns: `ServiceResponse`: A dictionary with two variables: `status` and `content`. The `status` variable is from the ServiceExecStatus enum, - and `content` is a dictionary containing the result or error information, + and `content` is a dictionary containing + the result or error information, which depends on the `status` variable. Example: @@ -41,21 +47,25 @@ def query_wolfram_alpha_short_answers(api_key: str, query: str) -> ServiceRespon if response.status_code == 200: return ServiceResponse( status=ServiceExecStatus.SUCCESS, - content={"result": response.text} - ) - else: - return ServiceResponse( - status=ServiceExecStatus.ERROR, - content={"error": f"HTTP Error: {response.status_code} {response.text}"} + content={"result": response.text}, ) + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={ + "error": f"HTTP Error: {response.status_code} {response.text}", + }, + ) except Exception as e: return ServiceResponse( status=ServiceExecStatus.ERROR, - content={"error": str(e)} + content={"error": str(e)}, ) -# Define the Wolfram Alpha Simple query function -def query_wolfram_alpha_simple(api_key: str, query: str) -> ServiceResponse: + +def query_wolfram_alpha_simple( + api_key: str, + query: str, +) -> ServiceResponse: """ Query the Wolfram Alpha Simple API. @@ -68,7 +78,8 @@ def query_wolfram_alpha_simple(api_key: str, query: str) -> ServiceResponse: Returns: `ServiceResponse`: A dictionary with two variables: `status` and `content`. The `status` variable is from the ServiceExecStatus enum, - and `content` is a dictionary containing the result or error information, + and `content` is a dictionary containing + the result or error information, which depends on the `status` variable. Example: @@ -87,26 +98,32 @@ def query_wolfram_alpha_simple(api_key: str, query: str) -> ServiceResponse: try: response = requests.get(url, params=params) if response.status_code == 200: - with open('wolfram_alpha_result.png', 'wb') as file: + with open("wolfram_alpha_result.png", "wb") as file: file.write(response.content) print("Result saved as 'wolfram_alpha_result.png'") return ServiceResponse( status=ServiceExecStatus.SUCCESS, - content={"result": response.text} - ) - else: - return ServiceResponse( - status=ServiceExecStatus.ERROR, - content={"error": f"HTTP Error: {response.status_code} {response.text}"} + content={ + "result": "Image saved as 'wolfram_alpha_result.png'", + }, ) + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={ + "error": f"HTTP Error: {response.status_code} {response.text}", + }, + ) except Exception as e: return ServiceResponse( status=ServiceExecStatus.ERROR, - content={"error": str(e)} + content={"error": str(e)}, ) -# Define the Wolfram Alpha Show Steps query function -def query_wolfram_alpha_show_steps(api_key: str, query: str) -> ServiceResponse: + +def query_wolfram_alpha_show_steps( + api_key: str, + query: str, +) -> ServiceResponse: """ Query the Wolfram Alpha Show Steps API. @@ -119,7 +136,8 @@ def query_wolfram_alpha_show_steps(api_key: str, query: str) -> ServiceResponse: Returns: `ServiceResponse`: A dictionary with two variables: `status` and `content`. The `status` variable is from the ServiceExecStatus enum, - and `content` is a dictionary containing the result or error information, + and `content` is a dictionary containing + the result or error information, which depends on the `status` variable. Example: @@ -137,46 +155,57 @@ def query_wolfram_alpha_show_steps(api_key: str, query: str) -> ServiceResponse: "input": query, "appid": api_key, "output": "JSON", - "podstate": "Step-by-step solution" + "podstate": "Step-by-step solution", } try: response = requests.get(url, params=params) if response.status_code == 200: - data = json.loads(response.text) + data: Dict[str, Any] = response.json() steps = [] logger.info(f"Show Steps API response: {data}") - for pod in data['queryresult'].get('pods', []): + for pod in data["queryresult"].get("pods", []): logger.info(f"Processing pod: {pod.get('title')}") - for subpod in pod.get('subpods', []): + for subpod in pod.get("subpods", []): logger.info(f"Processing subpod: {subpod.get('title')}") - plaintext = subpod.get('plaintext', '').strip() + plaintext = subpod.get("plaintext", "").strip() if plaintext: steps.append(plaintext) if not steps: - logger.warning("No step-by-step solution found in the response.") + logger.warning( + "No step-by-step solution found in the response.", + ) return ServiceResponse( status=ServiceExecStatus.ERROR, - content={"error": "No step-by-step solution found in the response."} + content={ + "error": ( + "No step-by-step solution found in the response." + ), + }, ) formatted_steps = "\n".join(steps) print(formatted_steps) return ServiceResponse( status=ServiceExecStatus.SUCCESS, - content={"result": formatted_steps} - ) - else: - return ServiceResponse( - status=ServiceExecStatus.ERROR, - content={"error": f"HTTP Error: {response.status_code} {response.text}"} + content={"result": formatted_steps}, ) + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={ + "error": f"HTTP Error: {response.status_code} {response.text}", + }, + ) except Exception as e: return ServiceResponse( status=ServiceExecStatus.ERROR, - content={"error": str(e)} + content={"error": str(e)}, ) - -def query_wolfram_alpha_llm(api_key: str, query: str) -> ServiceResponse: + + +def query_wolfram_alpha_llm( + api_key: str, + query: str, +) -> ServiceResponse: """ Query the Wolfram Alpha LLM API. @@ -189,7 +218,8 @@ def query_wolfram_alpha_llm(api_key: str, query: str) -> ServiceResponse: Returns: `ServiceResponse`: A dictionary with two variables: `status` and `content`. The `status` variable is from the ServiceExecStatus enum, - and `content` is a dictionary containing the result or error information, + and `content` is a dictionary containing + the result or error information, which depends on the `status` variable. Example: @@ -199,7 +229,7 @@ def query_wolfram_alpha_llm(api_key: str, query: str) -> ServiceResponse: "your_api_key", "Explain quantum entanglement" ) - if result.status == ServiceExecStatus.SUCCESS: + if result.status == ServiceExecStatus.SUCCESS, print(result.content['result']) """ url = "https://www.wolframalpha.com/api/v1/llm-api" @@ -210,15 +240,16 @@ def query_wolfram_alpha_llm(api_key: str, query: str) -> ServiceResponse: if response.status_code == 200: return ServiceResponse( status=ServiceExecStatus.SUCCESS, - content={"result": response.text} - ) - else: - return ServiceResponse( - status=ServiceExecStatus.ERROR, - content={"error": f"HTTP Error: {response.status_code} {response.text}"} + content={"result": response.text}, ) + return ServiceResponse( + status=ServiceExecStatus.ERROR, + content={ + "error": f"HTTP Error: {response.status_code} {response.text}", + }, + ) except Exception as e: return ServiceResponse( status=ServiceExecStatus.ERROR, - content={"error": str(e)} - ) \ No newline at end of file + content={"error": str(e)}, + ) From aa5f9b5f34b24f0277990449de9f58c340ea9025 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 1 Aug 2024 19:58:10 +0800 Subject: [PATCH 03/15] Update wolfram_alpha.py --- .../service/reasoning/wolfram_alpha.py | 113 +++++++++++++++++- 1 file changed, 107 insertions(+), 6 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index c9d008115..e7786dfa3 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -14,7 +14,13 @@ def query_wolfram_alpha_short_answers( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha Short Answers API. + Query the Wolfram Alpha Short Answers API. The Short Answers API returns + a single plain text result directly from Wolfram|Alpha. In general, this + text is taken directly from the Result pod of Wolfram|Alpha output. This + API type is designed to deliver brief answers in the most basic format possible. + Suitable for queries such as simple knowledge/facts retrieval. + See https://products.wolframalpha.com/short-answers-api/documentation + for more details. Args: api_key (`str`): @@ -38,6 +44,7 @@ def query_wolfram_alpha_short_answers( ) if result.status == ServiceExecStatus.SUCCESS: print(result.content['result']) + # Output: Paris, Île-de-France, France """ url = "http://api.wolframalpha.com/v1/result" params = {"i": query, "appid": api_key} @@ -67,7 +74,10 @@ def query_wolfram_alpha_simple( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha Simple API. + Query the Wolfram Alpha Simple API. The Simple API generates full + Wolfram|Alpha output in a universally viewable image format. Suitable for queries + such as knowledge/facts retrieval. See + https://products.wolframalpha.com/simple-api/documentation for more details. Args: api_key (`str`): @@ -81,6 +91,7 @@ def query_wolfram_alpha_simple( and `content` is a dictionary containing the result or error information, which depends on the `status` variable. + The returned image is saved in the save directory as `wolfram_alpha_result.png`. Example: .. code-block:: python @@ -125,7 +136,13 @@ def query_wolfram_alpha_show_steps( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha Show Steps API. + Query the Wolfram Alpha Show Steps API. An extension of the Full Results API, + the Show Steps API gives direct access to Wolfram|Alpha's full for + queries in a variety of mathematical and scientific subjects. These + explanations of computed answers are designed to provide clarity and + understanding to the end user and are especially useful in educational + and training applications. see + https://products.wolframalpha.com/show-steps-api/documentation for more details. Args: api_key (`str`): @@ -149,6 +166,19 @@ def query_wolfram_alpha_show_steps( ) if result.status == ServiceExecStatus.SUCCESS: print(result.content['result']) + + Output: + solve x^2 + 2 x + 1 = 0 + x = -1 + Solve for x: + x^2 + 2 x + 1 = 0 + Write the left hand side as a square: + (x + 1)^2 = 0 + Take the square root of both sides: + x + 1 = 0 + Subtract 1 from both sides: + Answer: | + | x = -1 """ url = "http://api.wolframalpha.com/v2/query" params = { @@ -207,7 +237,11 @@ def query_wolfram_alpha_llm( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha LLM API. + Query the Wolfram Alpha LLM API. The LLM API is built for use specifically with + large language models and chat products. Although the majority of data available + through the Wolfram|Alpha website is also available through this API, certain + subjects may be restricted by default. see + https://products.wolframalpha.com/llm-api/documentation for more details. Args: api_key (`str`): @@ -227,10 +261,77 @@ def query_wolfram_alpha_llm( result = query_wolfram_alpha_llm( "your_api_key", - "Explain quantum entanglement" + "3 densest elemental metals" ) - if result.status == ServiceExecStatus.SUCCESS, + if result.status == ServiceExecStatus.SUCCESS: print(result.content['result']) + + Example output: + .. code-block:: text + + Query: + "3 densest elemental metals" + + Input interpretation: + 3 densest metallic elements | by mass density + + Result: + 1 | hassium | 41 g/cm^3 | + 2 | meitnerium | 37.4 g/cm^3 | + 3 | bohrium | 37.1 g/cm^3 | + + Periodic table location: + image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP339924348bb2bhie7aib000047ib639egd214fg7?MSPStoreType=image/png&s=14 + + Images: + image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340024348bb2bhie7aib00004beh851acg9931b0?MSPStoreType=image/png&s=14 + Wolfram Language code: Dataset[EntityValue[{Entity["Element", "Hassium"], Entity["Element", "Meitnerium"], Entity["Element", "Bohrium"]}, EntityProperty["Element", "Image"], "EntityAssociation"]] + + Basic elemental properties: + | hassium | meitnerium | bohrium + atomic symbol | Hs | Mt | Bh + atomic number | 108 | 109 | 107 + atomic mass | 269 u | 277 u | 270 u + half-life | 67 min | 30 min | 90 min + + Material properties: + | hassium | meitnerium | bohrium + mass density | 41 g/cm^3 | 37.4 g/cm^3 | 37.1 g/cm^3 + (properties at standard conditions) + + Reactivity: + | bohrium + valence | 7 + + Atomic properties: + | hassium | meitnerium | bohrium + term symbol | ^5D_4 | ^4F_(9/2) | ^6S_(5/2) + (electronic ground state properties) + + Abundances: + | (all cases) + crust abundance | 0 mass% + human abundance | 0 mass% + + Nuclear properties: + | hassium | meitnerium | bohrium + half-life | 67 min | 30 min | 90 min + specific radioactivity | 446085 TBq/g | 833168 TBq/g | 285952 TBq/g + unstable isotopes | hassium-276 (67 min) | ... | meitnerium-278 (30 min) | ... | bohrium-274 (90 min) | ... + + | (all cases) + decay mode | alpha emission + + Identifiers: + | hassium | meitnerium | bohrium + CAS number | 54037-57-9 | 54038-01-6 | 54037-14-8 + PubChem CID number | CID56951714 | CID56951716 | CID56951713 + + Wikipedia page hits history: + image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340124348bb2bhie7aib00002819bbig9a52fe5c?MSPStoreType=image/png&s=14 + + Wolfram|Alpha website result for "3 densest elemental metals": + https://www6b3.wolframalpha.com/input?i=3+densest+elemental+metals """ url = "https://www.wolframalpha.com/api/v1/llm-api" params = {"input": query, "appid": api_key} From 4529e916e7a2564f79ebc84e23d1c52ba59ecf5b Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:05:24 +0800 Subject: [PATCH 04/15] Update wolfram_alpha.py --- .../service/reasoning/wolfram_alpha.py | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index e7786dfa3..47edc0438 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -17,9 +17,10 @@ def query_wolfram_alpha_short_answers( Query the Wolfram Alpha Short Answers API. The Short Answers API returns a single plain text result directly from Wolfram|Alpha. In general, this text is taken directly from the Result pod of Wolfram|Alpha output. This - API type is designed to deliver brief answers in the most basic format possible. - Suitable for queries such as simple knowledge/facts retrieval. - See https://products.wolframalpha.com/short-answers-api/documentation + API type is designed to deliver brief answers in the most basic + format possible. Suitable for queries such as simple knowledge/facts + retrieval. See + https://products.wolframalpha.com/short-answers-api/documentation for more details. Args: @@ -75,9 +76,10 @@ def query_wolfram_alpha_simple( ) -> ServiceResponse: """ Query the Wolfram Alpha Simple API. The Simple API generates full - Wolfram|Alpha output in a universally viewable image format. Suitable for queries - such as knowledge/facts retrieval. See - https://products.wolframalpha.com/simple-api/documentation for more details. + Wolfram|Alpha output in a universally viewable image format. + Suitable for queries such as knowledge/facts retrieval. See + https://products.wolframalpha.com/simple-api/documentation + for more details. Args: api_key (`str`): @@ -91,7 +93,8 @@ def query_wolfram_alpha_simple( and `content` is a dictionary containing the result or error information, which depends on the `status` variable. - The returned image is saved in the save directory as `wolfram_alpha_result.png`. + The returned image is saved in the save directory as + `wolfram_alpha_result.png`. Example: .. code-block:: python @@ -136,13 +139,15 @@ def query_wolfram_alpha_show_steps( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha Show Steps API. An extension of the Full Results API, - the Show Steps API gives direct access to Wolfram|Alpha's full for - queries in a variety of mathematical and scientific subjects. These - explanations of computed answers are designed to provide clarity and - understanding to the end user and are especially useful in educational - and training applications. see - https://products.wolframalpha.com/show-steps-api/documentation for more details. + Query the Wolfram Alpha Show Steps API. An extension of the + Full Results API, the Show Steps API gives direct access to + Wolfram|Alpha's full for queries in a variety of mathematical + and scientific subjects. These explanations of computed answers + are designed to provide clarity and understanding to the + end user and are especially useful in educational and + training applications. see + https://products.wolframalpha.com/show-steps-api/documentation + for more details. Args: api_key (`str`): @@ -281,11 +286,11 @@ def query_wolfram_alpha_llm( 3 | bohrium | 37.1 g/cm^3 | Periodic table location: - image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP339924348bb2bhie7aib000047ib639egd214fg7?MSPStoreType=image/png&s=14 + image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP339924348bb2bhie7aib000047ib639egd214fg7?MSPStoreType=image/png&s=14 # noqa: E501 # pylint: disable=line-too-long Images: - image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340024348bb2bhie7aib00004beh851acg9931b0?MSPStoreType=image/png&s=14 - Wolfram Language code: Dataset[EntityValue[{Entity["Element", "Hassium"], Entity["Element", "Meitnerium"], Entity["Element", "Bohrium"]}, EntityProperty["Element", "Image"], "EntityAssociation"]] + image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340024348bb2bhie7aib00004beh851acg9931b0?MSPStoreType=image/png&s=14 # noqa: E501 # pylint: disable=line-too-long + Wolfram Language code: Dataset[EntityValue[{Entity["Element", "Hassium"], Entity["Element", "Meitnerium"], Entity["Element", "Bohrium"]}, EntityProperty["Element", "Image"], "EntityAssociation"]] # noqa: E501 # pylint: disable=line-too-long Basic elemental properties: | hassium | meitnerium | bohrium @@ -317,7 +322,7 @@ def query_wolfram_alpha_llm( | hassium | meitnerium | bohrium half-life | 67 min | 30 min | 90 min specific radioactivity | 446085 TBq/g | 833168 TBq/g | 285952 TBq/g - unstable isotopes | hassium-276 (67 min) | ... | meitnerium-278 (30 min) | ... | bohrium-274 (90 min) | ... + unstable isotopes | hassium-276 (67 min) | ... | meitnerium-278 (30 min) | ... | bohrium-274 (90 min) | ... # noqa: E501 # pylint: disable=line-too-long | (all cases) decay mode | alpha emission @@ -328,7 +333,7 @@ def query_wolfram_alpha_llm( PubChem CID number | CID56951714 | CID56951716 | CID56951713 Wikipedia page hits history: - image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340124348bb2bhie7aib00002819bbig9a52fe5c?MSPStoreType=image/png&s=14 + image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340124348bb2bhie7aib00002819bbig9a52fe5c?MSPStoreType=image/png&s=14 # noqa: E501 # pylint: disable=line-too-long Wolfram|Alpha website result for "3 densest elemental metals": https://www6b3.wolframalpha.com/input?i=3+densest+elemental+metals From 0fca3edf60ddaefe9114f0acc49fa043cd1b3bba Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:35:17 +0800 Subject: [PATCH 05/15] reformated docstring --- .../service/reasoning/wolfram_alpha.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index 47edc0438..8986537a7 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -286,11 +286,17 @@ def query_wolfram_alpha_llm( 3 | bohrium | 37.1 g/cm^3 | Periodic table location: - image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP339924348bb2bhie7aib000047ib639egd214fg7?MSPStoreType=image/png&s=14 # noqa: E501 # pylint: disable=line-too-long + image: https://www6b3.wolframalpha.com/Calculate/... Images: - image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340024348bb2bhie7aib00004beh851acg9931b0?MSPStoreType=image/png&s=14 # noqa: E501 # pylint: disable=line-too-long - Wolfram Language code: Dataset[EntityValue[{Entity["Element", "Hassium"], Entity["Element", "Meitnerium"], Entity["Element", "Bohrium"]}, EntityProperty["Element", "Image"], "EntityAssociation"]] # noqa: E501 # pylint: disable=line-too-long + image: https://www6b3.wolframalpha.com/Calculate/... + Wolfram Language code: Dataset[ + EntityValue[ + {Entity["Element", "Hassium"], Entity["Element", "Meitnerium"], Entity["Element", "Bohrium"]}, + EntityProperty["Element", "Image"], + "EntityAssociation" + ] + ] Basic elemental properties: | hassium | meitnerium | bohrium @@ -322,7 +328,9 @@ def query_wolfram_alpha_llm( | hassium | meitnerium | bohrium half-life | 67 min | 30 min | 90 min specific radioactivity | 446085 TBq/g | 833168 TBq/g | 285952 TBq/g - unstable isotopes | hassium-276 (67 min) | ... | meitnerium-278 (30 min) | ... | bohrium-274 (90 min) | ... # noqa: E501 # pylint: disable=line-too-long + unstable isotopes | hassium-276 (67 min) | ... + | meitnerium-278 (30 min) | ... + | bohrium-274 (90 min) | ... | (all cases) decay mode | alpha emission @@ -333,7 +341,7 @@ def query_wolfram_alpha_llm( PubChem CID number | CID56951714 | CID56951716 | CID56951713 Wikipedia page hits history: - image: https://www6b3.wolframalpha.com/Calculate/MSP/MSP340124348bb2bhie7aib00002819bbig9a52fe5c?MSPStoreType=image/png&s=14 # noqa: E501 # pylint: disable=line-too-long + image: https://www6b3.wolframalpha.com/Calculate/... Wolfram|Alpha website result for "3 densest elemental metals": https://www6b3.wolframalpha.com/input?i=3+densest+elemental+metals From 1bc22e980209b3d0c52981fd29e409ee656f90f4 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Fri, 2 Aug 2024 22:43:05 +0800 Subject: [PATCH 06/15] Update wolfram_alpha.py --- .../service/reasoning/wolfram_alpha.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index 8986537a7..79f80ad00 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -242,10 +242,11 @@ def query_wolfram_alpha_llm( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha LLM API. The LLM API is built for use specifically with - large language models and chat products. Although the majority of data available - through the Wolfram|Alpha website is also available through this API, certain - subjects may be restricted by default. see + Query the Wolfram Alpha LLM API. The LLM API is built for + use specifically with large language models and chat products. + Although the majority of data available through the + Wolfram|Alpha website is also available through this API, + certain subjects may be restricted by default. see https://products.wolframalpha.com/llm-api/documentation for more details. Args: @@ -290,9 +291,14 @@ def query_wolfram_alpha_llm( Images: image: https://www6b3.wolframalpha.com/Calculate/... - Wolfram Language code: Dataset[ + Wolfram Language code: + Dataset[ EntityValue[ - {Entity["Element", "Hassium"], Entity["Element", "Meitnerium"], Entity["Element", "Bohrium"]}, + { + Entity["Element", "Hassium"], + Entity["Element", "Meitnerium"], + Entity["Element", "Bohrium"] + }, EntityProperty["Element", "Image"], "EntityAssociation" ] @@ -328,9 +334,9 @@ def query_wolfram_alpha_llm( | hassium | meitnerium | bohrium half-life | 67 min | 30 min | 90 min specific radioactivity | 446085 TBq/g | 833168 TBq/g | 285952 TBq/g - unstable isotopes | hassium-276 (67 min) | ... - | meitnerium-278 (30 min) | ... - | bohrium-274 (90 min) | ... + unstable isotopes | hassium-276 (67 min) | ... + | meitnerium-278 (30 min) | ... + | bohrium-274 (90 min) | ... | (all cases) decay mode | alpha emission From 5bfbfce71285a31143b1c0416b14d409bb0e002f Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:30:52 +0800 Subject: [PATCH 07/15] Update wolfram_alpha.py --- .../service/reasoning/wolfram_alpha.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index 79f80ad00..b5c597201 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- """Query Wolfram Alpha API""" +import os from typing import Any, Dict # Corrected import order import requests # Corrected import order from loguru import logger # Corrected import order @@ -73,19 +74,21 @@ def query_wolfram_alpha_short_answers( def query_wolfram_alpha_simple( api_key: str, query: str, + save_path: str = "wolfram_alpha_result.png", ) -> ServiceResponse: """ Query the Wolfram Alpha Simple API. The Simple API generates full Wolfram|Alpha output in a universally viewable image format. - Suitable for queries such as knowledge/facts retrieval. See - https://products.wolframalpha.com/simple-api/documentation - for more details. + Suitable for queries such as knowledge/facts retrieval. Args: api_key (`str`): Your Wolfram Alpha API key. query (`str`): The query string to search. + save_path (`str`, optional): + The path where the result image will be saved. + Defaults to "wolfram_alpha_result.png" in the current directory. Returns: `ServiceResponse`: A dictionary with two variables: `status` and @@ -93,18 +96,18 @@ def query_wolfram_alpha_simple( and `content` is a dictionary containing the result or error information, which depends on the `status` variable. - The returned image is saved in the save directory as - `wolfram_alpha_result.png`. + The returned image is saved in the specified save path. Example: .. code-block:: python result = query_wolfram_alpha_simple( "your_api_key", - "Plot sin(x)" + "Plot sin(x)", + save_path="path/to/save/result.png" ) if result.status == ServiceExecStatus.SUCCESS: - print("Result saved as 'wolfram_alpha_result.png'") + print(f"Result saved as '{result.content['result']}'") """ url = "http://api.wolframalpha.com/v1/simple" params = {"i": query, "appid": api_key} @@ -112,13 +115,16 @@ def query_wolfram_alpha_simple( try: response = requests.get(url, params=params) if response.status_code == 200: - with open("wolfram_alpha_result.png", "wb") as file: + # Ensure the directory exists + os.makedirs(os.path.dirname(save_path), exist_ok=True) + + with open(save_path, "wb") as file: file.write(response.content) - print("Result saved as 'wolfram_alpha_result.png'") + print(f"Result saved as '{save_path}'") return ServiceResponse( status=ServiceExecStatus.SUCCESS, content={ - "result": "Image saved as 'wolfram_alpha_result.png'", + "result": save_path, }, ) return ServiceResponse( From 53cd67af8f25d12380044700d4d5376feddcebc3 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:54:51 +0800 Subject: [PATCH 08/15] Update wolfram_alpha.py --- .../service/reasoning/wolfram_alpha.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index b5c597201..b69d5e798 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -74,7 +74,7 @@ def query_wolfram_alpha_short_answers( def query_wolfram_alpha_simple( api_key: str, query: str, - save_path: str = "wolfram_alpha_result.png", + save_path: str = "wolfram_alpha_result.png" ) -> ServiceResponse: """ Query the Wolfram Alpha Simple API. The Simple API generates full @@ -107,7 +107,7 @@ def query_wolfram_alpha_simple( save_path="path/to/save/result.png" ) if result.status == ServiceExecStatus.SUCCESS: - print(f"Result saved as '{result.content['result']}'") + logger.info(f"Result saved as '{result.content['result']}'") """ url = "http://api.wolframalpha.com/v1/simple" params = {"i": query, "appid": api_key} @@ -115,12 +115,16 @@ def query_wolfram_alpha_simple( try: response = requests.get(url, params=params) if response.status_code == 200: - # Ensure the directory exists - os.makedirs(os.path.dirname(save_path), exist_ok=True) - + # Get the directory path + dir_path = os.path.dirname(save_path) + + # If dir_path is not empty, create the directory + if dir_path: + os.makedirs(dir_path, exist_ok=True) + with open(save_path, "wb") as file: file.write(response.content) - print(f"Result saved as '{save_path}'") + logger.info(f"Result saved as '{save_path}'") return ServiceResponse( status=ServiceExecStatus.SUCCESS, content={ @@ -134,6 +138,7 @@ def query_wolfram_alpha_simple( }, ) except Exception as e: + logger.error(f"Error in query_wolfram_alpha_simple: {str(e)}") return ServiceResponse( status=ServiceExecStatus.ERROR, content={"error": str(e)}, From 4da6822e1d3e1be5aa3ca1c589d37d81ef6787bb Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:58:06 +0800 Subject: [PATCH 09/15] Update wolfram_alpha.py --- src/agentscope/service/reasoning/wolfram_alpha.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index b69d5e798..9fadd0843 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -74,7 +74,7 @@ def query_wolfram_alpha_short_answers( def query_wolfram_alpha_simple( api_key: str, query: str, - save_path: str = "wolfram_alpha_result.png" + save_path: str = "wolfram_alpha_result.png", ) -> ServiceResponse: """ Query the Wolfram Alpha Simple API. The Simple API generates full @@ -117,11 +117,11 @@ def query_wolfram_alpha_simple( if response.status_code == 200: # Get the directory path dir_path = os.path.dirname(save_path) - + # If dir_path is not empty, create the directory if dir_path: os.makedirs(dir_path, exist_ok=True) - + with open(save_path, "wb") as file: file.write(response.content) logger.info(f"Result saved as '{save_path}'") From ebf74ac4e8cf4ac1de2ed1fdbae6fb29ff542a7c Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:19:44 +0800 Subject: [PATCH 10/15] Update wolfram_alpha.py --- src/agentscope/service/reasoning/wolfram_alpha.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index 9fadd0843..55c373611 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -79,7 +79,9 @@ def query_wolfram_alpha_simple( """ Query the Wolfram Alpha Simple API. The Simple API generates full Wolfram|Alpha output in a universally viewable image format. - Suitable for queries such as knowledge/facts retrieval. + Suitable for queries such as knowledge/facts retrieval. See + https://products.wolframalpha.com/simple-api/documentation + for more details. Args: api_key (`str`): From 66250b2003872a107a900165131cc4e2724a0d4c Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:48:50 +0800 Subject: [PATCH 11/15] updated tutorial/204-service.md for both CN and EN --- docs/sphinx_doc/en/source/tutorial/204-service.md | 5 ++++- docs/sphinx_doc/zh_CN/source/tutorial/204-service.md | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/sphinx_doc/en/source/tutorial/204-service.md b/docs/sphinx_doc/en/source/tutorial/204-service.md index 38b4047b0..84f8d53ff 100644 --- a/docs/sphinx_doc/en/source/tutorial/204-service.md +++ b/docs/sphinx_doc/en/source/tutorial/204-service.md @@ -49,7 +49,10 @@ The following table outlines the various Service functions by type. These functi | | `openai_image_to_text` | Convert text to image using OpenAI API | | `openai_text_to_audio` | Convert text to audio using OpenAI API | | `openai_audio_to_text` | Convert audio to text using OpenAI API - +| Reasoning | `query_wolfram_alpha_short_answers` | Query the Wolfram Alpha Short Answers API. | +| | `query_wolfram_alpha_simple` | Query the Wolfram Alpha Simple API. | +| | `query_wolfram_alpha_show_steps` | Query the Wolfram Alpha Show Steps API. | +| | `query_wolfram_alpha_llm` | Query the Wolfram Alpha LLM API. | | *More services coming soon* | | More service functions are in development and will be added to AgentScope to further enhance its capabilities. | diff --git a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md index 23c145a05..052fc5a8e 100644 --- a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md +++ b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md @@ -46,6 +46,10 @@ | | `openai_image_to_text` | 使用 OpenAI API 根据图片生成文字。 | | `openai_text_to_audio` | 使用 OpenAI API 根据文本生成音频。 | | `openai_audio_to_text` | 使用OpenAI API将音频转换为文本。 +| 推理 | query_wolfram_alpha_short_answers | 查询 Wolfram Alpha Short Answers API。 | +| | query_wolfram_alpha_simple | 查询 Wolfram Alpha Simple API。 | +| | query_wolfram_alpha_show_steps | 查询 Wolfram Alpha Show Steps API。 | +| | query_wolfram_alpha_llm | 查询 Wolfram Alpha LLM API。 | | *更多服务即将推出* | | 正在开发更多服务功能,并将添加到 AgentScope 以进一步增强其能力。 | 关于详细的参数、预期输入格式、返回类型,请参阅[API文档](https://modelscope.github.io/agentscope/)。 From c064a74bc9c798e7de41664f94da4d7ea0e0232b Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:47:33 +0800 Subject: [PATCH 12/15] updated according to comment --- .../en/source/tutorial/204-service.md | 8 ++++---- .../zh_CN/source/tutorial/204-service.md | 8 ++++---- src/agentscope/service/__init__.py | 16 ++++++++-------- .../service/reasoning/wolfram_alpha.py | 18 +++++++++--------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/docs/sphinx_doc/en/source/tutorial/204-service.md b/docs/sphinx_doc/en/source/tutorial/204-service.md index 84f8d53ff..931aae436 100644 --- a/docs/sphinx_doc/en/source/tutorial/204-service.md +++ b/docs/sphinx_doc/en/source/tutorial/204-service.md @@ -49,10 +49,10 @@ The following table outlines the various Service functions by type. These functi | | `openai_image_to_text` | Convert text to image using OpenAI API | | `openai_text_to_audio` | Convert text to audio using OpenAI API | | `openai_audio_to_text` | Convert audio to text using OpenAI API -| Reasoning | `query_wolfram_alpha_short_answers` | Query the Wolfram Alpha Short Answers API. | -| | `query_wolfram_alpha_simple` | Query the Wolfram Alpha Simple API. | -| | `query_wolfram_alpha_show_steps` | Query the Wolfram Alpha Show Steps API. | -| | `query_wolfram_alpha_llm` | Query the Wolfram Alpha LLM API. | +| Reasoning | `wolfram_alpha_short_answers_query` | Query the Wolfram Alpha Short Answers API. | +| | `wolfram_alpha_simple_query` | Query the Wolfram Alpha Simple API. | +| | `wolfram_alpha_show_steps_query` | Query the Wolfram Alpha Show Steps API. | +| | `wolfram_alpha_llm_query` | Query the Wolfram Alpha LLM API. | | *More services coming soon* | | More service functions are in development and will be added to AgentScope to further enhance its capabilities. | diff --git a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md index 052fc5a8e..4cba146dd 100644 --- a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md +++ b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md @@ -46,10 +46,10 @@ | | `openai_image_to_text` | 使用 OpenAI API 根据图片生成文字。 | | `openai_text_to_audio` | 使用 OpenAI API 根据文本生成音频。 | | `openai_audio_to_text` | 使用OpenAI API将音频转换为文本。 -| 推理 | query_wolfram_alpha_short_answers | 查询 Wolfram Alpha Short Answers API。 | -| | query_wolfram_alpha_simple | 查询 Wolfram Alpha Simple API。 | -| | query_wolfram_alpha_show_steps | 查询 Wolfram Alpha Show Steps API。 | -| | query_wolfram_alpha_llm | 查询 Wolfram Alpha LLM API。 | +| 推理 | wolfram_alpha_short_answers_query | 查询 Wolfram Alpha Short Answers API。 | +| | wolfram_alpha_simple_query | 查询 Wolfram Alpha Simple API。 | +| | wolfram_alpha_show_steps_query | 查询 Wolfram Alpha Show Steps API。 | +| | wolfram_alpha_llm_query | 查询 Wolfram Alpha LLM API。 | | *更多服务即将推出* | | 正在开发更多服务功能,并将添加到 AgentScope 以进一步增强其能力。 | 关于详细的参数、预期输入格式、返回类型,请参阅[API文档](https://modelscope.github.io/agentscope/)。 diff --git a/src/agentscope/service/__init__.py b/src/agentscope/service/__init__.py index 3a86db8fd..7d6cd02ea 100644 --- a/src/agentscope/service/__init__.py +++ b/src/agentscope/service/__init__.py @@ -27,10 +27,10 @@ dblp_search_venues, ) from .reasoning.wolfram_alpha import ( - query_wolfram_alpha_short_answers, - query_wolfram_alpha_simple, - query_wolfram_alpha_show_steps, - query_wolfram_alpha_llm, + wolfram_alpha_short_answers_query, + wolfram_alpha_simple_query, + wolfram_alpha_show_steps_query, + wolfram_alpha_llm_query, ) from .multi_modality.dashscope_services import ( dashscope_image_to_text, @@ -107,10 +107,10 @@ def get_help() -> None: "openai_image_to_text", "openai_edit_image", "openai_create_image_variation", - "query_wolfram_alpha_short_answers", - "query_wolfram_alpha_simple", - "query_wolfram_alpha_show_steps", - "query_wolfram_alpha_llm", + "wolfram_alpha_short_answers_query", + "wolfram_alpha_simple_query", + "wolfram_alpha_show_steps_query", + "wolfram_alpha_llm_query", # to be deprecated "ServiceFactory", ] diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index 55c373611..5b4d2f9b8 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -10,7 +10,7 @@ ) -def query_wolfram_alpha_short_answers( +def wolfram_alpha_short_answers_query( api_key: str, query: str, ) -> ServiceResponse: @@ -40,7 +40,7 @@ def query_wolfram_alpha_short_answers( Example: .. code-block:: python - result = query_wolfram_alpha_short_answers( + result = wolfram_alpha_short_answers_query( "your_api_key", "What is the capital of France?" ) @@ -71,7 +71,7 @@ def query_wolfram_alpha_short_answers( ) -def query_wolfram_alpha_simple( +def wolfram_alpha_simple_query( api_key: str, query: str, save_path: str = "wolfram_alpha_result.png", @@ -103,7 +103,7 @@ def query_wolfram_alpha_simple( Example: .. code-block:: python - result = query_wolfram_alpha_simple( + result = wolfram_alpha_simple_query( "your_api_key", "Plot sin(x)", save_path="path/to/save/result.png" @@ -140,14 +140,14 @@ def query_wolfram_alpha_simple( }, ) except Exception as e: - logger.error(f"Error in query_wolfram_alpha_simple: {str(e)}") + logger.error(f"Error in wolfram_alpha_simple_query: {str(e)}") return ServiceResponse( status=ServiceExecStatus.ERROR, content={"error": str(e)}, ) -def query_wolfram_alpha_show_steps( +def wolfram_alpha_show_steps_query( api_key: str, query: str, ) -> ServiceResponse: @@ -178,7 +178,7 @@ def query_wolfram_alpha_show_steps( Example: .. code-block:: python - result = query_wolfram_alpha_show_steps( + result = wolfram_alpha_show_steps_query( "your_api_key", "Solve x^2 + 2x + 1 = 0" ) @@ -250,7 +250,7 @@ def query_wolfram_alpha_show_steps( ) -def query_wolfram_alpha_llm( +def wolfram_alpha_llm_query( api_key: str, query: str, ) -> ServiceResponse: @@ -278,7 +278,7 @@ def query_wolfram_alpha_llm( Example: .. code-block:: python - result = query_wolfram_alpha_llm( + result = wolfram_alpha_llm_query( "your_api_key", "3 densest elemental metals" ) From 99fed5523bbe4b7028ed17814056f34b9cb28da0 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:52:58 +0800 Subject: [PATCH 13/15] Update 204-service.md --- docs/sphinx_doc/en/source/tutorial/204-service.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/sphinx_doc/en/source/tutorial/204-service.md b/docs/sphinx_doc/en/source/tutorial/204-service.md index 931aae436..97de98f1b 100644 --- a/docs/sphinx_doc/en/source/tutorial/204-service.md +++ b/docs/sphinx_doc/en/source/tutorial/204-service.md @@ -49,10 +49,10 @@ The following table outlines the various Service functions by type. These functi | | `openai_image_to_text` | Convert text to image using OpenAI API | | `openai_text_to_audio` | Convert text to audio using OpenAI API | | `openai_audio_to_text` | Convert audio to text using OpenAI API -| Reasoning | `wolfram_alpha_short_answers_query` | Query the Wolfram Alpha Short Answers API. | -| | `wolfram_alpha_simple_query` | Query the Wolfram Alpha Simple API. | -| | `wolfram_alpha_show_steps_query` | Query the Wolfram Alpha Show Steps API. | -| | `wolfram_alpha_llm_query` | Query the Wolfram Alpha LLM API. | +| Reasoning | `wolfram_alpha_short_answers_query` | Query the Wolfram Alpha Short Answers API and return the answer as a string. | +| | `wolfram_alpha_simple_query` | Query the Wolfram Alpha Simple API and return the answer as an image. | +| | `wolfram_alpha_show_steps_query` | Query the Wolfram Alpha Show Steps API and return the step-by-step solution as a string. | +| | `wolfram_alpha_llm_query` | Query the Wolfram Alpha LLM API and return the answer from the LLM as a string. | | *More services coming soon* | | More service functions are in development and will be added to AgentScope to further enhance its capabilities. | From 908922583576e0a3e8962f02445be18233806ad7 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:57:03 +0800 Subject: [PATCH 14/15] update the docs --- docs/sphinx_doc/en/source/tutorial/204-service.md | 2 +- docs/sphinx_doc/zh_CN/source/tutorial/204-service.md | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/sphinx_doc/en/source/tutorial/204-service.md b/docs/sphinx_doc/en/source/tutorial/204-service.md index 97de98f1b..1c3744955 100644 --- a/docs/sphinx_doc/en/source/tutorial/204-service.md +++ b/docs/sphinx_doc/en/source/tutorial/204-service.md @@ -50,7 +50,7 @@ The following table outlines the various Service functions by type. These functi | | `openai_text_to_audio` | Convert text to audio using OpenAI API | | `openai_audio_to_text` | Convert audio to text using OpenAI API | Reasoning | `wolfram_alpha_short_answers_query` | Query the Wolfram Alpha Short Answers API and return the answer as a string. | -| | `wolfram_alpha_simple_query` | Query the Wolfram Alpha Simple API and return the answer as an image. | +| | `wolfram_alpha_simple_query` | Query the Wolfram Alpha Simple API and return the answer as a PNG image. | | | `wolfram_alpha_show_steps_query` | Query the Wolfram Alpha Show Steps API and return the step-by-step solution as a string. | | | `wolfram_alpha_llm_query` | Query the Wolfram Alpha LLM API and return the answer from the LLM as a string. | diff --git a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md index 4cba146dd..8b8561b22 100644 --- a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md +++ b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md @@ -46,10 +46,10 @@ | | `openai_image_to_text` | 使用 OpenAI API 根据图片生成文字。 | | `openai_text_to_audio` | 使用 OpenAI API 根据文本生成音频。 | | `openai_audio_to_text` | 使用OpenAI API将音频转换为文本。 -| 推理 | wolfram_alpha_short_answers_query | 查询 Wolfram Alpha Short Answers API。 | -| | wolfram_alpha_simple_query | 查询 Wolfram Alpha Simple API。 | -| | wolfram_alpha_show_steps_query | 查询 Wolfram Alpha Show Steps API。 | -| | wolfram_alpha_llm_query | 查询 Wolfram Alpha LLM API。 | +| 推理 | wolfram_alpha_short_answers_query | 查询 Wolfram Alpha Short Answers API, 并以字符串形式返回答案。 | +| | wolfram_alpha_simple_query | 查询 Wolfram Alpha Simple API,并以PNG图片形式返回答案。。 | +| | wolfram_alpha_show_steps_query | 查询 Wolfram Alpha Show Steps API,并返回逐步解决方案作为字符串。 | +| | wolfram_alpha_llm_query | 查询 Wolfram Alpha LLM API,并将LLM返回的答案作为字符串返回。 | | *更多服务即将推出* | | 正在开发更多服务功能,并将添加到 AgentScope 以进一步增强其能力。 | 关于详细的参数、预期输入格式、返回类型,请参阅[API文档](https://modelscope.github.io/agentscope/)。 From 46767111c46a509bc19b5978375b9c1d60ddd3d2 Mon Sep 17 00:00:00 2001 From: zyzhang1130 <36942574+zyzhang1130@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:08:11 +0800 Subject: [PATCH 15/15] updated the doc --- .../en/source/tutorial/204-service.md | 6 +-- .../zh_CN/source/tutorial/204-service.md | 6 +-- .../service/reasoning/wolfram_alpha.py | 47 +++++++++++-------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/docs/sphinx_doc/en/source/tutorial/204-service.md b/docs/sphinx_doc/en/source/tutorial/204-service.md index 1c3744955..71ecd902d 100644 --- a/docs/sphinx_doc/en/source/tutorial/204-service.md +++ b/docs/sphinx_doc/en/source/tutorial/204-service.md @@ -49,10 +49,10 @@ The following table outlines the various Service functions by type. These functi | | `openai_image_to_text` | Convert text to image using OpenAI API | | `openai_text_to_audio` | Convert text to audio using OpenAI API | | `openai_audio_to_text` | Convert audio to text using OpenAI API -| Reasoning | `wolfram_alpha_short_answers_query` | Query the Wolfram Alpha Short Answers API and return the answer as a string. | +| Reasoning | `wolfram_alpha_short_answers_query` | Query the Wolfram Alpha Short Answers API and return a brief answer in text. | | | `wolfram_alpha_simple_query` | Query the Wolfram Alpha Simple API and return the answer as a PNG image. | -| | `wolfram_alpha_show_steps_query` | Query the Wolfram Alpha Show Steps API and return the step-by-step solution as a string. | -| | `wolfram_alpha_llm_query` | Query the Wolfram Alpha LLM API and return the answer from the LLM as a string. | +| | `wolfram_alpha_show_steps_query` | Query the Wolfram Alpha Show Steps API and return the step-by-step solution in text. | +| | `wolfram_alpha_llm_query` | Query the Wolfram Alpha LLM API and return the answer from the LLM in text. | | *More services coming soon* | | More service functions are in development and will be added to AgentScope to further enhance its capabilities. | diff --git a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md index 8b8561b22..d270f3ec2 100644 --- a/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md +++ b/docs/sphinx_doc/zh_CN/source/tutorial/204-service.md @@ -46,10 +46,10 @@ | | `openai_image_to_text` | 使用 OpenAI API 根据图片生成文字。 | | `openai_text_to_audio` | 使用 OpenAI API 根据文本生成音频。 | | `openai_audio_to_text` | 使用OpenAI API将音频转换为文本。 -| 推理 | wolfram_alpha_short_answers_query | 查询 Wolfram Alpha Short Answers API, 并以字符串形式返回答案。 | +| 推理 | wolfram_alpha_short_answers_query | 查询 Wolfram Alpha Short Answers API, 并以文本形式返回简短的答案。 | | | wolfram_alpha_simple_query | 查询 Wolfram Alpha Simple API,并以PNG图片形式返回答案。。 | -| | wolfram_alpha_show_steps_query | 查询 Wolfram Alpha Show Steps API,并返回逐步解决方案作为字符串。 | -| | wolfram_alpha_llm_query | 查询 Wolfram Alpha LLM API,并将LLM返回的答案作为字符串返回。 | +| | wolfram_alpha_show_steps_query | 查询 Wolfram Alpha Show Steps API,并以文本形式返回返回逐步答案。 | +| | wolfram_alpha_llm_query | 查询 Wolfram Alpha LLM API,并将LLM返回的答案作为文本形式返回。 | | *更多服务即将推出* | | 正在开发更多服务功能,并将添加到 AgentScope 以进一步增强其能力。 | 关于详细的参数、预期输入格式、返回类型,请参阅[API文档](https://modelscope.github.io/agentscope/)。 diff --git a/src/agentscope/service/reasoning/wolfram_alpha.py b/src/agentscope/service/reasoning/wolfram_alpha.py index 5b4d2f9b8..09bade7c6 100644 --- a/src/agentscope/service/reasoning/wolfram_alpha.py +++ b/src/agentscope/service/reasoning/wolfram_alpha.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- """Query Wolfram Alpha API""" import os -from typing import Any, Dict # Corrected import order -import requests # Corrected import order -from loguru import logger # Corrected import order +from typing import Any, Dict +import requests +from loguru import logger from agentscope.service.service_response import ( ServiceResponse, ServiceExecStatus, @@ -16,11 +16,12 @@ def wolfram_alpha_short_answers_query( ) -> ServiceResponse: """ Query the Wolfram Alpha Short Answers API. The Short Answers API returns - a single plain text result directly from Wolfram|Alpha. In general, this - text is taken directly from the Result pod of Wolfram|Alpha output. This - API type is designed to deliver brief answers in the most basic - format possible. Suitable for queries such as simple knowledge/facts - retrieval. See + a single plain text result directly from Wolfram|Alpha. + + In general, this text is taken directly from the Result pod of + Wolfram|Alpha output. This API type is designed to deliver brief + answers in the most basic format possible. Suitable for queries + such as simple knowledge/facts retrieval. See https://products.wolframalpha.com/short-answers-api/documentation for more details. @@ -79,6 +80,7 @@ def wolfram_alpha_simple_query( """ Query the Wolfram Alpha Simple API. The Simple API generates full Wolfram|Alpha output in a universally viewable image format. + Suitable for queries such as knowledge/facts retrieval. See https://products.wolframalpha.com/simple-api/documentation for more details. @@ -152,12 +154,14 @@ def wolfram_alpha_show_steps_query( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha Show Steps API. An extension of the - Full Results API, the Show Steps API gives direct access to - Wolfram|Alpha's full for queries in a variety of mathematical - and scientific subjects. These explanations of computed answers - are designed to provide clarity and understanding to the - end user and are especially useful in educational and + Query the Wolfram Alpha Show Steps API, gives step-by-step solution + in text. + + An extension of the Full Results API, the Show Steps API gives + direct access to Wolfram|Alpha's full for queries in a variety + of mathematical and scientific subjects. These explanations of + computed answers are designed to provide clarity and understanding + to the end user and are especially useful in educational and training applications. see https://products.wolframalpha.com/show-steps-api/documentation for more details. @@ -255,12 +259,15 @@ def wolfram_alpha_llm_query( query: str, ) -> ServiceResponse: """ - Query the Wolfram Alpha LLM API. The LLM API is built for - use specifically with large language models and chat products. - Although the majority of data available through the - Wolfram|Alpha website is also available through this API, - certain subjects may be restricted by default. see - https://products.wolframalpha.com/llm-api/documentation for more details. + Query the Wolfram Alpha LLM API and return solution as text. + + The LLM API is built for use specifically with large language + models and chat products. Although the majority of data + available through the Wolfram|Alpha website is also available + through this API, certain subjects may be restricted by default. + see + https://products.wolframalpha.com/llm-api/documentation + for more details. Args: api_key (`str`):