@@ -17,25 +17,8 @@ Agent的三个核心板块:模型、工具、流程。
1717
1818单一智能体各家都可以实现,因此技术选型主要集中在如何快速构建多智能体流程,尤其在于:记忆管理(分组、长短记忆、修剪与同步)、human-in-the-loop、智能体切换逻辑。
1919
20- | 开发方式 | 代表产品/框架 | 优点 | 缺点 |
21- | ----------------- | ------------------------ | ---------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
22- | 低代码/无代码开发 | Coze | • 简单易用 | • 功能有限• 收费昂贵• 云端执行非自主(必须联网)• 不可控• 不同平台差异大,不易迁移• 插件大多需要独立的Token• 平台可用的大模型有限 |
23- | 框架开发 | LangChain、AutoGen、MG等 | • 功能强大• 可以满足复杂需求• 代码可以灵活复用 | • 需要一定的技术门槛 |
24-
2520## Agent 设计理论
2621
27- ### Agent 开发与变化趋势
28-
29- | 时代 | 主要特点 | 示例 | 局限性 |
30- | ------------| --------------------------------------------------------------------------| ----------------------------------------------------------------------| ------------------------------------------------------------------------|
31- | 规则时代 | 基于预定义规则和关键词匹配,无法理解语言深层含义。 | 用户说“小爱同学,关灯”,必须精确匹配关键词才能执行。 | 无法处理复杂语义;智能化提升依赖于编写更复杂的规则。 |
32- | Agent 1.0 | 利用语义理解处理自然语言变体和歧义,但仅限于单个任务。 | 用户说“帮我给张总打电话”,即使通讯录中是“张三总”,也能正确识别并拨打。 | 不能串联多个任务或处理复杂工作流程。 |
33- | Agent 2.0 | 能将多个任务串联成工作流程,自动规划和执行,但依赖于提供的函数或API。 | 用户说“查询明天天气并给某联系人发邮件”,Agent能自动完成整个过程。 | 无法处理未提供工具的任务;类似智能驾驶只能在特定路段自动驾驶。 |
34- | Agent 2.5 | 具备多模态理解,如视觉,能使用通用工具执行任务,不再局限于特定API。 | 用户说“用PS调整这张照片的对比度”,或“识别森林摄像头中的多种珍稀动物”。 | 相较于职业熟练度顶级的人类,执行速度较慢;特定任务效率低于传统方案。 |
35- | Agent 3.0 | 能够调用子代理为自己编写代码工具。结合视觉理解甚至可以帮你申请API_KEY | 用户说“查下我的快递” | 编写对应的查询API,并调用执行。 |
36-
37- ### 大模型调用工具方式
38-
3922大模型本身不具备执行能力,需要调用工具。
4023
4124调用方式都是换汤不换药,就是将可调用的工具(函数)作为提示词的一部分,传入给大模型。大模型做选择填空,并返回特定格式。
@@ -45,151 +28,8 @@ Agent的三个核心板块:模型、工具、流程。
4528
4629<Highlight >Agent中的大模型,第一核心能力是让“大模型做选择填空,并返回特定格式”的指令遵循能力</Highlight >。
4730
48- #### 提示词工程
49-
50- ``` python showLineNumbers
51- import openai
52- import re
53- import datetime
54-
55- # 设置 OpenAI API Key
56- openai.api_key = " YOUR_API_KEY"
57-
58- # 实际业务函数:获取天气信息
59- def get_current_weather (location ):
60- # 模拟获取天气的逻辑
61- return f " { location} 的当前天气是晴朗。 "
62-
63- # 新增的工具:获取当前时间
64- def get_current_time ():
65- now = datetime.datetime.now().strftime(" %Y-%m-%d %H:%M:%S" )
66- return f " 当前时间是: { now} "
67-
68- def prompt_engineered_function_call (user_input ):
69- """
70- 使用提示词工程实现函数调用:
71- 1. 当用户输入中涉及"天气"关键词时,输出调用 get_current_weather 的指令,
72- 格式:CALL get_current_weather(location="<城市名称>")
73- 2. 当用户输入中涉及"时间"关键词时,输出调用 get_current_time 的指令,
74- 格式:CALL get_current_time()
75- 3. 如果用户的问题不涉及"天气"或"时间",则直接回答。
76- """
77- prompt = f """
78- 你是一个智能助手,以下是你的工作规则:
79- 1. 当用户提问包含"天气"关键词时,请按照格式输出函数调用指令,格式如下:
80- CALL get_current_weather(location="<城市名称>")
81- 2. 当用户提问包含"时间"关键词时,请按照格式输出函数调用指令,格式如下:
82- CALL get_current_time()
83- 3. 如果用户的问题不涉及"天气"或"时间",请直接给出答案,不要输出任何函数调用指令。
84-
85- 请根据以下用户输入返回结果:
86- 用户输入: { user_input}
87- """
88- response = openai.ChatCompletion.create(
89- model = " gpt-3.5-turbo" , # 或其他支持的模型
90- messages = [
91- {" role" : " system" , " content" : " 你是一个智能助手,根据规则输出相应格式。" },
92- {" role" : " user" , " content" : prompt}
93- ],
94- temperature = 0.2
95- )
96- return response.choices[0 ].message.content.strip()
97-
98- if __name__ == ' __main__' :
99- user_query = input (" 请输入查询内容: " )
100- model_reply = prompt_engineered_function_call(user_query)
101- print (" 模型回复:" , model_reply)
102-
103- # 判断模型回复是否为函数调用指令
104- if model_reply.startswith(" CALL get_current_weather" ):
105- # 解析 location 参数
106- match = re.search(r ' location="( . +? ) "' , model_reply)
107- if match:
108- location = match.group(1 )
109- result = get_current_weather(location)
110- print (" 调用函数结果:" , result)
111- else :
112- print (" 无法解析函数调用参数。" )
113- elif model_reply.startswith(" CALL get_current_time" ):
114- result = get_current_time()
115- print (" 调用函数结果:" , result)
116- else :
117- print (" 直接回答:" , model_reply)
118- ```
119-
120- #### function calling
121-
122- function calling 是 OpenAI 推出的一个功能,允许开发者将大模型的输出结果作为函数调用,并执行函数。一定程度上简化了代码。
123-
124- ``` python showLineNumbers
125- import openai
126- import json
127-
128- # 请设置你的 OpenAI API Key
129- openai.api_key = " YOUR_API_KEY"
130-
131- # 定义实际业务函数:获取天气信息
132- def get_current_weather (location ):
133- # 模拟获取天气信息的逻辑
134- return f " { location} 的当前天气是晴朗。 "
135-
136- # 定义大模型可调用的函数描述(Function Schema)
137- functions = [
138- {
139- " name" : " get_current_weather" ,
140- " description" : " 获取指定城市的天气" ,
141- " parameters" : {
142- " type" : " object" ,
143- " properties" : {
144- " location" : {" type" : " string" , " description" : " 城市名称" }
145- },
146- " required" : [" location" ]
147- }
148- }
149- ]
150-
151- def large_model_integration (user_input ):
152- """
153- 模拟大模型处理用户输入,
154- 若识别到需要调用天气查询函数,则使用函数调用功能。
155- """
156- # 调用大模型接口,启用函数调用
157- response = openai.ChatCompletion.create(
158- model = " gpt-4-0613" , # 模型支持函数调用
159- messages = [{" role" : " user" , " content" : user_input}],
160- functions = functions,
161- function_call = " auto" # 模型自动决定是否调用函数
162- )
163-
164- message = response[" choices" ][0 ][" message" ]
165-
166- # 判断是否触发了函数调用
167- if message.get(" function_call" ):
168- func_name = message[" function_call" ][" name" ]
169- arguments = message[" function_call" ][" arguments" ]
170-
171- # 解析函数参数
172- args = json.loads(arguments)
173-
174- # 根据函数名称调用对应的函数
175- if func_name == " get_current_weather" :
176- result = get_current_weather(** args)
177- return f " 大模型调用函数 { func_name} 得到结果: { result} "
178- else :
179- return " 大模型触发未知函数调用。"
180- else :
181- # 如果大模型没有调用函数,则直接返回回答
182- return message.get(" content" , " 大模型未生成有效回复。" )
183-
184- if __name__ == ' __main__' :
185- user_query = input (" 请输入查询内容: " )
186- result = large_model_integration(user_query)
187- print (result)
188-
189- ```
190-
191- #### 工程化调用工具
19231
32+ #### langchain框架中Tool原理
19333
19434``` python showLineNumbers
19535import json
@@ -346,6 +186,77 @@ if __name__ == "__main__":
346186"""
347187```
348188
189+ #### function calling
190+
191+ function calling 是 OpenAI 推出的一个功能,允许开发者将大模型的输出结果作为函数调用,并执行函数。一定程度上简化了代码。
192+
193+ ``` python showLineNumbers
194+ import openai
195+ import json
196+
197+ # 请设置你的 OpenAI API Key
198+ openai.api_key = " YOUR_API_KEY"
199+
200+ # 定义实际业务函数:获取天气信息
201+ def get_current_weather (location ):
202+ # 模拟获取天气信息的逻辑
203+ return f " { location} 的当前天气是晴朗。 "
204+
205+ # 定义大模型可调用的函数描述(Function Schema)
206+ functions = [
207+ {
208+ " name" : " get_current_weather" ,
209+ " description" : " 获取指定城市的天气" ,
210+ " parameters" : {
211+ " type" : " object" ,
212+ " properties" : {
213+ " location" : {" type" : " string" , " description" : " 城市名称" }
214+ },
215+ " required" : [" location" ]
216+ }
217+ }
218+ ]
219+
220+ def large_model_integration (user_input ):
221+ """
222+ 模拟大模型处理用户输入,
223+ 若识别到需要调用天气查询函数,则使用函数调用功能。
224+ """
225+ # 调用大模型接口,启用函数调用
226+ response = openai.ChatCompletion.create(
227+ model = " gpt-4-0613" , # 模型支持函数调用
228+ messages = [{" role" : " user" , " content" : user_input}],
229+ functions = functions,
230+ function_call = " auto" # 模型自动决定是否调用函数
231+ )
232+
233+ message = response[" choices" ][0 ][" message" ]
234+
235+ # 判断是否触发了函数调用
236+ if message.get(" function_call" ):
237+ func_name = message[" function_call" ][" name" ]
238+ arguments = message[" function_call" ][" arguments" ]
239+
240+ # 解析函数参数
241+ args = json.loads(arguments)
242+
243+ # 根据函数名称调用对应的函数
244+ if func_name == " get_current_weather" :
245+ result = get_current_weather(** args)
246+ return f " 大模型调用函数 { func_name} 得到结果: { result} "
247+ else :
248+ return " 大模型触发未知函数调用。"
249+ else :
250+ # 如果大模型没有调用函数,则直接返回回答
251+ return message.get(" content" , " 大模型未生成有效回复。" )
252+
253+ if __name__ == ' __main__' :
254+ user_query = input (" 请输入查询内容: " )
255+ result = large_model_integration(user_query)
256+ print (result)
257+
258+ ```
259+
349260
350261#### Mcp
351262
@@ -363,21 +274,12 @@ MCP 的缺点
3632743 . MCP 需要联网,在开发内部项目时需要额外对工具库项目封装。
364275
365276
366- #### A2A
367-
368- A2A 是 Agent 到 Agent 的通信,是多智能体系统中的一种通信方式。
369-
370-
371277### 智能体流程架构类型分析
372278
373279![ alt text] ( https://langchain-ai.github.io/langgraph/concepts/img/multi_agent/architectures.png )
374280
375281参考文章:https://langchain-ai.github.io/langgraph/concepts/multi_agent/
376282
377- ### 多智能体LLM系统失效原因
378-
379- 专门微调一个验证规则代理。拥有图片识别能力,浏览器操作能力
380-
381283以下表格总结了多智能体系统失效分类体系(MASFT)及其失效模式的发生频率:
382284
383285| 失效类别 | 发生频率 (%) | 失效模式 | 发生频率 (%) |
@@ -399,19 +301,3 @@ A2A 是 Agent 到 Agent 的通信,是多智能体系统中的一种通信方
399301
400302
401303参考文章: https://www.aimodels.fyi/papers/arxiv/why-do-multi-agent-llm-systems-fail
402-
403-
404- ## 案例分析
405-
406- ### 构建能主动提问的智能体
407-
408- 常用于医疗咨询、商城导购、智能客服等场景。
409-
410- ### 构建Human-in-the-loop的智能体
411-
412- 常用于需要人工介入的场景,例如:购买商品需要人工确认、需要人工介入的复杂任务。
413-
414- ### 构建多智能体协作的智能体
415-
416- 常用于需要多个智能体协作的场景,例如:狼人杀、三国杀、斯坦福小镇等桌面游戏。
417-
0 commit comments