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

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
简介: 本期文章,我们将向大家展示如何使用AgentScope内置的ReAct智能体解决更为复杂的问题。

前言

在前几期的文章中,我们了解了如何利用AgentScope构建一个带有@功能的对话,以及如何搭建一个简单的五子棋游戏。在这些应用中,一个共性是大模型会直接根据提示产生回复,对于简单任务,或许大模型还能够处理。但当我们面对更加复杂的任务时,我们期待大模型变得更加“聪明”,能够应对更多样的场景,解决更加复杂的问题。本期文章,我们将向大家展示如何使用AgentScope内置的ReAct智能体解决更为复杂的问题。完整的代码和运行结果可以在链接(https://github.com/modelscope/agentscope/tree/main/examples/conversation_with_react_agent)中找到

欢迎关注AgentScope,在github上(https://github.com/modelscope/agentscope) 为我们star 🌟。在接下来几天,我们会继续推出一系列教程,让大家通过简单的代码搭建出有趣的多智能体应用!

背景:ReAct算法

ReAct算法来源于论文“ReAct:Synergizing Reasoning and Acting in Language Models”,发表在2023年的ICLR会议上。ReAct算法的核心是通过交替进行reasoning和acting的方式帮助智能体更好地应对和解决复杂问题。相较于一般的reasoning方法,能够根据当前的场景进行分析,做出更具有针对性的行动,并且提供了更好的可解释性和更高的容错度。

image.png

ReAct与其它reasoning算法的对比

准备工作

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

调用ReActAgent

ReAct算法中,Acting一般通过调用工具的方式进行,因此我们首先需要为智能体准备工具函数,然后再使用这些工具函数解决我们的具体问题。

第一步:准备工具函数

为了让大模型能够使用工具函数,我们需要首先对工具函数进行处理,这里的处理包括两个层面:

  1. 处理需要开发者填入的参数,例如API key,账户,密码等信息,使大模型可以直接调用该函数
  2. 生成大模型能够理解的函数说明,例如目前许多模型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
func_for_llm, func_desc = ServiceFactory.get(
  bing_search, 
  api_key=BING_API_KEY, 
  num_results=3
)

其中JSON schema格式的变量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, # or google_search,
    read_text_file,
    write_text_file, 
    ServiceFactory
)
# Deal with arguments that need to be input by developers
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,然后创建ReActAgent并输入对应的参数。

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, # set verbose to True to show the reasoning process
)

为了让大家更加清楚ReActAgent的运行原理,我们这里首先展示一下这个智能体的系统提示(system prompt),包含了身份的说明,工具函数说明和提示三个部分。

You are a helpful assistant.
The following tool functions are available 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)

下面我们将智能体打印出来的思考过程按照轮次拆分开进行分析

  • 第一轮
  • reasoning: 我们首先看到智能体分析了用户输入的问题,并且决定采用bing_search的工具函数进行调用
  • acting: 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': 'https://www.cnblogs.com/LittleHann/p/17541295.html', 'snippet': 'Aside from the Apple Remote, what other devices can control the program Apple Remote was originally designed to interact with? 我们可以看到该模型生成了任务解决轨迹(思考、行动)。'}, {'title': 'REACT:在大模型中协同推理与行动,使其能够解决推理和 ...', 'link': 'https://baijiahao.baidu.com/s?id=1775590643651365661', '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': 'https://www.promptingguide.ai/techniques/react', '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.'}]

  • 第二轮
##################### 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': 'https://support.apple.com/guide/tv/use-other-remotes-atvb0410f604/tvos', 'snippet': 'Table of Contents. Use other remotes to control Apple TV. 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': 'https://www.makeuseof.com/alternative-apple-tv-remotes/', 'snippet': "Fortunately, if you find yourself looking for alternatives to control your Apple TV, there are several good options for you. You can use other Apple devices to operate the Apple TV, or even affordable Bluetooth accessories. We'll highlight the best alternatives to the Apple TV remote. 1. Use Your iPhone or iPad to Control the Apple TV"}, {'title': 'Apple TV Siri Remote: How to control TVs and receivers - 9to5Mac', 'link': 'https://9to5mac.com/2021/05/27/apple-tv-siri-remote-control-tv-receiver/', 'snippet': 'The old Siri Remote and Apple TV Remote also feature this functionality, but without a dedicated power button, it’s less convenient and intuitive to use. Another benefit of HDMI-CEC is that...'}]

  • 第三轮
  • reasoning:在这一轮中,智能体总结了上一轮bing_search搜索出来的结果,并在“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智能体的任务,并进行了初步的尝试。该次尝试的完整的代码和运行结果可以在链接(https://github.com/modelscope/agentscope/tree/main/examples/conversation_with_react_agent)中找到。未来AgentScope也会持续添加新的算法以及样例,欢迎大家持续关注~

延伸阅读和资源

比赛

看到这里,如果你有好玩的想法,不如实践起来,还可以顺便参加下面的比赛~

image.png


点击即可跳转~

Create @ AI 创客松第四季 (aliyun.com)

相关文章
|
1月前
|
决策智能
Multi-Agent实践第2期: @智能体 你怎么看?
我们将带你体验如何实现一个更具互动性的多智能体群聊:你可以直接"@"提及某个智能体来引发对话。
|
27天前
|
自然语言处理 算法 前端开发
Multi-Agent实践第5期:RAG智能体的应用:让AgentScope介绍一下自己吧
本期文章,我们将向大家展示如何使用AgentScope中构建和使用具有RAG功能的智能体,创造AgentScope助手群,为大家解答和AgentScope相关的问题。
|
15天前
|
搜索推荐 开发工具 决策智能
Agent调研--19类Agent框架对比(中)
Agent调研--19类Agent框架对比(中)
65 0
|
15天前
|
人工智能 前端开发 开发工具
Agent调研--19类Agent框架对比(上)
Agent调研--19类Agent框架对比(上)
198 0
|
15天前
|
人工智能 自然语言处理 开发工具
Agent调研--19类Agent框架对比(下)
Agent调研--19类Agent框架对比(下)
82 0
|
26天前
|
JSON 前端开发 决策智能
Multi-Agent实践第6期:面向智能体编程:狼人杀在AgentScope
本期文章,我们会介绍一下AgentScope的一个设计哲学(Agent-oriented programming)
|
1月前
|
小程序 API 决策智能
Multi-Agent实践第1期:5分钟上手AgentScope
阿里云与魔搭社区联合举办Create@AI创客松,邀请开发者探索基于多智能体的人机协作模式。活动提供资源支持和专家指导,获胜者可获得近5万元现金奖励及6亿次千问调用额度。参赛者需准备大模型API,如DashScope或OpenAI,使用AgentScope开源框架开发多智能体应用。立即报名参加:[报名链接](https//startup.aliyun.com/special/aihackathon4)。
|
3月前
|
缓存 前端开发 JavaScript
Angular Service Worker 在 PWA 应用 HTTP 交互中扮演的角色
Angular Service Worker 在 PWA 应用 HTTP 交互中扮演的角色
45 0

热门文章

最新文章