解密Prompt系列31. LLM Agent之从经验中不断学习的智能体

发布时间:2026/8/27 18:27:56
解密Prompt系列31. LLM Agent之从经验中不断学习的智能体 前言Agent智能体的工作流可以简单分成两种一种是固定的静态工作流一种是智能体自主决策的动态工作流。静态流程的Agent举几个例子例如新闻热点追踪推送Agent每日新论文摘要总结Agent它们的优点是可控稳定可复现缺点是一种流程基本只能固定适配一种场景就像工厂的流水线。而动态流程的Agent也叫自主智能体例如AutoGPTBabyAgent它们自主感知环境基于观测进行决策并做出行动然后基于行动结果进行反思并给出下一步行动。优点自然是’理论上’可以泛化到任意场景不需要基于经验的预置工作流的抽象但缺点就是不可控不稳定不能复现且任务完成率有限尤其是在非通用的垂直领域。影响自主智能体在垂直领域任务完成率的有以下2个亟待解决的问题模型自主能力进化失败是成功之母模型该如何基于失败的任务流进行反思和探索一步步提高自己的任务完成率呢模型自主能力获得最初模型如何掌握该领域的技能之前的方案多数是依赖SFT通过人工或者人工模型来构建领域样本来教会模型部分能力。说白了还是人手把手教模型。那这一步能否自主化让模型在trial and error里面自主进行学习呢毕竟人类也是靠实验和探索一步步掌握新的技能的。其实以上两个问题都可以通过Self-Reflection from past experience来解决那问题就转变成了如何获得past-experiencepast-experience如何转化成经验如何在新的推理中使用这些经验。这一章会介绍三个模型自主探索学习和经验总结的方案分别是AppAgentTrial and Error和AutoGuideAPPAgentAppAgent: Multimodal Agents as Smartphone Usershttps://github.com/mnotgod96/AppAgentAPPAgent是腾讯实验室推出出的和Andriod手机自主交互的智能体整体方案和上一章我们讲过的WebVoyager的方案类似使用多模态大模型和SOM页面元素分割来识别每一步模型和页面的哪些元素进行交互。而自主学习的部分论文基于模型的前期自主探索来构建工具说明书帮助模型了解每款APP的使用从而提高推理阶段的任务完成率。这里论文在9个android app上进行了测试一些测试任务如下那如何使用模型来自主生成APP操作说明书呢类比人类在使用一个新工具时通过Trial and Error来不断更新自己对工具的认知和使用方式这里的模型探索也是如此。论文先生成了一组基于APP的任务指令然后基于每个指令模型会对APP的使用进行自主探索每一步模型的输入包括手机交互的4种功能的功能介绍包括点击、键入、长按、左右滑动任务描述历史的交互行为的总结当前手机应用页面的截图每一步模型的输出包括如下图Thought完成任务下一步做啥Action使用以上哪个功能的Function Calling或者FINISHSummary:加入最新的Action对所有历史行为进行总结作为下一步的输入APP操作的具体prompt省略细节如下self_explore_task_templateYou are an agent that is trained to complete certain tasks on a smartphone. You will be given a screenshot of a smartphone app. The interactive UI elements on the screenshot are labeled with numeric tags starting from 1. You can call the following functions to interact with those labeled elements to control the smartphone: 1. tap(element: int) 功能介绍。。。 2. text(text_input: str) 功能介绍。。。 3. long_press(element: int) 功能介绍。。。 4. swipe(element: int, direction: str, dist: str) 功能介绍。。。 The task you need to complete is to task_description. Your past actions to proceed with this task are summarized as follows: last_act Now, given the following labeled screenshot, you need to think and call the function needed to proceed with the task. Your output should include three parts in the given format: Observation: Describe what you observe in the image Thought: To complete the given task, what is the next step I should do Action: The function call with the correct parameters to proceed with the task. If you believe the task is completed or there is nothing to be done, you should output FINISH. You cannot output anything else except a function call or FINISH in this field. Summary: Summarize your past actions along with your latest action in one or two sentences. Do not include the numeric tag in your summary You can only take one action at a time, so please directly call the function.然后基于以上模型自主探索生成的行为序列我们可以记录每一步操作前后收集页面的变化并基于变化让大模型总结该操作步骤究竟是干什么的从而生成APP中每个按钮的交互说明书。以下是点击类操作的说明书prompt如果一个按钮被使用多次则模型会不断更新该按钮的说明文档。tap_doc_template I will give you the screenshot of a mobile app before and after tapping the UI element labeled with the number ui_element on the screen. The numeric tag of each element is located at the center of the element. Tapping this UI element is a necessary part of proceeding with a larger task, which is to task_desc. Your task is to describe the functionality of the UI element concisely in one or two sentences. Notice that your description of the UI element should focus on the general function. For example, if the UI element is used to navigate to the chat window with John, your description should not include the name of the specific person. Just say: Tapping this area will navigate the user to the chat window. Never include the numeric tag of the UI element in your description. You can use pronouns such as the UI element to refer to the element.这样基于前期的模型探索我们可以得到每个APP上各种按钮的一份操作说明书。然后在推理阶段模型会同时使用当前手机界面的UI前期生成的说明书5种交互行为说明历史操作来生成下一步的交互操作。论文验证了前期自主探索形成的说明书对模型的任务完成准确率有很大的提升几乎可以逼近基于人工探索形成的说明书Watching Demos以及直接手工编写说明书(Manually Crafted)的水平。STESimulated Trial and ErrorLLMs in the Imaginarium: Tool Learning through Simulated Trial and Errorhttps://github.com/microsoft/simulated-trial-and-error上面APPAgent帮助模型自我学习如何进行前端交互微软提出的STE是针对后端API交互让模型通过前期的多轮API交互学习API调用并通过In-Context-Learning或者SFT使用前期探索的结果帮助模型更好的使用API来完成任务。这里STE使用了BmTools的API作为工具池前期的工具探索阶段分成以下3个步骤Query生成基于工具名称和工具描述让模型生成一条能使用该API回答的问题。prompt指令如下Your taskisto answer the users queryasbest you can.You have access to the following tools which you can use via API call tohelpwithyour response:{api_descriptions}Now you have the chance to explore the available APIs.You can do this by1)synthesizing some natural user query that calling the API couldhelp,and2)trying to respond to the user querywiththehelpof the APIs.Here,you can focus on queries that only require calling the API once.Now,firstinputyour synthesized user query.You should make the query natural-forexample,tryto avoid using the provided API descriptionsorAPI namesinthe query,asthe user doesnotknow what APIs you have access to.Alsotryto make the queryasspecificaspossible.Input just the user query alone;do NOT solve the queryfornow.User Query:工具调用推理基于以上生成的Query工具描述模型使用ReACT范式来生成工具调用语句。把推理语句解析成API调用后调用API并获取返回值然后让模型基于返回进行反思。这一步可以最多重复4次直到模型判断API调用结果可以回答用户提问并且每次都会使用之前N-1次的推理结果和观测作为上文也就是上图中的Short-Memory部分来帮助模型从错误中进行迭代和优化。这里论文使用ChatGPTprompt如下Now,tryto respond to the query using the available APIs.Theformatyou use the APIisby specifying1)Action:the API function name youd like to call2)Action Input:theinputparameters of the API callina json stringformat.The result of the API call will be returned startingwithObservation:.Remember that you should only perform a SINGLE action at a time,do NOTreturnalistof multiple actions.Reminder:1)the only values that should followAction:are:{api_names}2)use the following json stringformatforthe API arguments:Action Input:{{key_1:value_1,...key_n:value_n,}}Remember to ALWAYS use the followingformat:Thought:you should always think about what to donextAction:the API function name Action Input:theinputparameters of the API callinjson stringformatObservation:thereturnresult of the API call.Thisiswhat I will provide youwith;you donotneed to repeat itinyour response....(this Thought/Action/Action Input/Observation can repeat N times)Thought:I now know the final answer Final Answer:the response to the user query Begin! Remember that your response should never startwithObservation:since thatiswhat I will provide youwith.Once you have enough information,please immediately use \nThought:I now know the final answer\nFinal Answer:User Query(the same you just synthesized):{query}扩展Query: 这里论文针对每个API会进行15次query生成和尝试并且为了提高query的多样性在生成新query时会加入历史已经生成的query和每个query模型是否成功调用工具完成。也就是上图Long-Term Memory的部分判断query是否成功执行也是使用大模型prompt这里使用了能力更强的GPT4。Now you know a bit more about the API.You can synthesize another user query to explore the API a bit furtherandconsolidate your understanding of the API,based on things that you discovered about this API.Again,justinputthe user query alone;do NOT solve the queryfornow.User Query:每个API会重复15次以上的步骤2和步骤3并记录每一次尝试的路径用于训练或者后续的In-Context-Learning。这里我们只关注ICL的方案因为泛化性更好能更快拓展新工具和新场景。和上面APPAgent不同的这里的ICL不是使用前期探索生成的工具说明书而是直接使用模型调用工具的历史操作类似于案例。当用户有新的提问时会基于query的Embedding(SentenceBert召回前期探索阶段中最相似的15个query和最终模型的API调用结果作为推理上文进行工具推理。效果上论文对比了多个模型直接进行工具调用推理使用前期探索的案例作为上文和构建样本进行SFT的效果。小模型还是需要微调才能获得最高的任务完成率但GPT4这类能力强的模型只需要ICL就可以达到很好的任务完成率以及不论是SFT还是ICL相比BaseLine都有很明显的效果提升。AutoGuideAutoGuide: Automated Generation and Selection of State-Aware Guidelines for Large Language Model Agents对比AppAgent是把经验在APP按钮的操作级别进行总结形成工具说明书推理时召回当前工具的说明书。STE是直接使用原始经验推理时召回相关历史经验作为上文而AutoGuide则是通过对比成功和失败的经验在每一步的状态级别进行总结在推理时召回相关的状态和状态经验作为上文。通俗点说AppAgent是使用说明书STE是操作案例集AutoGuide是使用指南。想要构建并使用指南AutoGuide包含三个核心模块状态总结模块State Summarization指南抽取模块(Guideline Extraction)和指南召回模块。论文针对不同的Agent场景设计了不同的状态总结和抽取prompt这里还是用我们上一章刚提过的webagent中的WebArena数据集为例分别说下两个模块State Summarization状态总结模块是基于模型的规划链路ThoughtAction来总结模型处于的状态。具体来说是基于同一个任务的成功和失败的两条行为链路定位到两个链路首次出现不同行为的时间节点T使用T的链路行为作为输入使用以下prompt进行状态总结。举个例子以下的任务中两条行为链路是在Action1的时候出现了差异则会使用Action1之前的观察和行为作为输入(current trajectory) 进行状态总结。这里得到的状态应该是You are on the List of forum PageGuideline Extraction得到状态后则需要生成该状态下的行为指南这里同样分别用到成功和失败的行为链路以及前面的状态总结作为输入来生成指南具体prompt如下同样是上面的例子针对状态You are on the List of forum Page以上prompt得到的指南是“if you want to navigate to a specific forum, you can click on the link that exactly matches the forum name you are looking for.”在不断基于state生成guideline的过程中论文还会使用大模型prompt对相似的状态进行合并最终得到的是一个字典{state:guidelines}。以下是webArena场景中最终生成的状态指南示例Apply Guideline at Test基于以上获取的状态和状态指南在推理阶段每一步执行会先使用State Summarization模块对当前状态进行总结然后基于当前的状态去构建好的状态指南中先定位相似的状态这里使用了和上面状态消重合并相同的大模型prompt然后基于定位到的状态获取所有的相关指南。如果指南数量太多则使用下面的prompt对指南进行筛选只保留Top-K。然后基于这Top-K指南进行下一步思考和行为的推理。最后感谢你们的阅读和喜欢我收藏了很多技术干货可以共享给喜欢我文章的朋友们如果你肯花时间沉下心去学习它们一定能帮到你。因为这个行业不同于其他行业知识体系实在是过于庞大知识更新也非常快。作为一个普通人无法全部学完所以我们在提升技术的时候首先需要明确一个目标然后制定好完整的计划同时找到好的学习方法这样才能更快的提升自己。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】一、全套AGI大模型学习路线AI大模型时代的学习之旅从基础到前沿掌握人工智能的核心技能二、640套AI大模型报告合集这套包含640份报告的合集涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师还是对AI大模型感兴趣的爱好者这套报告合集都将为您提供宝贵的信息和启示。三、AI大模型经典PDF籍随着人工智能技术的飞速发展AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型如GPT-3、BERT、XLNet等以其强大的语言理解和生成能力正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。四、AI大模型商业化落地方案五、面试资料我们学习AI大模型必然是想找到高薪的工作下面这些面试题都是总结当前最新、最热、最高频的面试题并且每道题都有详细的答案面试前刷完这套面试题资料小小offer不在话下。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】