11# 如何使用动态提示词(Live Prompts)
22
3- OxyGent 提供了动态提示词功能,允许您在运行时动态加载和更新智能体的提示词,无需重启应用程序。这个功能特别适用于需要频繁调整提示词或在生产环境中进行 A/B 测试的场景 。
3+ OxyGent 提供了动态提示词功能,允许您在运行时动态加载和更新智能体的提示词,无需重启应用程序,适用于需要频繁调整提示词的场景 。
44
55## 什么是动态提示词
66
77动态提示词(Live Prompts)是一个存储在数据库中的提示词管理系统,支持:
8- - ** 实时更新 ** :在运行时修改提示词,无需重启应用
8+ - ** 自动集成 ** :LocalAgent 内置支持,无需额外配置
99- ** 版本管理** :保存提示词的历史版本,支持回滚
10- - ** 热重载 ** :提示词修改后立即生效
11- - ** 备用机制 ** :当动态提示词不可用时,自动使用默认提示词
10+ - ** 备用机制 ** :当动态提示词不可用时,自动使用代码中的默认提示词
11+ - ** 灵活开关 ** :可选择启用或禁用 live prompt 功能
1212
1313## 基本用法
1414
15- ### 1. 导入动态提示词功能
15+ ### 方式 1: 默认用法(推荐)
16+
17+ 直接在 Agent 中设置 ` prompt ` 参数,系统会自动使用 live prompt 功能:
1618
1719``` python
18- from oxygent.live_prompt import get_live_prompts
20+ oxy.ReActAgent(
21+ name = " time_agent" ,
22+ desc = " A tool that can query the time" ,
23+ prompt = " You are a time management assistant. Help users with time-related queries." ,
24+ tools = [" time_tools" ],
25+ # use_live_prompt=True 是默认值,可以省略
26+ )
1927```
2028
21- ### 2. 在智能体中使用动态提示词
29+ ** 工作原理:**
30+ 1 . Agent 初始化时,自动从存储中查找键为 ` time_agent_prompt ` 的 live prompt
31+ 2 . 如果找到且激活,使用存储中的提示词
32+ 3 . 如果未找到,使用代码中的 ` prompt ` 参数作为后备
33+
34+ ### 方式 2: 自定义 prompt_key
35+
36+ 如果想使用不同的键名:
2237
2338``` python
2439oxy.ReActAgent(
2540 name = " time_agent" ,
2641 desc = " A tool that can query the time" ,
27- prompt = get_live_prompts(
28- " time_agent_prompt" , # 提示词键名
29- " You are a time management assistant. Help users with time-related queries." # 默认提示词
30- ),
42+ prompt = " Default prompt" ,
43+ prompt_key = " my_custom_prompt_key" , # 自定义键名
3144 tools = [" time_tools" ],
3245)
3346```
3447
35- ## 函数参数说明
48+ ### 方式 3: 禁用 Live Prompt
49+
50+ 如果不需要动态提示词功能(仅使用静态提示词):
3651
37- ` get_live_prompts(prompt_key: str, default_prompt: Optional[str] = None) -> str `
52+ ``` python
53+ oxy.ReActAgent(
54+ name = " time_agent" ,
55+ desc = " A tool that can query the time" ,
56+ prompt = " You are a time management assistant." ,
57+ tools = [" time_tools" ],
58+ use_live_prompt = False # 禁用 live prompt
59+ )
60+ ```
61+
62+ ## 参数说明
3863
39- ** 参数:**
40- - ` prompt_key ` (str): 提示词的唯一标识符,用于从存储中检索提示词
41- - ` default_prompt ` (Optional[ str] ): 默认提示词,当动态提示词不存在或不可用时使用
64+ ### LocalAgent 的 Live Prompt 相关参数
4265
43- ** 工作逻辑:**
44- 1 . 首先尝试从存储系统中使用 ` prompt_key ` 解析提示词
45- 2 . 如果未找到且提供了 ` default_prompt ` ,则使用默认提示词
46- 3 . 如果未找到且 ` default_prompt ` 为 None 或空,则返回空字符串
66+ - ** ` prompt ` ** (str): 提示词内容,作为后备使用
67+ - ** ` prompt_key ` ** (str, 可选): Live prompt 的键名
68+ - 默认值: ` "{agent_name}_prompt" `
69+ - 用于从存储中查找动态提示词
70+ - ** ` use_live_prompt ` ** (bool, 可选): 是否启用 live prompt 功能
71+ - 默认值: ` True `
72+ - 设为 ` False ` 时只使用代码中的 ` prompt ` 参数
4773
4874## 完整示例
4975
@@ -54,7 +80,6 @@ import asyncio
5480import os
5581
5682from oxygent import MAS , Config, oxy, preset_tools
57- from oxygent.live_prompt import get_live_prompts
5883
5984Config.set_agent_llm_model(" default_llm" )
6085
@@ -66,42 +91,39 @@ oxy_space = [
6691 model_name = os.getenv(" DEFAULT_LLM_MODEL_NAME" ),
6792 ),
6893 preset_tools.time_tools,
94+ # 使用系统默认提示词
6995 oxy.ReActAgent(
7096 name = " time_agent" ,
7197 desc = " A tool that can query the time" ,
72- prompt = get_live_prompts(
73- " time_agent_prompt" ,
74- " You are a time management assistant. Help users with time-related queries."
75- ),
98+ prompt = " You are a time management assistant. Help users with time-related queries." ,
7699 tools = [" time_tools" ],
100+ use_live_prompt = False # 关闭动态提示词,且prompt为空,则使用系统默认提示词
77101 ),
78102 preset_tools.file_tools,
103+
104+ # 只使用代码中的提示词
79105 oxy.ReActAgent(
80106 name = " file_agent" ,
81107 desc = " A tool that can operate the file system" ,
82108 tools = [" file_tools" ],
83- prompt = get_live_prompts(
84- " file_agent_prompt" ,
85- " You are a file system assistant. Help users with file operations safely and efficiently."
86- )
109+ prompt = " You are a file system assistant. Help users with file operations safely and efficiently." ,
110+ use_live_prompt = False # 关闭动态提示词,则使用代码中的 prompt 参数
87111 ),
88112 preset_tools.math_tools,
113+
114+ # 使用 动态提示词
89115 oxy.ReActAgent(
90116 name = " math_agent" ,
91117 desc = " A tool that can perform mathematical calculations." ,
92118 tools = [" math_tools" ],
93- prompt = get_live_prompts(
94- " math_agent_prompt" # 没有默认提示词,如果不存在将返回空字符串
95- ),
119+ prompt = " You are a math assistant. Help users with mathematical calculations." ,
96120 ),
121+ # 使用 动态提示词
97122 oxy.ReActAgent(
98123 is_master = True ,
99124 name = " master_agent" ,
100125 sub_agents = [" time_agent" , " file_agent" , " math_agent" ],
101- prompt = get_live_prompts(
102- " master_agent_prompt" ,
103- " " # 空字符串作为默认值,将使用系统默认提示词
104- ),
126+ prompt = " You are the master agent. Coordinate the actions of your sub-agents effectively." ,
105127 ),
106128]
107129
@@ -115,57 +137,96 @@ if __name__ == "__main__":
115137 asyncio.run(main())
116138```
117139
118- ## 使用场景
140+ ## 热更新提示词
119141
120- ### 1. 生产环境优化
121- 在生产环境中,您可以通过 Web 界面实时调整提示词,优化智能体的表现,无需重启服务。
142+ 修改存储中的提示词后,需要手动触发热更新才能生效:
122143
123- ### 2. A/B 测试
124- 通过动态切换不同版本的提示词,可以快速进行 A/B 测试,比较不同提示词的效果。
144+ ### 方法 1: 通过 Agent 实例
125145
126- ### 3. 多语言支持
127- 根据用户的语言偏好,动态加载对应语言的提示词。
146+ ``` python
147+ # 获取 agent 实例
148+ agent = mas.oxy_name_to_oxy[" time_agent" ]
128149
129- ### 4. 渐进式优化
130- 在开发过程中,可以先使用默认提示词启动系统,然后逐步添加和优化动态提示词。
150+ # 热更新提示词
151+ success = await agent.reload_prompt()
152+ if success:
153+ print (" 提示词已更新" )
154+ ```
131155
132- ## 最佳实践
156+ ### 方法 2: 通过便捷函数
133157
134- ### 1. 提供有意义的默认提示词
135158``` python
136- prompt= get_live_prompts(
137- " customer_service_prompt" ,
138- " You are a helpful customer service assistant. Be polite and professional."
159+ from oxygent.live_prompt.wrapper import (
160+ hot_reload_agent, # 按 agent 名称更新
161+ hot_reload_prompt, # 按 prompt_key 更新
162+ hot_reload_all_prompts # 更新所有 agents
139163)
140- ```
141164
142- ### 2. 使用描述性的键名
143- ``` python
144- # 好的做法
145- prompt= get_live_prompts(" email_summarizer_prompt" , default_prompt)
165+ # 热更新单个 agent
166+ await hot_reload_agent(" time_agent" )
146167
147- # 避免的做法
148- prompt= get_live_prompts(" prompt1" , default_prompt)
168+ # 热更新使用指定 prompt_key 的所有 agents
169+ await hot_reload_prompt(" time_agent_prompt" )
170+
171+ # 热更新所有 agents
172+ await hot_reload_all_prompts()
149173```
150174
151- ### 3. 处理空提示词情况
175+ ### 方法 3: 在保存时自动触发(推荐)
176+
177+ 在提示词管理平台的保存 API 中自动触发热更新:
178+
152179``` python
153- # 对于可选的提示词,可以不提供默认值
154- prompt= get_live_prompts(" optional_enhancement_prompt" )
180+ from oxygent.live_prompt.manager import PromptManager
181+ from oxygent.live_prompt.wrapper import hot_reload_prompt
182+
183+ # 保存提示词
184+ await manager.save_prompt(
185+ prompt_key = " time_agent_prompt" ,
186+ prompt_content = " Updated prompt content..."
187+ )
188+
189+ # 自动触发热更新
190+ await hot_reload_prompt(" time_agent_prompt" )
155191```
156192
157193## 配置要求
158194
159195动态提示词功能需要配置数据库连接:
160196- 支持 Elasticsearch 作为主要存储
161- - 当 ES 不可用时,自动回退到 LocalEs(本地存储 )
197+ - 当 ES 不可用时,自动回退到 LocalEs(本地文件存储 )
162198- 通过 ` Config ` 系统配置数据库连接参数
163199
200+
164201## 注意事项
165202
166- 1 . ** 性能考虑** :动态提示词会在首次使用时从数据库加载,后续会使用缓存
167- 2 . ** 错误处理** :当动态提示词系统不可用时,会自动使用默认提示词,确保系统稳定运行
168- 3 . ** 版本管理** :系统会自动保存提示词的修改历史,支持版本回滚
203+ 1 . ** 向后兼容** :现有代码无需修改,默认启用 live prompt 功能
204+ 2 . ** 性能考虑** :
205+ - 提示词在初始化时从数据库加载一次,之后使用缓存
206+ - 禁用 live prompt 的 Agent 性能略好(不查询数据库)
207+ 3 . ** 错误处理** :当 live prompt 系统不可用时,自动使用代码中的 ` prompt ` 参数,确保系统稳定运行
208+ 4 . ** 版本管理** :系统会自动保存提示词的修改历史,支持版本回滚
209+ 5 . ** 灵活控制** :可以为每个 Agent 单独设置是否启用 live prompt
210+
211+ ## 常见问题
212+
213+ ### Q1: 如何禁用 live prompt 功能?
214+ ** A** : 设置 ` use_live_prompt=False ` 参数。
215+
216+ ### Q2: prompt_key 的默认值是什么?
217+ ** A** : ` {agent_name}_prompt ` ,例如 Agent 名为 ` time_agent ` ,则默认 ` prompt_key ` 为 ` time_agent_prompt ` 。
218+
219+ ### Q3: 如果存储中没有提示词会怎样?
220+ ** A** : 会使用代码中的 ` prompt ` 参数作为后备。
221+
222+ ### Q4: live prompt 会影响性能吗?
223+ ** A** : 影响很小。只在初始化时访问一次数据库,之后使用缓存。如有极高性能要求,可禁用 live prompt。
224+
225+ ## 相关文档
226+
227+ - [ Live Prompts 集成指南] ( ./live_prompts_integration.md ) - 详细的集成说明
228+ - [ Live Prompts 热更新指南] ( ./live_prompts_hot_reload.md ) - 热更新使用方法
229+ - [ use_live_prompt 开关参考] ( ./use_live_prompt_reference.md ) - 开关参数详解
169230
170231[ 上一章:选择智能体种类] ( ./1_4_select_agent.md )
171232[ 下一章:注册单个智能体] ( ./1_register_single_agent.md )
0 commit comments