Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neurosymbolic QA with WolframAlpha API #396

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/sphinx_doc/en/source/tutorial/204-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
4 changes: 4 additions & 0 deletions docs/sphinx_doc/zh_CN/source/tutorial/204-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)。
Expand Down
35 changes: 35 additions & 0 deletions examples/neurosymbolic_QA/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


# Neurosymbolic Mathematical Reasoning Question Answering with AgentScope

This example will show:
- How to set up and use the ReActAgent from AgentScope for neurosymbolic reasoning.
- How to integrate various Wolfram Alpha query functions into a neurosymbolic question-answering system, particularly for question involves mathetical reasoning.
- How to use AgentScope Studio to visualize conversations between the agents.

## Background
LLMs excel at many tasks, but currently still fall short in rigorous reasoning, like doing complex mathematical calcuations.
This script demonstrates a neurosymbolic question-answering system using AgentScope, which integrates various external APIs like Wolfram Alpha to assist LLM agents in solving problems that involve mathetical reasoning. The LLM agent first formulates the mathematical problems that need to be solved based on its interpretation of the problem statement, then it uses Wolfram Alpha to solve the mathematical problems, synthesizes the solution and finally returns a reply.

Two LLM agents are set up in this demonstration: one with access to Wolfram Alpha APIs for solving mathematical problems, and another without such access (for comparison), aiming to solve a game theory problem on finding mixed Nash equilibria. The agent without access to the tools will often fail to give the correct solution.



## Tested Models

These models are tested in this example. For other models, some modifications may be needed.
- `gpt-4o-mini` - With the given temperature setting. Note that with temperature tuning or more advanced models such as claude-3.5-sonnet, the agent is able to solve the given problem without relying on external tools. The example servers as a proof-of-concept for the scenario that, given a sufficiently different question for the given model capability, using external tools can increase the chance of successfully solving the problem,


## Prerequisites

To run this example successfully, ensure you meet the following requirements:
- The latest AgentScope library installed.
- Valid API keys for Wolfram Alpha and Anthropic API, which should be replaced in the placeholders within the script.
- [Optional] Knowledge in game theory and understanding of Nash equilibria for interpreting the agent's reasoning trace.



## Running the Example
This example has been set to run with AgentScope Studio by default. After starting running the script, open `http://127.0.0.1:5000` with your browser (Chorme is preferred).
For more information on AgentScope Studio see `http://doc.agentscope.io/en/tutorial/209-gui.html`
121 changes: 121 additions & 0 deletions examples/neurosymbolic_QA/neurosymbolic_QA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
"""
This module implements a neurosymbolic question-answering system
using AgentScope.

It sets up two student agents: one with access to Wolfram Alpha APIs
for solving mathematical problems, and another without such access.
The agents are tasked with solving game theory problems,
specifically finding mixed Nash equilibria.

The module demonstrates the use of ReActAgent, ServiceToolkit, and various
Wolfram Alpha query functions from AgentScope.
"""
from agentscope.service import ServiceToolkit
from agentscope.message import Msg
import agentscope
from agentscope.agents import DialogAgent
from agentscope.service import (
query_wolfram_alpha_short_answers,
query_wolfram_alpha_simple,
query_wolfram_alpha_show_steps,
query_wolfram_alpha_llm,
)
from agentscope.agents import ReActAgent


agentscope.init(
# ...
project="xxx",
name="xxx",
studio_url="http://127.0.0.1:5000", # The URL of AgentScope Studio
)


YOUR_MODEL_CONFIGURATION = [
{
"model_type": "openai_chat",
"config_name": "gpt",
"model_name": "gpt-4o-mini",
"api_key": "",
"generate_args": {
"temperature": 0.1,
},
},
]


# Initialize the ServiceToolkit and register the TripAdvisor API functions
service_toolkit = ServiceToolkit()
service_toolkit.add(
query_wolfram_alpha_short_answers,
api_key="",
) # Replace with your actual TripAdvisor API key
service_toolkit.add(
query_wolfram_alpha_simple,
api_key="",
) # Replace with your actual TripAdvisor API key
service_toolkit.add(
query_wolfram_alpha_show_steps,
api_key="",
) # Replace with your actual TripAdvisor API key
service_toolkit.add(
query_wolfram_alpha_llm,
api_key="",
) # Replace with your actual TripAdvisor API key

agentscope.init(model_configs=YOUR_MODEL_CONFIGURATION)

student_agent1 = DialogAgent(
name="Student1",
sys_prompt="""You are a smart student. You are given problem to solve.""",
model_config_name="gpt", # replace by your model config name
)

student_agent2 = ReActAgent(
name="Student2",
sys_prompt="""You are a smart student. You are given problem to solve.
Use the approprite wolfram alpha APIs to help you solve simultaneous
equations/calculations. Note that wolfram alpha apis can only help you
solve an explicitly given equation or calculation. Don't use them
if you are not solving equations or doing calculations.""",
model_config_name="gpt",
service_toolkit=service_toolkit,
verbose=True, # set verbose to True to show the reasoning trace
)


x1 = Msg(
"system",
"""Solve the problem: For the following games,
compute all mixed Nash equilibria. In each table cell,
the number on the left is the payoff to the row player, and
the number on the right is the payoff to the column player.
| | L | R |
|---|-----|-----|
| T | 7,1 | 2,2 |
| B | 0,5 | 3,1 |""",
role="user",
)

student_agent1.speak(x1)
x1 = student_agent1(x1)


x2 = Msg(
"system",
"""Solve the problem: For the following games,
compute all mixed Nash equilibria. In each table cell,
the number on the left is the payoff to the row player, and
the number on the right is the payoff to the column player.
| | L | R |
|---|-----|-----|
| T | 7,1 | 2,2 |
| B | 0,5 | 3,1 |
You need to formulate the relevant equations
and ask it to solve it for you.""",
role="user",
)

student_agent2.speak(x2)
x2 = student_agent2(x2)
10 changes: 10 additions & 0 deletions src/agentscope/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
]
Loading