如何提升大模型Agent的能力 ——LLM Agent框架 Modelscope-Agent 实战

本文涉及的产品
交互式建模 PAI-DSW,每月250计算时 3个月
模型训练 PAI-DLC,100CU*H 3个月
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
简介: 本文介绍Agent到底是什么 ,如何进行优化,以及如何使用Agen框架。

大型语言模型(LLM)的出现带火了Agent。利用LLM理解人类意图、生成复杂计划并且能够自主行动的能力。Agent具有无与伦比的能力,能够做出类似于人类复杂性的决策和完成一些复杂的工作。目前市面上已经出现非常多得Agent框架:XAgent, AutoGPT、BabyAGI、CAMEL、MetaGPT、AutoGen、DSPy、AutoAgents、OpenAgents、Agents、AgentVerse、ChatDev。他们将LLM作为核心引擎,给模型增加记忆,装上工具,去完成一些复杂的任务,使得LLM更像一个人去工作,不得不说Agent的威力还是很值得期待。

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

ModelScope-Agent框架

ModelScope-Agent是一个通用的、可定制的Agent框架,以大语言模型作为核心驱动的应用开发框架,笔者之前在王鹏:Qwen 7B大模型ReAct Prompt详解以及LLM 智能体Agent实战 这篇文章中详细的介绍了一下Agent的原理,本质上还是prompt工程,通过Prompt去驱动模型进行 计划和工具调用。

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

ModelScope-Agent

如何提升Agent的能力呢

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

  • 1.使用更强大的LLM
  • 2.设计更合理的Prompt和Agent Pipeline
  • 3.使用特定数据进行微调

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

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

XAgent

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

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

XAgent

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 帮我进行读取并可视化

name

count

a

1

b

2

c

3


结语

最后可以看看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

点击直达modelscope-agent开源地址,欢迎star~

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

相关文章
|
1月前
|
人工智能 自然语言处理 TensorFlow
134_边缘推理:TensorFlow Lite - 优化移动端LLM部署技术详解与实战指南
在人工智能与移动计算深度融合的今天,将大语言模型(LLM)部署到移动端和边缘设备已成为行业发展的重要趋势。TensorFlow Lite作为专为移动和嵌入式设备优化的轻量级推理框架,为开发者提供了将复杂AI模型转换为高效、低功耗边缘计算解决方案的强大工具。随着移动设备硬件性能的不断提升和模型压缩技术的快速发展,2025年的移动端LLM部署已不再是遥远的愿景,而是正在成为现实的技术实践。
|
6月前
|
人工智能 安全 API
不到100行代码,实现一个简易通用智能LLM Agent
本文将分享如何使用不到 100 行的 Python 代码,实现一个具备通用智能潜力的简易 LLM Agent。你将看到整个实现过程——从核心原理、提示(Prompt)调优、工具接口设计到主循环交互,并获得完整复现代码的详细讲解。
1265 101
不到100行代码,实现一个简易通用智能LLM Agent
|
7月前
|
数据采集 算法 数据挖掘
CLIMB自举框架:基于语义聚类的迭代数据混合优化及其在LLM预训练中的应用
英伟达提出的CLIMB框架,是一种自动化优化大型语言模型(LLM)预训练数据混合的创新方法。通过语义嵌入与聚类技术,CLIMB能系统地发现、评估并优化数据混合策略,无需人工干预。该框架包含数据预处理、迭代自举及最优权重确定三大阶段,结合小型代理模型与性能预测器,高效搜索最佳数据比例。实验表明,基于CLIMB优化的数据混合训练的模型,在多项推理任务中显著超越现有方法,展现出卓越性能。此外,研究还构建了高质量的ClimbMix数据集,进一步验证了框架的有效性。
281 0
CLIMB自举框架:基于语义聚类的迭代数据混合优化及其在LLM预训练中的应用
|
1月前
|
机器学习/深度学习 缓存 自然语言处理
11_文本总结实战:用LLM浓缩长文章
在信息爆炸的时代,面对海量的长文本内容,如何高效地提取核心信息成为一项关键技能。文本摘要作为自然语言处理(NLP)中的重要任务,能够将冗长的文本压缩为保留核心信息的简短摘要,极大地提高了信息获取和处理的效率。随着大语言模型(LLM)技术的快速发展,特别是基于Transformer架构的模型如BART的出现,文本摘要技术取得了突破性进展。
|
8月前
|
人工智能 数据可视化 API
36.7K star!拖拽构建AI流程,这个开源LLM应用框架绝了!
`Flowise` 是一款革命性的低代码LLM应用构建工具,开发者通过可视化拖拽界面,就能快速搭建基于大语言模型的智能工作流。该项目在GitHub上线不到1年就斩获**36.7K星标**,被开发者誉为"AI时代的乐高积木"。
461 8
|
8月前
|
数据采集 人工智能 分布式计算
MCP+Hologres+LLM搭建数据分析Agent
本文探讨了LLM大模型在数据分析领域的挑战,并介绍了Hologres结合MCP协议和LLM搭建数据分析Agent的解决方案。传统LLM存在实时数据接入能力不足、上下文记忆短等问题,而Hologres通过高性能数据分析能力和湖仓一体支持,解决了这些痛点。MCP协议标准化了LLM与外部系统的连接,提升集成效率。文中详细描述了如何配置Hologres MCP Server与Claude Desktop集成,并通过TPC-H样例数据展示了分析流程和效果。最后总结指出,该方案显著提高了复杂分析任务的实时性和准确性,为智能决策提供支持。
|
7月前
|
机器学习/深度学习 人工智能 算法
零训练成本优化LLM: 11种LLM权重合并策略原理与MergeKit实战配置
随着大语言模型快速发展,如何优化性能同时降低计算成本成为关键问题。本文系统介绍了11种零训练成本的LLM权重合并策略,涵盖线性权重平均(Model Soup)、球面插值(SLERP)、任务算术、TIES-Merging等方法,通过MergeKit工具提供实战配置示例。无论研究者还是开发者,都能从中找到高效优化方案,在有限资源下实现模型性能显著提升。
289 10
零训练成本优化LLM: 11种LLM权重合并策略原理与MergeKit实战配置
|
7月前
|
机器学习/深度学习 人工智能 算法
RAGEN:RL训练LLM推理新范式!开源强化学习框架让Agent学会多轮决策
RAGEN是一个基于StarPO框架的开源强化学习系统,通过马尔可夫决策过程形式化Agent与环境的交互,支持PPO、GRPO等多种优化算法,显著提升多轮推理训练的稳定性。
711 5
RAGEN:RL训练LLM推理新范式!开源强化学习框架让Agent学会多轮决策
|
5月前
|
机器学习/深度学习 自然语言处理 算法
万字长文详解|DLRover LLM Agent:大模型驱动的高效集群资源调优
本文介绍了DLRover LLM Agent,展示了基于 LLM 上下文学习能力的优化算法设计理念以及在DLRover 资源调优上的应用方法和效果。
|
8月前
|
机器学习/深度学习 人工智能 算法
SWEET-RL:基于训练时信息的多轮LLM代理强化学习框架
SWEET-RL是一种基于训练时信息的逐步评估算法,显著提升了多轮大型语言模型(LLM)代理在强化学习中的成功率。相比现有方法,SWEET-RL将成功率提高6%,使小型开源模型如Llama-3.1-8B达到甚至超越GPT-4O等大型专有模型性能。通过非对称Actor-Critic结构、创新优势函数参数化及两阶段训练流程,SWEET-RL优化了信用分配机制与泛化能力,降低了计算成本。ColBench基准测试显示,SWEET-RL在后端编程和前端设计任务中表现卓越,为AI代理训练技术带来突破性进展。
264 2
SWEET-RL:基于训练时信息的多轮LLM代理强化学习框架

热门文章

最新文章