ReAct Agent智能体实战:思考与行动深度解析

2026-06-23阅读 0热度 0
ai 人工智能

在前几期的内容里,我们聊过如何用AgentScope搭建带@功能的对话,以及如何快速写出一个五子棋游戏。这些例子中,大模型基本上都是根据提示直接生成回复——对简单任务来说这确实够用,但一旦任务变得复杂,我们就希望模型能更“聪明”一点:能应对更多样的场景,也能解决更棘手的问题。

本期文章,就来讲讲怎么用AgentScope内置的ReAct智能体来应对这类复杂任务。完整的代码和运行结果,可以在这里找到:GitHub 示例

背景:ReAct算法

ReAct算法来自2023年ICLR会议上的一篇论文——《ReAct: Synergizing Reasoning and Acting in Language Models》。它的核心思路,是通过让模型交替进行“推理”和“行动”,来更灵活地应对并解决复杂问题。相比单纯的推理方法,ReAct能结合当前场景做更有针对性的操作,同时整个过程也更透明、容错性更高。

Multi-Agent实践第4期:智能体的“想”与“做”-ReAct Agent

准备工作

在正式开始之前,有两件事需要先完成:

  • 首先,根据GitHub中的说明安装agentscope库。
  • 其次,准备好模型配置文件。AgentScope支持本地部署和第三方库等多种模型调用方式,具体部署和使用方法可以参考官方教程。这里我们以DashScope提供的通义千问API为例。

模型配置文件的示例(JSON格式):

// model_configs.json
[
  {
    "model_type": "dashscope_chat",
    "config_name": "tongyi_qwen_config",
    "model_name": "qwen-max",
    "api_key": "************"  // 请在这里填写你的DashScope API-KEY
  }
]

调用ReActAgent

ReAct中的“行动”,通常是通过调用工具来实现的。所以我们第一步,就是为智能体准备好工具函数,然后利用这些函数来解决具体问题。

第一步:准备工具函数

为了让大模型能用上工具函数,我们需要做两件事:一是处理那些需要开发者手动填写的参数(比如API key、账户信息之类),使大模型可以直接调函数;二是生成大模型能理解的函数说明——目前很多模型API都要求JSON schema格式。

为了降低开发难度,AgentScope提供了ServiceFactory模块,可以轻松处理工具函数的转换。以bing_search函数为例,它需要以下几个参数:

  • query:查询的问题
  • api_key:Bing搜索的API Key
  • num_results:返回的搜索结果数量

在调用ServiceFactory.get时,我们可以先为bing_search指定api_key和num_results,这样就能生成一个只要求使用者输入query的函数,同时也会自动生成JSON schema格式的函数说明:

from agentscope.service import bing_search, ServiceFactory

func_for_llm, func_desc = ServiceFactory.get(
    bing_search, 
    api_key=BING_API_KEY, 
    num_results=3
)

生成的func_desc大体长这样:

{
  "type": "function",
  "function": {
    "name": "bing_search",
    "description": "Search question in Bing Search API and return the searching results",
    "parameters": {
      "type": "object",
      "properties": {
        "question": {
          "type": "string",
          "description": "The search query string."
        }
      },
      "required": ["question"]
    }
  }
}

用类似的方法,我们可以把不同的工具函数都转换成大模型能调用的形式。这里我们准备了以下几个工具:

from agentscope.service import (
    bing_search,
    read_text_file,
    write_text_file, 
    ServiceFactory
)

tools = [
    ServiceFactory.get(bing_search, api_key=BING_API_KEY, num_results=3),
    ServiceFactory.get(execute_python_code),
    ServiceFactory.get(read_text_file),
    ServiceFactory.get(write_text_file),
]

第二步:创建智能体

工具函数准备就绪后,创建ReAct智能体就非常简单了:初始化agentscope,然后传入参数即可:

from agentscope.agents import ReActAgent
import agentscope

agentscope.init(model_configs=YOUR_MODEL_CONFIGURATION)

agent = ReActAgent(
    name="assistant",
    model_config_name=YOUR_MODEL_CONFIGURATION_NAME,
    tools=tools,
    sys_prompt="You are a helpful assistant.",
    verbose=True,  # 设为True可以显示推理过程
)

为了让运行原理更直观,这里展示一下智能体的系统提示(system prompt)。它大致包含身份说明、工具函数说明和注意事项三部分:

You are a helpful assistant.

The following tool functions are a vailable in the format of
```
{index}. {function name}: {function description}
{argument name} ({argument type}): {argument description}
...
```

Tool Functions:
1. bing_search: Search question in Bing Search API and return the searching results
   question (string): The search query string.
2. execute_python_code: Execute Python code and capture the output. Note you must `print` the output to get the result.
   code (string): The Python code to be executed.
3. read_text_file: Read the content of the text file.
   file_path (string): The path to the text file to be read.
4. write_text_file: Write content to a text file.
   overwrite (boolean): Whether to overwrite the file if it already exists.
   file_path (string): The path to the file where content will be written.
   content (string): Content to write into the file.

Notice:
1. Fully understand the tool function and its arguments before using it.
2. Only use the tool function when it's necessary.
3. Check if the arguments you provided to the tool function is correct in type and value.
4. You can't take some problems for granted. For example, where you are, what's the time now, etc. But you can try to use the tool function to solve the problem.
5. If the function execution fails, you should analyze the error and try to solve it.

第三步:测试ReAct智能体能力

我们用ReAct论文中的经典问题来测试一下:"Aside from the Apple Remote, what other device can control the program Apple Remote was originally designed to interact with?"

from agentscope.message import Msg

msg_question = Msg(
    name="user", 
    content="Aside from the Apple Remote, what other device can control the program Apple Remote was originally designed to interact with?", 
    role="user"
)

res = agent(msg_question)

智能体打印出来的思考过程,我们可以按轮次拆开来分析:

第一轮

  • 推理:智能体先分析了用户的问题,决定调用bing_search。
  • 行动:bing_search返回了搜索结果,但这次返回的内容和预期的答案完全不相关。因为ReAct算法本身太火了,这个经典问题搜索到的结果,全都是关于ReAct算法的介绍!这个结果确实挺意外,也很能说明问题——接下来看看智能体怎么应对。
##################### ITER 1, STEP 1: REASONING ######################
assistant: {
  "thought": "I need to search for information about what other devices can control the program that the Apple Remote was originally designed to interact with.",
  "speak": "Let me find that information for you.",
  "function": [
    {
      "name": "bing_search",
      "arguments": {
        "question": "What devices can control the program Apple Remote was designed for?"
      }
    }
  ]
}
####################### ITER 1, STEP 2: ACTION #######################
>>> Executing function bing_search ...
>>> END
system: Execution Results:
1. bing_search:
[EXECUTE STATUS]: SUCCESS
[EXECUTE RESULT]: [
  {'title': '《ReAct: SYNERGIZING REASONING AND ACTING IN ...', 'link': '...', 'snippet': 'Aside from the Apple Remote, what other devices can control the program Apple Remote was originally designed to interact with? 我们可以看到该模型生成了任务解决轨迹(思考、行动)。'},
  {'title': 'REACT:在大模型中协同推理与行动,使其能够解决推理和 ...', 'link': '...', 'snippet': 'Q:Aside from the Apple Remote, what other device can control the program Apple Remote was originally designed to intect with? 除了苹果遥控器之外,还有哪些设备可以控制最初设计用于连接的苹果遥控器所配合的程序?'},
  {'title': 'ReAct Prompting | Prompt Engineering Guide', 'link': '...', 'snippet': 'Aside from the Apple Remote, what other devices can control the program Apple Remote was originally designed to interact with? Note that in-context examples are also added to the prompt but we exclude that here for simplicity.'}
]

第二轮

  • 推理:智能体马上意识到了问题——搜索没有得到明确答案。于是它调整了搜索策略,把查询改写成了“What devices can control Apple TV other than Apple Remote”。
  • 行动:这次返回了相关的搜索结果。
##################### ITER 2, STEP 1: REASONING ######################
assistant: {
  "thought": "The search results did not provide a clear answer to the question. I need to refine the search query to get more specific results.",
  "speak": "I'm sorry, I couldn't find a clear answer. Let me try a different search.",
  "function": [
    {
      "name": "bing_search",
      "arguments": {
        "question": "What devices can control Apple TV other than Apple Remote?"
      }
    }
  ]
}
####################### ITER 2, STEP 2: ACTION #######################
>>> Executing function bing_search ...
>>> END
system: Execution Results:
1. bing_search:
[EXECUTE STATUS]: SUCCESS
[EXECUTE RESULT]: [
  {'title': 'Use other remotes to control Apple TV - Apple Support', 'link': '...', 'snippet': 'You can use a supported TV or receiver remote, a network-based remote for home-control systems, or an infrared remote (commonly known as a universal remote) to control Apple TV.'},
  {'title': '4 Siri Remote Alternatives You Can Use to Control Your Apple TV - MUO', 'link': '...', 'snippet': '... you can use other Apple devices to operate the Apple TV, or even affordable Bluetooth accessories. ...'},
  {'title': 'Apple TV Siri Remote: How to control TVs and receivers - 9to5Mac', 'link': '...', 'snippet': '...'}
]

第三轮

  • 推理:智能体总结了搜索结果,在“speak”字段给出了最终回答。
##################### ITER 3, STEP 1: REASONING ######################
assistant: {
  "thought": "The search results provided a clear answer to the question. Other than the Apple Remote, you can use a supported TV or receiver remote, a network-based remote for home-control systems, an infrared remote (commonly known as a universal remote), or other Apple devices like an iPhone or iPad to control the Apple TV.",
  "speak": "Other than the Apple Remote, you can use a supported TV or receiver remote, a network-based remote for home-control systems, an infrared remote (commonly known as a universal remote), or other Apple devices like an iPhone or iPad to control the Apple TV."
}

这个例子非常有意思——它不仅展示了ReAct算法如何处理错误信息、如何自纠正并给出正确答案,也让我们看到了在AgentScope中使用ReActAgent解决实际问题有多方便。

总结

到这一步,我们已经完成了用AgentScope创建ReAct智能体的过程,并且做了一次初步的测试。整个过程的完整代码和运行结果,还是可以在GitHub 示例中找到。未来AgentScope也会持续加入新算法和更多的样例,值得持续关注。

免责声明

本网站新闻资讯均来自公开渠道,力求准确但不保证绝对无误,内容观点仅代表作者本人,与本站无关。若涉及侵权,请联系我们处理。本站保留对声明的修改权,最终解释权归本站所有。

相关阅读

更多
欢迎回来 登录或注册后,可保存提示词和历史记录
登录后可同步收藏、历史记录和常用模板
注册即表示同意服务条款与隐私政策