用通俗易懂的方式讲解:如何提升大模型 Agent 的能力?

2024-01-08 22:44:13

大型语言模型(LLM)的出现带火了Agent。利用LLM理解人类意图、生成复杂计划并且能够自主行动的能力。Agent具有无与伦比的能力,能够做出类似于人类复杂性的决策和完成一些复杂的工作。

目前市面上已经出现非常多得Agent框架:XAgent, AutoGPT、BabyAGI、CAMEL、MetaGPT、AutoGen、DSPy、AutoAgents、OpenAgents、Agents、AgentVerse、ChatDev。他们将LLM作为核心引擎,给模型增加记忆,装上工具,去完成一些复杂的任务,使得LLM更像一个人去工作,不得不说Agent的威力还是很值得期待。

接下来我就使用一个 Agent 框架去介绍一下,Agent到底是什么 ,如何进行优化,以及如何使用 Agent 框架。

ModelScope-Agent框架

ModelScope-Agent是一个通用的、可定制的Agent框架,以大语言模型作为核心驱动的应用开发框架,通过Prompt去驱动模型进行计划和工具调用。

框架的好处将有记忆能力,计划能力,工具调用能力的Agent封装成了一个类,用户只需要使用简单的接口就能实现将知识和工具注入到Agent类当中。整个Agent的运行会遵循下方流程:准备好记忆和工具,然后进行计划,最终按照计划使用记忆,调用工具,返回结果。

在这里插入图片描述

通俗易懂讲解大模型系列

如何提升Agent的能力呢

之前一直知道大模型有计划推理能力,但是做过一些实验,有时候自定义的工具经常会调用失败,而且有时候结果也不尽如人意。如何在特定的场景进行一些能力增强呢?或者问题问大一点,如何提升Agent的能力呢。主要有3个方面的工作可以做:

  • 1.使用更强大的LLM

  • 2.设计更合理的Prompt和Agent Pipeline

  • 3.使用特定数据进行微调

1.更强的大语言模型LLM(使用gpt4)

下方是XAgent框架的使用建议,建议用gpt4来作为Agent的核心引擎,不然效果会大打折扣。可以看出其实很多大模型的计划推理和反思能力其实不足以去完成一个复杂任务。

在这里插入图片描述

2.更优的Prompt和工作流程设计(prompt工程和pipeline设计)

下方是XAgent的流程设计,包含了任务拆解,结果验证与反思这一个循环,更符合人类思维模式的进化习惯了。当然速度也会更慢,如果是特定领域,也可以设计特定领域的工作流程去将Agent打造成某一领域的专家(也就是GPTS)。
在这里插入图片描述

3.更多的推理,决策,计划,反思类数据的训练。(llm微调)

下方是千问的为了让模型具备工具调用能力微调数据,可以看出就是将模型推理过程数据化了。

[
  {
    "conversations": [
      {
        "from": "user",
        "value": "Answer the following questions as best you can. You have access to the following APIs:\\n\\ngoogle_search: Call this tool to interact with the Google Search API. What is the Google Search API useful for? Google Search is a general search engine that can be used to access the internet, query encyclopedia knowledge, and stay informed about current events. Format the arguments as a JSON object. Parameters: [{\\"name\\": \\"search_query\\", \\"description\\": \\"Search keywords or phrases\\", \\"required\\": true, \\"schema\\": {\\"type\\": \\"string\\"}}]\\n\\ncode_interpreter: Call this tool to interact with the Code Interpreter API. What is the Code Interpreter API useful for? Code interpreter that can execute Python code.Enclose the code within triple backticks (`) at the beginning and end of the code. Parameters: [{\\"name\\": \\"code\\", \\"description\\": \\"Code to be executed\\", \\"required\\": true, \\"schema\\": {\\"type\\": \\"string\\"}}]\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [google_search, code_interpreter]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: Who is jay chou? And who is his wife?"
      },
      {
        "from": "assistant",
        "value": "Thought: I need to google Jay Chou.\\nAction: google_search\\nAction Input: {\\"search_query\\": \\"Jay Chou\\"}\\nObservation: Jay Chou is ...\\nThought: I need to google Jay's wife.\\nAction: google_search\\nAction Input: {\\"search_query\\": \\"Jay Chou's wife\\"}\\nObservation: Jay Chou's wife is ...\\nThought: I now know the final answer\\nFinal Answer: Jay Chou is ... His wife is ..."
      }
    ]
  },
  {
    "conversations": [
      {
        "from": "user",
        "value": "Answer the following questions as best you can. You have access to the following APIs:\\n\\ngoogle_search: Call this tool to interact with the Google Search API. What is the Google Search API useful for? Google Search is a general search engine that can be used to access the internet, query encyclopedia knowledge, and stay informed about current events. Format the arguments as a JSON object. Parameters: [{\\"name\\": \\"search_query\\", \\"description\\": \\"Search keywords or phrases\\", \\"required\\": true, \\"schema\\": {\\"type\\": \\"string\\"}}]\\n\\ncode_interpreter: Call this tool to interact with the Code Interpreter API. What is the Code Interpreter API useful for? Code interpreter that can execute Python code.Enclose the code within triple backticks (`) at the beginning and end of the code. Parameters: [{\\"name\\": \\"code\\", \\"description\\": \\"Code to be executed\\", \\"required\\": true, \\"schema\\": {\\"type\\": \\"string\\"}}]\\n\\nUse the following format:\\n\\nQuestion: the input question you must answer\\nThought: you should always think about what to do\\nAction: the action to take, should be one of [google_search, code_interpreter]\\nAction Input: the input to the action\\nObservation: the result of the action\\n... (this Thought/Action/Action Input/Observation can be repeated zero or more times)\\nThought: I now know the final answer\\nFinal Answer: the final answer to the original input question\\n\\nBegin!\\n\\nQuestion: 123+456=?"
      },
      {
        "from": "assistant",
        "value": "Thought: I need to compute the result using Code Interpreter.\\nAction: code_interpreter\\nAction Input:\\n```py\\n123 + 456\\n```\\nObservation: 579\\nThought: I now know the final answer\\nFinal Answer: 579"
      },
      {
        "from": "user",
        "value": "Multiply the result by 2."
      },
      {
        "from": "assistant",
        "value": "Thought: Code Interpreter is helpful for answering this question.\\nAction: code_interpreter\\nAction Input:\\n```py\\n579 * 2\\n```\\nObservation: 1158\\nThought: I now know the final answer\\nFinal Answer: 1158"
      },
      {
        "from": "user",
        "value": "You are so smart, Qwen."
      },
      {
        "from": "assistant",
        "value": "Thought: I now know the final answer\\nFinal Answer: Thank you."
      },
      {
        "from": "user",
        "value": "Please re-execute the code for computing my first question again."
      },
      {
        "from": "assistant",
        "value": "Thought: I need to re-compute the result.\\nAction: code_interpreter\\nAction Input:\\n```py\\n123 + 456\\n```\\nObservation: 579\\nThought: I now know the final answer\\nFinal Answer: 579"
      }
    ]
  }
]

ModelScope-Agent框架实战

下方实现了一个带记忆,能够使用code interpreter工具的一个agent,用到的大模型就是qwen-max

from modelscope_agent.agent import AgentExecutor
from modelscope_agent.tools import CodeInterpreter
from  modelscope_agent.agent_types import AgentType
from modelscope_agent.llm import LLMFactory
from langchain.embeddings import ModelScopeEmbeddings, DashScopeEmbeddings
from langchain.vectorstores import FAISS
from modelscope_agent.retrieve import ToolRetrieval, KnowledgeRetrieval
import faiss

from dotenv import load_dotenv  
import os
from modelscope.utils.config import Config
os.environ['DASHSCOPE_API_KEY'] = "你的千问key"
model_cfg = Config.from_file("./config/cfg_model_template.json")

model_name = 'qwen-max'
llm = LLMFactory.build_llm(model_name, model_cfg)

# 额外工具列表,这里只有一个code_interpreter
additional_tool_list = {
    CodeInterpreter.name: CodeInterpreter()
}

# 基于阿里云DashScope向量Embedding的配置
embeddings = DashScopeEmbeddings(model="text-embedding-v1")
# 构建知识库
file_path = 'qa.txt'
knowledge_retrieval = KnowledgeRetrieval.from_file(file_path, embeddings, FAISS)

# 构建Agent,需要传入llm,工具配置config以及工具检索
agent = AgentExecutor(llm, agent_type=AgentType.MRKL, additional_tool_list=additional_tool_list,tool_retrieval=False, knowledge_retrieval=knowledge_retrieval)
available_tool_list = ['code_interpreter']
agent.set_available_tools(available_tool_list)

# 接下来就可以进行agent.run了

检索示例——知识库检索

在这里插入图片描述

code interpreter 读取excel并可视化

给定下方一个execl 文档,希望agent使用python 帮我进行读取并可视化

namecount
a1
b2
c3

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

结语

最后可以看看agent的历史聊天记录,整个过程,包括模型的计划,推理和工具使用等还是大量使用了ReAct Prompt,没有特别的设计。这样我们从这一个简单的Agent 流,进一步认识到Agent是什么,同时也可以了解到从哪些方面进行进一步优化。

图片

[{'content': 'Answer the following questions as best you can. You have access '
             'to the following tools: `\\n'
             '\\n'
             'code_interpreter: code_interpreter API. Executes code on the '
             "user's machine, **in the users local environment**, and returns "
             'the output 输入参数: [{"name": "language", "description": "The '
             'programming language (required parameter to the `execute` '
             'function)", "required": true}, {"name": "code", "description": '
             '"The code to execute (required)", "required": true}]\\n'
             '\\n'
             'Use the following format:\\n'
             '\\n'
             'Question: the input question you must answer\\n'
             'Thought: you should always think about what to do\\n'
             'Action: the action to take, should be one of '
             "['code_interpreter']\\n"
             'Action Input: the input to the action\\n'
             'Observation: the result of the action\\n'
             '... (this Thought/Action/Action Input/Observation can be '
             'repeated zero or more times)\\n'
             'Thought: I now know the final answer\\n'
             'Final Answer: the final answer to the original input question\\n'
             '\\n'
             'Begin!\\n'
             "Question: 读取一个文件'./123.xlsx'\\n",
  'role': 'user'},
 {'content': 'Thought: 我需要使用代码来读取一个Excel文件。\\n'
             'Action: code_interpreter\\n'
             'Action Input:\\n'
             '```py\\n'
             'import pandas as pd\\n'
             '# 读取Excel文件\\n'
             "df = pd.read_excel('./123.xlsx')\\n"
             '# 显示数据框的前几行\\n'
             'df.head()\\n'
             '```\\n'
             'Observation: name  count\\n'
             '\\n'
             '0    a      1\\n'
             '\\n'
             '1    b      2\\n'
             '\\n'
             '2    c      3\\n'
             '\\n'
             '\\n'
             'Thought: 代码成功读取了Excel文件并显示了数据的前几行。\\n'
             "Final Answer: 文件'./123.xlsx'的内容如下:\\n"
             '\\n'
             '| name | count |\\n'
             '| ---- | ----- |\\n'
             '| a    | 1     |\\n'
             '| b    | 2     |\\n'
             '| c    | 3     |\\n'
             '\\n'
             '请注意,这只是一个示例,实际文件内容可能会有所不同。',
  'role': 'assistant'},
 {'content': 'Question: 然后帮我把结果可视化一下,并保存成./123.png\\n', 'role': 'user'},
 {'content': 'Thought: 我需要使用代码来创建一个数据的可视化并保存为PNG文件。\\n'
             'Action: code_interpreter\\n'
             'Action Input:\\n'
             '```py\\n'
             'import pandas as pd\\n'
             'import matplotlib.pyplot as plt\\n'
             '\\n'
             '# 读取Excel文件\\n'
             "df = pd.read_excel('./123.xlsx')\\n"
             '\\n'
             '# 创建图表\\n'
             "plt.bar(df['name'], df['count'])\\n"
             "plt.xlabel('Name')\\n"
             "plt.ylabel('Count')\\n"
             "plt.title('Name vs Count')\\n"
             '\\n'
             '# 保存图表为PNG文件\\n'
             "plt.savefig('./123.png')\\n"
             '```\\n'
             'Observation: <BarContainer object of 3 artists>\\n'
             '\\n'
             "Text(0.5, 0, 'Name')\\n"
             '\\n'
             "Text(0, 0.5, 'Count')\\n"
             '\\n'
             "Text(0.5, 1.0, 'Name vs Count')\\n"
             ' \\n'
             '\\n'
             "Thought: 代码成功创建了一个条形图并保存为'./123.png'。\\n"
             'Final Answer: '
             "我已经将'./123.xlsx'文件中的数据可视化为一个条形图,并将其保存为'./123.png'。这个图表显示了每个名字对应的计数。你可以查看'./123.png'文件来查看生成的图表。请注意,由于这是一个文本环境,我无法直接展示图像。但是在你的本地环境中,你应该能在相同目录下看到生成的图像文件。",
  'role': 'assistant'}]

参考

https://link.zhihu.com/?target=https%3A//github.com/OpenBMB/XAgent/nt/

https://github.com/modelscope/modelscope-agent

文章来源:https://blog.csdn.net/2301_78285120/article/details/135467142
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。