前言
在前几期的文章中,我们了解了如何利用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方法,能够根据当前的场景进行分析,做出更具有针对性的行动,并且提供了更好的可解释性和更高的容错度。
ReAct与其它reasoning算法的对比
准备工作
- 首先请按照GitHub中的说明(https://github.com/modelscope/agentscope)安装agentscope库
- 与之前的文章相似,我们需要根据模型类型准备相应的模型配置文件,AgentScope支持包括本地部署和第三方库等多种不同的模型API调用方式,部署与使用方法请参考我们的教程(https://modelscope.github.io/agentscope/en/tutorial/203-model.html)。这里我们以DashScope提供的通义千问 API为例。
// model_configs.json [ { "model_type": "dashscope_chat", "config_name": "tongyi_qwen_config", "model_name": "qwen-max", "api_key": "************" // 在这里填写你DashScope中的API-KEY } ]
调用ReActAgent
ReAct算法中,Acting一般通过调用工具的方式进行,因此我们首先需要为智能体准备工具函数,然后再使用这些工具函数解决我们的具体问题。
第一步:准备工具函数
为了让大模型能够使用工具函数,我们需要首先对工具函数进行处理,这里的处理包括两个层面:
- 处理需要开发者填入的参数,例如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 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也会持续添加新的算法以及样例,欢迎大家持续关注~
延伸阅读和资源
- 完整的中文文档和教程:https://modelscope.github.io/agentscope/zh_CN/index.html
- AgentScope的GitHub示例库:https://github.com/modelscope/agentscope/tree/main/examples
- 论文及研究资料:https://arxiv.org/abs/2402.14034
- 加入我们的讨论组:扫描GitHub项目首页(https://github.com/modelscope/agentscope/blob/main/README_ZH.md)的二维码。
比赛
看到这里,如果你有好玩的想法,不如实践起来,还可以顺便参加下面的比赛~
点击即可跳转~
Create @ AI 创客松第四季 (aliyun.com)