一文探秘LLM应用开发(24)-Prompt(架构模式Agent)

技术

动手点关注

picture.image

干货不迷路

picture.image

本文旨在让无大模型开发背景的工程师或者技术爱好者无痛理解大语言模型应用开发的理论和主流工具,因此会先从与LLM应用开发相关的基础概念谈起,并不刻意追求极致的严谨和完备,而是从直觉和本质入手,结合笔者调研整理及消化理解,帮助大家能够更容易的理解LLM技术全貌,大家可以基于本文衍生展开,结合自己感兴趣的领域深入研究。若有不准确或者错误的地方也希望大家能够留言指正。

本文体系完整,内容丰富,由于内容比较多,分多次连载

第一部分 基础概念

1.机器学习场景类别

2.机器学习类型(LLM相关)

3.深度学习的兴起

4.基础模型

第二部分 应用挑战

1.问题定义与基本思路

2.基本流程与相关技术

1)Tokenization与Embbeding

2)向量数据库

3)finetune(微调)

4)模型部署与推理

5)prompt

6)编排与集成

7)预训练

第三部分 场景案例

常用参考

第二部分 应用挑战

2.基本流程与相关技术

4)Prompt

在前面的内容里,我们提到过要使用模型完成下游任务,有两种方式,一种是通过收集标记样本针对不同的任务进行指令微调,另一种方式便是大模型特有的,可以通过将指令以对话的方式提供给模型,期待模型能够给我们返回预期的结果。相较于前者,后者具有更高的灵活性,使用成本也更低,因此,这一方式成了如今大语言模型区别于传统NLP模型的重要标志。

在本章你将学习到:

1)Prompt,In-Context-Learning,Prompt engineering等相关概念

2)如何写好一个Prompt及相关Prompt调试工具

3)基于Prompt催生的一些新的编程范式

基于Prompt催生的一些新的编程范式

LLM技术的兴起,带来的是AI应用开发范式的改变。而这一切的变化来自于LLM独特的操作模式。通过Prompt可以让大模型完成各种不同的任务,从传统的NLP任务,如情感分析,摘要总结,到大模型特有的逻辑推理不一而足。正是如此,MLOPS到LLMOPS的应用开发方式转变来自于以数据为中心的模型训练推理的闭环构建向以finetune模型为中心的应用构建转变。

picture.image

在这样的大背景下,业内结合大模型的能力特点及层出不穷的Prompt技术,在此形成了一些工程化应用的架构范式。所谓架构范式,就是一种将一些共性的场景问题用一种共识性的解决框架,在此框架上解决问题可以提高成功率,降低实现难度。最典型的基于Prompt技术构建的LLM应用架构模式为RAG和Agent,在接下来内容中将分别对这两种模式进行介绍。

RAG

当前LLM应用最火的实现方式莫过于RAG,RAG最火的核心原因在于做到了大模型的能力与场景需要的结合。受限于大模型的context window大小限制、性能,信息的及时性以及难以克服的幻觉,内容可信等多方面技术要求,加之于大模型的交互可以简化为问答式聊天的场景交互,使得RAG成为知识问答,任务辅助(Copilot,使用工具+多轮对话)等的核心架构。

picture.image

笔者曾经在专栏《LLM应用架构》里对RAG有比较多的介绍,并持续跟进这一领域的进展,欢迎阅读,在本文中就不再赘述。

LLM应用架构之检索增强(RAG)的缘起与架构介绍

改进召回(Retrieval)和引入重排(Reranking)提升RAG架构下的LLM应用效果

引入元数据(metadata)提升RAG架构下LLM应用的效果和管控精度

Langchain下利用MutiVector Retriever更好支持RAG架构下表格文字混合内容问答

Agent

要说RAG是LLM应用到场景的一种妥协形态,那么Agent就是真正迈向AGI的变革式的新方案。不论是RAG还是进阶的Copilot,其本质是为了提高人类效率,是既有业务模式上的体验增强,其每一步都需要和人类协同完成。

picture.image

而Agent则是为了替代人而出现,给定一个既有目标或复杂任务,让机器自己去思考如何分解完成,而整个过程无需人类参与,甚至到了Muti-Agent阶段,Agent可以有效分工协作进而替代一个组织,完成更为复杂的任务。公司运营将更多通过使用 AI Agent 和工具进行,而不全是人类员工。这种AI+员工的新模式将对未来组织形式产生深远的影响。

picture.image

可以看到,Agent与RAG的本质区别在于RAG等LLMChain的解决问题的核心流程是人工确定的,LLM在过程中只起到局部作用,而Agent的整个解决问题的流程都是由AI自主确定的,LLM在此期间起着决定性作用,没有了LLM那么这个应用将无法工作,可以说Agent才是真正的高纯度AI-Native的范式,甚至可以说是真的AI APP的编程范式。

何为Agent

picture.image

Agent并不是一个新概念,早在前人工智能时代就有软件智能体的概念,是人类认知上最符合人类认知的智能形态,比如游戏中的NPC,利用构造行为树,使得NPC能够根据环境自动的完成观察,攻击,回血等动作。就像之前提到的那样,到了AI时代,有了LLM的加持,Agent被当作是走向AGI梦想的一个关键形态。

picture.image

Agent的智能程度和其采用的技术直接相关,从早期规则,再到状态机,后来的行为树,最后到了大模型,特别是ChatGPT的发布,关于Agent的实现研究有了大规模明显的增长。而其本质在于大模型技术特有的智能涌现,让Agent实现人类预期的智能成为一种可能。

作为基于OpenAI大模型接口,最早实现Agent的框架Langchain,它在其官方文档这么介绍Agent:

Agent

The core idea of agents is to use an LLM to choose a sequence of actions to take. In chains, a sequence of actions is hardcoded (in code). In agents, a language model is used as a reasoning engine to determine which actions to take and in which order.

https://python.langchain.com/docs/modules/agents/

picture.image

可以看出,它的解释是与其Chain做比较,就如前文所讲,一个任务的规划本质上是一个处理流编排的过程,而是不是Agent关键在于这个流程是人定义的,还是大模型来指定的。另一个关键问题是,这样一个流程是根据实际执行的过程结合外界反馈动态执行的,还是一次性的生成静态执行,这体现了Agent的智能程度和处理外界复杂情况的鲁棒性。这里需要区分另一个概念:超自动化(RPA),而他们之间的核心便是AI和人之间主导权的问题,以及整个流程是否能够随着数据反馈自我进化。

超级自动化是使用人工智能 (AI)、机器学习 (ML)、自然语言处理 (NLP)、流程挖掘和机器人流程自动化 (RPA) 等先进自动化技术生态系统的过程。它旨在通过自动化业务流程来增强人类知识,使企业从高效的决策和生产中受益。

Agent并不是算法,而是一种基于LLM的工程实现,其核心依托于LLM的能力,通过外在的Prompt来激发大模型的规划推理等智能能力,从而达到一种前所未有的效果,这也是我们将它放在Prompt这一张介绍的原因。但需要明确的LLM并不是全部,Agent是一种工程架构实现系统,包含着其他的构成部分,不能简单割裂看待。

Agent的架构

picture.image

在复旦大学《The Rise and Potential of Large Language Model Based Agents: A Survey》论文中,一个完整的基于LLM的Agents系统,包含了三个模块,大脑(Brain),感知(Perception)和行动(Action)。大脑模块承担着记忆、思考等基本任务,以及决策。感知模块感知和处理来自外部环境的多模式信息,行动模块使用工具和影响周围环境。

一般来说,Agent的工作流程为:首先,感知模块,类似人类的感觉系统,如眼睛和耳朵,感知外部环境,然后将多模式信息转换为可被Agent理解的表达。随后,作为控制中心的大脑模块参与信息处理活动,如思考、决策和记忆和知识的存储操作。最后,动作模块与人体肢体相对应,执行在工具的帮助下执行,并对周围环境产生影响。通过重复上述过程中,Agent可以不断地获得反馈并与环境交互。

1)大脑负责记忆(回话信息及知识)存储,以及决策、推理、规划。

在 这个 框架中,“ 记 忆 ”存储了Agent的相关知识及 过去的观察、想法和行动的序列,可参考人类分为短时记忆和长期记忆,短时记忆以上下文会话为主,一般作为上下文内容提交给大模型,而长时记忆以知识内容为主,包含于语言知识、常识和专业领域知识,通常存储在外部存储中,如向量数据库中,按需检索。而在 记忆存储时面临两个挑战,一是记忆容量有限,短时记忆(会话 历史) 的长度 突破了模型的上下文窗口大小导致记忆的截断 , 二是长期记忆 检索困难, 由于大量的历史信息 导致提取相关主题的 记忆信息变得困难,导致在处理当下任务时无法有效回应。

picture.image

对于第一个挑战,即提高记忆容量上,可以从提高大模型的上下文长度,总结提炼记忆内容,利用向量等数据结构压缩记忆。对于记忆检索,当Agent与其环境或用户交互时从内存中检索最合适的内容。这样可以确保Agent访问相关且准确的信息以执行特定操作,这里的记忆检索过程是一个自动的行为,而这一行为通常考虑三个指标:近期性,相关性和重要性,而其最终召回的结果由这些指标加权排序获得。另外,也有类似于交互式记忆的方式是,即用户通过操纵记忆信息,从而对齐记忆和用户期待。

推理和规划是Agent大脑的另一个重要能力,它们都是大模型智能涌现的结果,而这些能力的激发方式便是前面提到的prompt技术,特别是以cot为代表的高阶Prompt技术。

除此之外,Agent大脑还应包含“举一反三”的迁移泛化能力。这其中涉及到了微调阶段训练集未见到的任务的处理能力,少量示例的上下文学习以及不断更新迭代技能的持续学习能力。

2)感知负责多种模态的信息输入.

picture.image

如图可见,一个Agent系统的感知能力包含了文本、图像、声音等输入读取等。

3)行动包含Agent的文本输出、工具使用以及具身智能。

picture.image

受限于大模型的先天缺陷,且不可能是万能的,将工作作为大模型能力的扩展非常有意义,这和人类使用工具简化问题加速问题解决效率。而大模型对工具的利用包含了学习工具,使用工具和制作工具三个方面,有关工具使用的。比如下面的一段Prompt描述,便是告诉大模型有什么样的工具以及这些工具干什么用,从而让大模型在解决问题时按需选择:

picture.image


          
Answer the following questions as best you can. You have access to the following tools:
          

          
Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.
          
Calculator: Useful for when you need to answer questions about math.
          

          
Use the following format:
          
...
      

在应用场景层面,主要有三类:单个agent、多个agent的互动和人与agent的互动。单个agent拥有多种能力,可以在各种应用方向上展示出优秀的任务解决能力。当多个agent进行互动时,它们可以通过合作或对抗性互动实现进步。此外,在人与agent交互中,人的反馈可以Agent可以更高效、更安全地执行任务,同时代理还可以为人类提供更好的服务。

在工业界,Agent系统的实现可以更简单的表达为:

picture.image

@参考lilianweng

其代表性的架构为:

picture.image

就目前agent的实现来讲,有两类Agent比较常见,一种是Re-Act模式,一种是Plan-Execute模式。这两个模式的理论基础都来自于前面Prompt技术。

picture.image

Re -Act Agent的基本工作模式就是基于问题一步步的分析推理,每一步思考(Reasion)产生一个动作执行(Action),获得外部反馈后,进一步思考,得到下一步反馈,直到解决 问题。下面以langchain中re-act agent回答“ Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power? ”这一问题 为例推演一下 这类agent的执行过程 。

代码如下:


          
from langchain.agents import load_tools
          
from langchain.agents import initialize_agent
          
from langchain.agents import AgentType
          
from langchain.llms import OpenAI
          
import os
          
os.environ['OPENAI_API_KEY'] = ''
          
os.environ['SERPAPI_API_KEY'] = ''
          
os.environ["WOLFRAM_ALPHA_APPID"] = ""
          

          
llm = OpenAI(temperature=0)
          

          
tools = load_tools(["serpapi", "wolfram-alpha","llm-math"], llm=llm)
          

          
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
          

          
agent.run("Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?")
          

      

具体执行过程:

1)agent第一次提交LLM:


          
Answer the following questions as best you can. You have access to the following tools:
          

          
Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.
          
Calculator: Useful for when you need to answer questions about math.
          

          
Use the following format:
          

          
Question: the input question you must answer
          
Thought: you should always think about what to do
          
Action: the action to take, should be one of [Search, Calculator]
          
Action Input: the input to the action
          
Observation: the result of the action
          
... (this Thought/Action/Action Input/Observation can repeat N times)
          
Thought: I now know the final answer
          
Final Answer: the final answer to the original input question
          

          
Begin!
          

          
Question: Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?
          
Thought:
      

2)LLM返回:


          
I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.
          
Action: Search
          
Action Input: "Leo DiCaprio girlfriend
      

3)根据LLM选择的工具,Agent使用Search工具检索:

picture.image

4)提取搜索结果提交给llm。


          
Action: the action to take, should be one of [Search, Calculator]
          
Action Input: the input to the action
          
Observation: the result of the action
          
... (this Thought/Action/Action Input/Observation can repeat N times)
          
Thought: I now know the final answer
          
Final Answer: the final answer to the original input question
          

          
Begin!
          

          
Question: Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?
          
Thought: I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.
          
Action: Search
          
Action Input: "Leo DiCaprio girlfriend"
          
Observation: Helena Christensen: 1997 ... Helena and Leo were linked together in the '90s, but again...no actual evidence that they dated due to the internet ...
          
Thought:
      

5)LLM得到搜索结果后,再次返回新指令:


          
I need to find out Helena Christensen's age.
          
Action: Search
          
Action Input: "Helena Christensen age"
      

6)agent再次执行search工具,获得检索结果:

picture.image

7)获得搜索结果,再次提交LLM:


          
Answer the following questions as best you can. You have access to the following tools:
          

          
Search: A search engine. Useful for when you need to answer questions about current events. Input should be a search query.
          
Calculator: Useful for when you need to answer questions about math.
          

          
Use the following format:
          

          
Question: the input question you must answer
          
Thought: you should always think about what to do
          
Action: the action to take, should be one of [Search, Calculator]
          
Action Input: the input to the action
          
Observation: the result of the action
          
... (this Thought/Action/Action Input/Observation can repeat N times)
          
Thought: I now know the final answer
          
Final Answer: the final answer to the original input question
          

          
Begin!
          

          
Question: Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?
          
Thought: I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.
          
Action: Search
          
Action Input: "Leo DiCaprio girlfriend"
          
Observation: Helena Christensen: 1997 ... Helena and Leo were linked together in the '90s, but again...no actual evidence that they dated due to the internet ...
          
Thought: I need to find out Helena Christensen's age.
          
Action: Search
          
Action Input: "Helena Christensen age"
          
Observation: 54 years
          
Thought:
      

8)LLM返回:

I need to calculate 54 raised to the 0.43 power.  
Action: Calculator  
Action Input: 54^0.43

9)Agent根据大模型要求,调用Calculator,此时llm-math模块利用大模型制造工具的能力,生成一个python脚本充当计算器。


          
Translate a math problem into a expression that can be executed using Python's numexpr library. Use the output of running this code to answer the question.
          

          
Question: ${Question with math problem.}
          
```text
          
${single line mathematical expression that solves the problem}
          

...numexpr.evaluate(text)...

          
${Output of running the code}
          

Answer: ${Answer}

Begin.

Question: What is 37593 * 67?

          
37593 * 67
          

...numexpr.evaluate("37593 * 67")...

          
2518731
          

Answer: 2518731

Question: 54^0.43




10)获得LLM返回后llm-math执行python表达式,获得正确的结果:5.558137888477938







'

          
54**0.43
          

...numexpr.evaluate("54**0.43")...

'

def _process_llm_result(

    self, llm_output: str, run_manager: CallbackManagerForChainRun
      
) -> Dict[str, str]:
      
    run_manager.on_text(llm_output, color="green", verbose=self.verbose)
      
    llm_output = llm_output.strip()
      
    text_match = re.search(r"^```text(.*?)```", llm_output, re.DOTALL)
      
    if text_match:
      
        expression = text_match.group(1)
      
        output = self._evaluate_expression(expression)
      
        run_manager.on_text("\nAnswer: ", verbose=self.verbose)
      
        run_manager.on_text(output, color="yellow", verbose=self.verbose)
      
        answer = "Answer: " + output
      
    elif llm_output.startswith("Answer:"):
      
        answer = llm_output
      
    elif "Answer:" in llm_output:
      
        answer = "Answer: " + llm_output.split("Answer:")[-1]
      
    else:
      
        raise ValueError(f"unknown format from LLM: {llm_output}")
      
    return {self.output_key: answer}
  



11)agent汇总以上信息后,再次提交LLM:







....

Question: Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?

Thought: I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.

Action: Search

Action Input: "Leo DiCaprio girlfriend"

Observation: Helena Christensen: 1997 ... Helena and Leo were linked together in the '90s, but again...no actual evidence that they dated due to the internet ...

Thought: I need to find out Helena Christensen's age.

Action: Search

Action Input: "Helena Christensen age"

Observation: 54 years

Thought: I need to calculate 54 raised to the 0.43 power.

Action: Calculator

Action Input: 54^0.43

Observation: Answer: 5.558137888477938

Thought:




至此,经过11步的交互,Agent最终获得答案:








Thought: I now know the final answer.

Final Answer: Helena Christensen, Leo DiCaprio's girlfriend, is 54 years old and her age raised to the 0.43 power is 5.558137888477938.




根据上面的推演,我们看到了Agent是如何通过Observation->Thought->Action一步步接近答案并完成任务的,期间制造了工具,也利用了工具,感叹大模型的强大之余,也能感觉到Re-Act模型的一个特点,就是“走到哪算哪”,一旦一步错误,那么可能会越走越偏。另外,由于每一步都需要携带历史会话和所有工具的描述信息,这对于上下文窗口也是一个大的挑战。因此,Re-act只适合一些任务比较简单的情况。



而当前被众多Agent框架采用的是改进版本的“Plan-And-Solve”,它的思维过程更像人类,首先不着急生成下一步动作,而是对问题进行一个拆解,生成待执行任务列表
,分步执行,并且可以根据实际的情况,不断完善更新整个执行计划,像是一个开车又快又稳的老司机。以babyAGI为例,它的执行过程如图:




****![picture.image](https://p3-volc-community-sign.byteimg.com/tos-cn-i-tlddhu82om/44ae35f6f8ea4445bff1dce4aa5d6b6c~tplv-tlddhu82om-image.image?=&rk3s=8031ce6d&x-expires=1727598845&x-signature=QfDjJCsBa73v2WyykaolzU6HQkQ%3D)****




核心流程通过运行一个无限循环来驱动:  
  
1)从任务列表中提取第一个任务。  
2)将任务发送给执行代理,执行代理根据上下文使用 OpenAI 的 API 完成任务。  
3)丰富结果并将其存储在 Chroma/Weaviate 中。  
4)根据目标和前一个任务的结果创建新任务,并调整任务列表的优先级。



下面是babyAGI在解决这一问题时的处理过程:






OBJECTIVE = “You work for a non profit organization titled Aliens are Real that is trying to find UFOs and aliens among us and give them a warm welcome to Earth. Your goal is to prove Aliens are real through research, first hand accounts, and evidence of extraterrestrial and/or intradimensional life. Your first task in providing evidence that aliens exist is to make a todo list to map out your steps in order to prove aliens are real.”

baby_agi({“objective”: OBJECTIVE})

TASK LIST

1: Make a todo list

NEXT TASK

1: Make a todo list

Entering new AgentExecutor chain…

Thought: What steps do I need to take to prove aliens exist?

Action: TODO

Action Input: Prove aliens exist

Observation:

Research existing evidence of alien life, such as UFO sightings, crop circles, and other unexplained phenomena.

Analyze the evidence to determine if it is credible and could be used to prove the existence of aliens.

Develop a hypothesis based on the evidence and research.

Design experiments to test the hypothesis.

Conduct experiments and analyze the results.

Publish the results in a scientific journal.

Present the findings to the scientific community.

Respond to any criticism or questions from the scientific community.

Make any necessary revisions to the findings.

Submit the revised findings to the scientific community for review.

Thought: I now know the final answer

Final Answer: A todo list has been created to prove aliens exist. The list includes researching existing evidence, analyzing the evidence, developing a hypothesis, designing experiments, conducting experiments, publishing the results, presenting the findings, responding to criticism, making revisions, and submitting the revised findings.

Finished chain.

TASK RESULT

A todo list has been created to prove aliens exist. The list includes researching existing evidence, analyzing the evidence, developing a hypothesis, designing experiments, conducting experiments, publishing the results, presenting the findings, responding to criticism, making revisions, and submitting the revised findings.

TASK LIST

2: Research existing evidence of extraterrestrial and/or intradimensional life.

3: Analyze the evidence to determine its validity.

4: Develop a hypothesis based on the evidence.

5: Design experiments to test the hypothesis.

6: Conduct experiments to test the hypothesis.

7: Publish the results of the experiments.

8: Present the findings to the public.

9: Respond to criticism of the findings.

10: Make revisions to the findings based on the criticism.

11: Submit the revised findings to a scientific journal.

NEXT TASK

2: Research existing evidence of extraterrestrial and/or intradimensional life.

Entering new AgentExecutor chain…

Thought: What resources can I use to research?

Action: Search

Action Input: Resources to research extraterrestrial and/or intradimensional life

Observation: SETI Institute’s researchers are in the forefront of this effort. Be part of the search. Our work Meet our scientists. A Nonprofit Research Organization.

Thought: What other resources can I use?

Action: Search

Action Input: Other resources to research extraterrestrial and/or intradimensional life

Observation: List of search terms used in response to requests for information on UFOs and … Alien Abduction(s); Alien Autopsy; Alien Life Form; Animal Mutilations …

Thought: What other steps can I take to prove aliens are real?

Action: TODO

Action Input: Steps to prove aliens are real

Observation:

Research existing evidence of alien life, such as UFO sightings, crop circles, and other unexplained phenomena.

Analyze the evidence to determine if it is credible and reliable.

Consult with experts in the field of astronomy and extraterrestrial life to gain insight into the possibility of alien life.

Investigate potential sites of alien activity, such as Area 51 and other restricted areas.

Utilize advanced technology to search for signs of alien life, such as radio signals or other forms of communication.

Develop a hypothesis based on the evidence collected and test it.

Publish the results of the research and analysis in a scientific journal.

Present the findings to the public and other experts in the field.

Follow up with additional research and analysis to further support the findings.

Thought: I now know the final answer

Final Answer: The final answer is to create a todo list that includes researching existing evidence of extraterrestrial and/or intradimensional life, analyzing the evidence to determine if it is credible and reliable, consulting with experts in the field of astronomy and extraterrestrial life, investigating potential sites of alien activity, utilizing advanced technology to search for signs of alien life, developing a hypothesis based on the evidence collected and testing it, publishing the results of the research and analysis in a scientific journal, presenting the findings to the public and other experts in the field, and following up with additional research and analysis to further support the findings.

Finished chain.

TASK RESULT

The final answer is to create a todo list that includes researching existing evidence of extraterrestrial and/or intradimensional life, analyzing the evidence to determine if it is credible and reliable, consulting with experts in the field of astronomy and extraterrestrial life, investigating potential sites of alien activity, utilizing advanced technology to search for signs of alien life, developing a hypothesis based on the evidence collected and testing it, publishing the results of the research and analysis in a scientific journal, presenting the findings to the public and other experts in the field, and following up with additional research and analysis to further support the findings.

TASK ENDING

{‘objective’: ‘You work for a non profit organization titled Aliens are Real that is trying to find UFOs and aliens among us and give them a warm welcome to Earth. Your goal is to prove Aliens are real through research, first hand accounts, and evidence of extraterrestrial and/or intradimensional life. Your first task in providing evidence that aliens exist is to make a todo list to map out your steps in order to prove aliens are real.’}




当然Agent的实现方式不止这两种,下面以一个minecraft游戏agent:







You are a helpful assistant that writes Mineflayer javascript code to complete any Minecraft task specified by me.

Here are some useful programs written with Mineflayer APIs.

{programs}

At each round of conversation, I will give you

Code from the last round: ...

Execution error: ...

Chat log: ...

Biome: ...

Time: ...

Nearby blocks: ...

Nearby entities (nearest to farthest):

Health: ...

Hunger: ...

Position: ...

Equipment: ...

Inventory (xx/36): ...

Chests: ...

Task: ...

Context: ...

Critique: ...

You should then respond to me with

Explain (if applicable): Are there any steps missing in your plan? Why does the code not complete the task? What does the chat log and execution error imply?

Plan: How to complete the task step by step. You should pay attention to Inventory since it tells what you have. The task completeness check is also based on your final inventory.

Code:

1) Write an async function taking the bot as the only argument.
      
2) Reuse the above useful programs as much as possible.
      
    - Use `mineBlock(bot, name, count)` to collect blocks. Do not use `bot.dig` directly.
      
    - Use `craftItem(bot, name, count)` to craft items. Do not use `bot.craft` or `bot.recipesFor` directly.
      
    - Use `smeltItem(bot, name count)` to smelt items. Do not use `bot.openFurnace` directly.
      
    - Use `placeItem(bot, name, position)` to place blocks. Do not use `bot.placeBlock` directly.
      
    - Use `killMob(bot, name, timeout)` to kill mobs. Do not use `bot.attack` directly.
      
3) Your function will be reused for building more complex functions. Therefore, you should make it generic and reusable. You should not make strong assumption about the inventory (as it may be changed at a later time), and therefore you should always check whether you have the required items before using them. If not, you should first collect the required items and reuse the above useful programs.
      
4) Functions in the "Code from the last round" section will not be saved or executed. Do not reuse functions listed there.
      
5) Anything defined outside a function will be ignored, define all your variables inside your functions.
      
6) Call `bot.chat` to show the intermediate progress.
      
7) Use `exploreUntil(bot, direction, maxDistance, callback)` when you cannot find something. You should frequently call this before mining blocks or killing mobs. You should select a direction at random every time instead of constantly using (1, 0, 1).
      
8) `maxDistance` should always be 32 for `bot.findBlocks` and `bot.findBlock`. Do not cheat.
      
9) Do not write infinite loops or recursive functions.
      
10) Do not use `bot.on` or `bot.once` to register event listeners. You definitely do not need them.
      
11) Name your function in a meaningful way (can infer the task from the name).
      

      

You should only respond in the format as described below:

RESPONSE FORMAT:

{response_format}




这是一个电脑销售AI Agent的Prompt:







delimiter = "####"

system_message = f"""

Follow these steps to answer the customer queries.

The customer query will be delimited with four hashtags,\

i.e. {delimiter}.

Step 1:{delimiter} First decide whether the user is \

asking a question about a specific product or products. \

Product cateogry doesn't count.

Step 2:{delimiter} If the user is asking about \

specific products, identify whether \

the products are in the following list.

All available products:

  1. Product: TechPro Ultrabook

    Category: Computers and Laptops

    Brand: TechPro

    Model Number: TP-UB100

    Warranty: 1 year

    Rating: 4.5

    Features: 13.3-inch display, 8GB RAM, 256GB SSD, Intel Core i5 processor

    Description: A sleek and lightweight ultrabook for everyday use.

    Price: $799.99

  2. Product: BlueWave Gaming Laptop

    Category: Computers and Laptops

    Brand: BlueWave

    Model Number: BW-GL200

    Warranty: 2 years

    Rating: 4.7

    Features: 15.6-inch display, 16GB RAM, 512GB SSD, NVIDIA GeForce RTX 3060

    Description: A high-performance gaming laptop for an immersive experience.

    Price: $1199.99

  3. Product: PowerLite Convertible

    Category: Computers and Laptops

    Brand: PowerLite

    Model Number: PL-CV300

    Warranty: 1 year

    Rating: 4.3

    Features: 14-inch touchscreen, 8GB RAM, 256GB SSD, 360-degree hinge

    Description: A versatile convertible laptop with a responsive touchscreen.

    Price: $699.99

  4. Product: TechPro Desktop

    Category: Computers and Laptops

    Brand: TechPro

    Model Number: TP-DT500

    Warranty: 1 year

    Rating: 4.4

    Features: Intel Core i7 processor, 16GB RAM, 1TB HDD, NVIDIA GeForce GTX 1660

    Description: A powerful desktop computer for work and play.

    Price: $999.99

  5. Product: BlueWave Chromebook

    Category: Computers and Laptops

    Brand: BlueWave

    Model Number: BW-CB100

    Warranty: 1 year

    Rating: 4.1

    Features: 11.6-inch display, 4GB RAM, 32GB eMMC, Chrome OS

    Description: A compact and affordable Chromebook for everyday tasks.

    Price: $249.99

Step 3:{delimiter} If the message contains products \

in the list above, list any assumptions that the \

user is making in their \

message e.g. that Laptop X is bigger than \

Laptop Y, or that Laptop Z has a 2 year warranty.

Step 4:{delimiter}: If the user made any assumptions, \

figure out whether the assumption is true based on your \

product information.

Step 5:{delimiter}: First, politely correct the \

customer's incorrect assumptions if applicable. \

Only mention or reference products in the list of \

5 available products, as these are the only 5 \

products that the store sells. \

Answer the customer in a friendly tone.

Use the following format:

Step 1:{delimiter} <step 1 reasoning>

Step 2:{delimiter} <step 2 reasoning>

Step 3:{delimiter} <step 3 reasoning>

Step 4:{delimiter} <step 4 reasoning>

Response to user:{delimiter}

Make sure to include {delimiter} to separate every step.

"""




执行过程:







user_message = f"""

by how much is the BlueWave Chromebook more expensive \

than the TechPro Desktop"""

messages = [

{'role':'system',

'content': system_message},

{'role':'user',

'content': f"{delimiter}{user_message}{delimiter}"},

]

response = get_completion_from_messages(messages)

print(response)

######################

Step 1:#### The user is asking a question about two specific products, the BlueWave Chromebook and the TechPro Desktop.

Step 2:#### The prices of the two products are as follows:

  • BlueWave Chromebook: $249.99

  • TechPro Desktop: $999.99

Step 3:#### The user is assuming that the BlueWave Chromebook is more expensive than the TechPro Desktop.

Step 4:#### The assumption is incorrect. The TechPro Desktop is actually more expensive than the BlueWave Chromebook.

Response to user:#### The BlueWave Chromebook is actually less expensive than the TechPro Desktop. The BlueWave Chromebook costs 249.99whiletheTechProDesktopcosts249.99 while the TechPro Desktop costs 999.99.




可以看出,Agent的关键能力在于大模型,大模型是否能充分发挥作用其关键又在Prompt。之所以将,A
I
 Agent是大模型原生的场景模式,其关键就是这一切都是
围绕模型来层层包装而来
,所有的实现都以配合LLM而来,这与RAG以及原来的AI应用开发的模式是完全不同的,需要大家适应转变
。
  








未完待续。。。


在接下来内容中,将探讨Agent模式的局限性及相关框架。








  





**合集目录:**
  





[第一部分 基础概念](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139461&idx=1&sn=de0ebc29a0b5cef9212f30437b6227c0&chksm=8739602bb04ee93dc61e3c8e750ccf2a4d1f1f36447e2012af851103b0168583a55bd4e5a1b0&scene=21#wechat_redirect)




[一文探秘LLM应用开发(1)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139461&idx=1&sn=de0ebc29a0b5cef9212f30437b6227c0&chksm=8739602bb04ee93dc61e3c8e750ccf2a4d1f1f36447e2012af851103b0168583a55bd4e5a1b0&scene=21#wechat_redirect)
  





[第二部分 应用挑战](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139572&idx=1&sn=a636cafdb95a3d334020ab5237fca2f0&chksm=8739605ab04ee94c87ece7c6457aea2d4c2ede0eccb5891ba7ef7c0796f0649697dec1d25c47&scene=21#wechat_redirect)




[1.问题定义与基本思路](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139461&idx=1&sn=de0ebc29a0b5cef9212f30437b6227c0&chksm=8739602bb04ee93dc61e3c8e750ccf2a4d1f1f36447e2012af851103b0168583a55bd4e5a1b0&scene=21#wechat_redirect)
  





[一文探秘LLM应用开发(2)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139572&idx=1&sn=a636cafdb95a3d334020ab5237fca2f0&chksm=8739605ab04ee94c87ece7c6457aea2d4c2ede0eccb5891ba7ef7c0796f0649697dec1d25c47&scene=21#wechat_redirect)
  





2
[.](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139643&idx=1&sn=cd16d5eea8a93113893320642ad0a204&chksm=87396095b04ee983fddae57c546d6f80830d399d6850d6b1ad5828c15d1bb76bbb11770c4207&scene=21#wechat_redirect)
[基本流程与相关技术](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139572&idx=1&sn=a636cafdb95a3d334020ab5237fca2f0&chksm=8739605ab04ee94c87ece7c6457aea2d4c2ede0eccb5891ba7ef7c0796f0649697dec1d25c47&scene=21#wechat_redirect)




[1)Tokenization与Embbeding](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139643&idx=1&sn=cd16d5eea8a93113893320642ad0a204&chksm=87396095b04ee983fddae57c546d6f80830d399d6850d6b1ad5828c15d1bb76bbb11770c4207&scene=21#wechat_redirect)
  





[一文探密LLM应用开发(3)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139643&idx=1&sn=cd16d5eea8a93113893320642ad0a204&chksm=87396095b04ee983fddae57c546d6f80830d399d6850d6b1ad5828c15d1bb76bbb11770c4207&scene=21#wechat_redirect)
  





[2)向量数据库](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139684&idx=1&sn=152bbf6a8b1f365da36c8d0640262ff4&chksm=873960cab04ee9dcaf4d4c6857a5bb0fa118548dfb4a48bae27e005c16381a9b065f65e78bcd&scene=21#wechat_redirect)




[一文探秘LLM应用开发(4)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139684&idx=1&sn=152bbf6a8b1f365da36c8d0640262ff4&chksm=873960cab04ee9dcaf4d4c6857a5bb0fa118548dfb4a48bae27e005c16381a9b065f65e78bcd&scene=21#wechat_redirect)




3)微调




[一文探秘LLM应用开发(5)-微调(背景与挑战)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139890&idx=1&sn=4f9177174ea447c978232dfe1fe09961&chksm=8739619cb04ee88a9963fd1ce6f9e9600e5bdfe804a7aa82fb6f2f1810bf970002a695d541e3&scene=21#wechat_redirect)
  





[一文探秘LLM应用开发(6)-微调(方案理论)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139904&idx=1&sn=172e92206322ce3c8077e5cdff70502a&chksm=873961eeb04ee8f85bc49cafa1c462f5e0b3eef4ba4a02425118eea7373251a98665e9931996&scene=21#wechat_redirect)




[一文探秘LLM应用开发(7)-微调(工具实践)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461139988&idx=1&sn=e2fef44af96bc7ad4d1cf17daa198c8e&chksm=8739663ab04eef2cf348f83c481026619debea6cdda78e6114cbf4443846e2fb47342d10ae94&scene=21#wechat_redirect)
  





4)模型部署与推理


[一文探秘LLM应用开发(8)-模型部署与推理(DEMO验证)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461140349&idx=1&sn=cd9ac2154c1e530087b346eeee19c517&chksm=87396753b04eee454c4553ac5b4040fdd575ff9c75b1006fe8fcb1a5ae69130ef29b09ab68af&scene=21#wechat_redirect)


[一文探秘LLM应用开发(9)-模型部署与推理(模型适配优化之GPU面面观-1)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461140820&idx=1&sn=ebda0dd8430f1b41efb2592aea0a13be&chksm=8739657ab04eec6c272cbe09c67501dbfd9b9181dd6f80bc0705ff38889fa15ea2c326d3cdf3&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(10)-模型部署与推理(模型适配优化之GPU面面观-2)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461140919&idx=1&sn=6f744ca8453382121cb9ee1757b11339&chksm=87396599b04eec8f92a11d96f91e53988416759ca8fde0fab8724d4ab65f576c06a785fbbf3b&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(11)-模型部署与推理(模型大小与推理性能的关系)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461141182&idx=1&sn=ee8193e3c1d74ae16f7cb9595143ffa3&chksm=87396a90b04ee386aa24f574b9a5c06db590fcba3ff9589ac0e5a42038e0c64a7fcb000b43ee&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(12)-模型部署与推理(大模型相关参数计算及性能分析)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461141182&idx=1&sn=ee8193e3c1d74ae16f7cb9595143ffa3&chksm=87396a90b04ee386aa24f574b9a5c06db590fcba3ff9589ac0e5a42038e0c64a7fcb000b43ee&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(13)-模型部署与推理(优化理论)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461141797&idx=1&sn=b1eec94052b01dfe6a6ece044c5a6dfa&chksm=8739690bb04ee01de5c8920dc01d03a7e941b1fa11da8d1eda52866134c6c9f2efc009c47b48&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(14)-模型部署与推理(技术架构)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461141945&idx=1&sn=e461aa8a4a0519eca5927e99b12df9bd&chksm=87396997b04ee081bc1b3733775ecee4d5b6e9a98f8b0e9fdf908fb0394fd88e5e2761d5960c&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(15)-模型部署与推理(框架工具-推理执行引擎(HF pipeline))](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461141961&idx=1&sn=a9950fcc891e4585fd9e7cbf498ab25b&chksm=873969e7b04ee0f11b712f5b14767ac7e63c03ed3143e27c01620831b859c4eedff55f6bc5a3&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(16)-模型部署与推理(框架工具-TGI,vLLM,TensorRT-LLM,DS-MII)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142012&idx=1&sn=dafb0b676cdf6d41fd9bd54f9b6a82d3&chksm=873969d2b04ee0c49b491e69b4a161b443a1f608ea2e5caafac6dbd8f3108d0c87687dd1ea69&scene=21#wechat_redirect)
  



[一文探秘LLM应用开发(17)-模型部署与推理(框架工具-ggml、mlc-llm、ollama)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142055&idx=1&sn=99a0a8c065601a990397b3964e7faa8c&chksm=87396e09b04ee71f5967a8b90463bc65b509103aa54b1aca5a9c32c4d5627ba7cd0739f73999&scene=21#wechat_redirect)


[一文探秘LLM应用开发(18)-模型部署与推理(框架工具-Triton Server、RayLLM、OpenLLM)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142079&idx=1&sn=07d9033203c0064408fe0af33d1f9414&chksm=87396e11b04ee707baa4dedfaf5f8a63113ee9e4f5091fe9d5b3f6afd7fb1e9816b533de3647&scene=21#wechat_redirect)


[一文探秘LLM应用开发(19)-模型部署与推理(FastChat、OpenChat、HuggingChat、GPT4ALL)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142106&idx=1&sn=30af26ffbd21c710029501226252006e&chksm=87396e74b04ee7624eb48c4dba22dd529d833390400262e27d4e078eec41e46d1bc41dfc0aae&scene=21#wechat_redirect)
  



5)Prompt


[一文探秘LLM应用开发(20)-Prompt(相关概念)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142187&idx=1&sn=2d316fd46d0a745c2beff86c1d13f2b7&chksm=87396e85b04ee793efd4b9811e42b047ec337aff697ee1530443800f7cb4ffa13d85bd19747d&scene=21#wechat_redirect)


[一文探秘LLM应用开发(21)-Prompt(提示工程技术、重要性与挑战)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142292&idx=1&sn=bead068edcb74bcfe2c22ff6d87c5cb0&chksm=87396f3ab04ee62c5777f09b94b528e35c754335d1ece5e60ee0c9b7a29b6663393aaf972652&scene=21#wechat_redirect)


[一文探秘LLM应用开发(21)-Prompt(编写技巧)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142422&idx=1&sn=69ee582b38f7966e5598bed8642ad239&chksm=87396fb8b04ee6ae9592ef11fcf137de2b5beeebf7bf765c12201f67cc378404df3bd211960e&scene=21#wechat_redirect)


[一文探秘LLM应用开发(23)-Prompt(相关工具)](http://mp.weixin.qq.com/s?__biz=MzA5MTIxNTY4MQ==&mid=2461142506&idx=1&sn=1016e4b2db066508d2f60c428d681315&chksm=87396fc4b04ee6d21f9cbdc2a6dd261be5713d9053eff269f2ec38ae2a26d91b29bbf3b8a4d1&scene=21#wechat_redirect)


​​


​​









0
0
0
0
关于作者
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论