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

news2024/9/20 18:30:04

大型语言模型(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的运行会遵循下方流程:准备好记忆和工具,然后进行计划,最终按照计划使用记忆,调用工具,返回结果。

在这里插入图片描述

通俗易懂讲解大模型系列

  • 用通俗易懂的方式讲解:使用 Mistral-7B 和 Langchain 搭建基于PDF文件的聊天机器人

  • 用通俗易懂的方式讲解:ChatGPT 开放的多模态的DALL-E 3功能,好玩到停不下来!

  • 用通俗易懂的方式讲解:结合检索和重排序模型,改善大模型 RAG 效果明显

  • 用通俗易懂的方式讲解:基于扩散模型(Diffusion),文生图 AnyText 的效果太棒了

  • 用通俗易懂的方式讲解:在 CPU 服务器上部署 ChatGLM3-6B 模型

  • 用通俗易懂的方式讲解:ChatGLM3-6B 功能原理解析

  • 用通俗易懂的方式讲解:使用 LangChain 和大模型生成海报文案

  • 用通俗易懂的方式讲解:一个强大的 LLM 微调工具 LLaMA Factory

  • 用通俗易懂的方式讲解:ChatGLM3-6B 部署指南

  • 用通俗易懂的方式讲解:LangChain Agent 原理解析

  • 用通俗易懂的方式讲解:HugggingFace 推理 API、推理端点和推理空间使用详解

  • 用通俗易懂的方式讲解:使用 LangChain 封装自定义的 LLM,太棒了

  • 用通俗易懂的方式讲解:使用 FastChat 部署 LLM 的体验太爽了

  • 用通俗易懂的方式讲解:基于 Langchain 和 ChatChat 部署本地知识库问答系统

  • 用通俗易懂的方式讲解:使用 Docker 部署大模型的训练环境

  • 用通俗易懂的方式讲解:在 Ubuntu 22 上安装 CUDA、Nvidia 显卡驱动、PyTorch等大模型基础环境

  • 用通俗易懂的方式讲解:Llama2 部署讲解及试用方式

  • 用通俗易懂的方式讲解:LangChain 知识库检索常见问题及解决方案

  • 用通俗易懂的方式讲解:基于 LangChain 和 ChatGLM2 打造自有知识库问答系统

  • 用通俗易懂的方式讲解:代码大模型盘点及优劣分析

  • 用通俗易懂的方式讲解:Prompt 提示词在开发中的使用

  • 用通俗易懂的方式讲解:万字长文带你入门大模型

如何提升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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1367563.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

如何使用PR制作抖音视频?抖音短视频创作素材剪辑模板PR项目工程文件

如何使用PR软件制作抖音视频作品&#xff1f;Premiere Pro 抖音短视频创作素材剪辑模板PR项目工程文件。 3种分辨率&#xff1a;10801920、10801350、10801080。 来自PR模板网&#xff1a;https://prmuban.com/37058.html

双向逆变器流程

逆变过程 反向充电过程 首先AC整流&#xff0c;然后经过LLC电路进行DC-DC转换&#xff0c;这样就可以给电池充电了。也就是市电通过上面的电路就可以存储到电池里面。能量存储到电池里面&#xff0c;后面需要用的时候&#xff0c;也可以通过一定电路把能量释放出来。这就是逆变…

20240107让Firefly的AIO-3399J开发板的Android11下配置为默认1080p录像

20240107让Firefly的AIO-3399J开发板的Android11下配置为默认1080p录像 2024/1/7 23:01 开发板&#xff1a;Firefly的AIO-3399J【RK3399】 SDK&#xff1a;rk3399-android-11-r20211216.tar.xz【Android11】 Android11.0.tar.bz2.aa【ToyBrick】 Android11.0.tar.bz2.ab Androi…

江科大-stm32-B站系统初识笔记P2

文章目录 一&#xff1a;ARM是什么二&#xff1a;关键字介绍 推荐视频&#xff1a;《 STM32入门教程-2023版 细致讲解 中文字幕》 一&#xff1a;ARM是什么 ①&#xff1a; ST – 意法半导体 M – Microelectronics 微电子 32 – 总线宽度 ARM架构: Cortex-A:Application 应用…

linux常见操作,and一些练习题加线上练习网站,无须配置linux.持续更新中。。。。

文章目录 cd命令相对路径特殊的路径表达符和cd一起使用pwdmore 查看文件内容支持翻页小技巧clear用户&#xff0c;用户权限 and 用户的切换如何创建用户 ls和通配符的使用利用通配符 *grep 过滤管道符 |如何学习Linux在线练习网站 https://www.lanqiao.cn/courses/1 cd命令 cd…

TrueNAS数据共享——SMB共享日志审计

日志审计对于事后溯源十分重要&#xff0c;所以使用TrueNAS共享时&#xff0c;开启Samba audit审计日志功能很有必要。Full_Audit 允许记录共享上的所有操作&#xff0c;如创建文件、删除文件、更改路径… 一、编辑SMB服务 1、进入smb服务编辑 服务--找到SMB--动作--高级选项…

Notepad++ v7.7.1 安装及添加插件

1、notepad_v7.7.1.zip npp.7.7.1.Installer.x64.exe npp.7.7.1.Installer.x86.exe notepad_v7.7.1.ziphttps://www.123pan.com/s/VTMXjv-X6H6v.html 2、notepad插件包_64bit_4.zip ComparePlugin ---->文件对比插件 ComparePlugin.dllNppFTP ---->FTP、FTPES和SFTP …

python自动化运维管理拓扑

目录 1、简介 2、实验环境 3、拓扑图 4、需求及其代码 4.1、测试连通性 4.2、远程登陆 4.3、配置loopback 4.4、监控内存使用率 4.5、自动化巡检内存使用率 4.6、自动化配置snmp服务 4.7、提取分析字符串 &#x1f343;作者介绍&#xff1a;双非本科大三网络工程专业…

记一次生产事故排查

背景&#xff1a;刚接手一个新工程&#xff0c;是一个给国内top级医院开发的老项目&#xff0c;因为历史原因&#xff0c;代码质量略低&#xff0c;测试难度略高。 上线很久的功能&#xff0c;最近一直频繁的爆发各种问题&#xff0c;经排查发现都是因为在业务过程中im聊天账号…

Hyperledger Fabric 生成组织身份解析

fabric 版本 2.4.1 Fabric 网络通过证书和密钥来管理和认证成员身份&#xff0c;经常需要生成证书文件。通常这些操作可以使用 PKI 服务&#xff08;如 Fabric-CA&#xff09;或者 OpenSSL 工具来实现&#xff08;针对单个证书的签发&#xff09;。为了方便批量管理组织证书&am…

网页设计工作室网站Web前端制作个人网页(html+css+javascript)网页设计网站模板采用DIV CSS布局制作,网页作品有多个页面

网页设计工作室网站Web前端制作个人网页(htmlcssjavascript)网页设计网站模板采用DIV CSS布局制作&#xff0c;网页作品有多个页面 【网页设计工作室网站Web前端制作个人网页(htmlcssjavascript)网页设计网站模板采用DIV CSS布局制作&#xff0c;网页作品有多个页面】 https://…

赋能软件开发:生成式AI在优化编程工作流中的应用与前景

随着人工智能&#xff08;AI&#xff09;技术的快速发展&#xff0c;特别是生成式AI模型如GPT-3/4的出现&#xff0c;软件开发行业正经历一场变革&#xff0c;这些模型通过提供代码生成、自动化测试和错误检测等功能&#xff0c;极大地提高了开发效率和软件质量。 本文旨在深入…

stm32---输入捕获实验实操(巨详细)

这次来分享上次没说完的输入捕获的知识点 实验中用到两个引脚&#xff0c;一个是通用定时器 TIM3 的通道 1&#xff0c;即 PA6&#xff0c;用于输出 PWM 信号&#xff0c;另一 个是高级控制定时器 TIM1 的通道 1&#xff0c;即 PA8&#xff0c;用于 PWM 输入捕获&#xff0c;实…

第19课 在Android环境中使用FFmpeg和openCV进行开发的一般步骤

在上节课&#xff0c;根据模板文件我们对在Android环境中使用FFmpeg和openCV进行开发有了一个初步的体验&#xff0c;这节课&#xff0c;我们来具体看一下其工作流程。 1.程序的入口 与VS2013程序开发类似&#xff0c;Android程序开发也有一个入口&#xff0c;在这个模板中&a…

Docker 部署后端项目自动化脚本

文章目录 开机自启动docker打包后端项目Dockerfile文件脚本文件使用 开机自启动docker systemctl enable docker打包后端项目 这里的项目位置是target同级目录 1.在项目下面新建一个bin目录 新建一个package.txt 写入下方代码后 后缀改为.bat echo off echo. echo [信息] 打…

WPF 导航界面悬浮两行之间的卡片 漂亮的卡片导航界面 WPF漂亮渐变颜色 WPF漂亮导航头界面 UniformGrid漂亮展现

在现代应用程序设计中&#xff0c;一个漂亮的WPF导航界面不仅为用户提供视觉上的享受&#xff0c;更对提升用户体验、增强功能可发现性和应用整体效率起到至关重要的作用。以下是对WPF漂亮导航界面重要性的详尽介绍&#xff1a; 首先&#xff0c;引人入胜的首页界面是用户与软…

C语言爬虫程序采集58商铺出租转让信息

为了找到一个好店铺好位置&#xff0c;往往要花费很大精力和财力过去寻找&#xff0c;就有有某些出租平台但是一个个查找下来也是十分麻烦&#xff0c;所以我利用我们的C语言基础&#xff0c;给大家写个商品转租的爬虫程序&#xff0c;让找店铺不在那么费时费力&#xff0c;至少…

命令行模式的rancher如何安装?

在学习kubectl操作的时候&#xff0c;发现rancher也有命令行模式&#xff0c;学习整理记录此文。 说明 rancher 命令是 Rancher 平台提供的命令行工具&#xff0c;用于管理 Rancher 平台及其服务。 前提 已经参照前文安装过了rancher环境了&#xff0c;拥有了自己的k8s集群…

基于SSM的校园二手交易管理系统的设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 目录…

使用HttpSession和过滤器实现一个简单的用户登录认证的功能

这篇文章分享一下怎么通过session结合过滤器来实现控制登录访问的功能&#xff0c;涉及的代码非常简单&#xff0c;通过session保存用户登录的信息&#xff0c;如果没有用户登录的话&#xff0c;会在过滤器中处理&#xff0c;重定向回登录页面。 创建一个springboot项目&#…