Skip to main content

mofa_foundation/prompt/
presets.rs

1//! 预置 Prompt 模板库
2//!
3//! 提供常用场景的 Prompt 模板
4
5use super::registry::PromptRegistry;
6use super::template::{PromptTemplate, PromptVariable};
7
8// ============================================================================
9// 通用助手模板
10// ============================================================================
11
12/// 通用助手系统提示
13pub fn general_assistant() -> PromptTemplate {
14    PromptTemplate::new("general-assistant")
15        .with_name("通用助手")
16        .with_description("通用 AI 助手系统提示")
17        .with_content(
18            "你是一个乐于助人的 AI 助手。请以清晰、准确、专业的方式回答用户的问题。\n\n\
19            回答时请注意:\n\
20            1. 保持回答简洁明了\n\
21            2. 如果不确定,请诚实说明\n\
22            3. 必要时提供示例说明",
23        )
24        .with_tag("system")
25        .with_tag("general")
26}
27
28/// 专业角色助手
29pub fn role_assistant() -> PromptTemplate {
30    PromptTemplate::new("role-assistant")
31        .with_name("角色助手")
32        .with_description("可自定义角色的助手模板")
33        .with_content(
34            "你是一个专业的{role}。你的专长是{expertise}。\n\n\
35            在回答问题时,请:\n\
36            1. 运用你的专业知识\n\
37            2. 提供有见地的分析\n\
38            3. 给出可操作的建议",
39        )
40        .with_variable(
41            PromptVariable::new("role").with_description("角色名称,如:软件工程师、数据分析师"),
42        )
43        .with_variable(
44            PromptVariable::new("expertise")
45                .with_description("专业领域")
46                .with_default("解决问题和提供帮助"),
47        )
48        .with_tag("system")
49        .with_tag("role")
50}
51
52// ============================================================================
53// 代码相关模板
54// ============================================================================
55
56/// 代码审查模板
57pub fn code_review() -> PromptTemplate {
58    PromptTemplate::new("code-review")
59        .with_name("代码审查")
60        .with_description("专业代码审查模板")
61        .with_content(
62            "请作为一个资深的{language}开发者,审查以下代码:\n\n\
63            ```{language}\n{code}\n```\n\n\
64            请从以下方面进行审查:\n\
65            1. **代码质量**:可读性、命名规范、代码结构\n\
66            2. **潜在问题**:Bug、边界情况、错误处理\n\
67            3. **性能**:效率问题、优化建议\n\
68            4. **安全性**:安全漏洞、敏感信息处理\n\
69            5. **最佳实践**:设计模式、惯用写法\n\n\
70            请提供具体的改进建议和示例代码。",
71        )
72        .with_variable(
73            PromptVariable::new("language")
74                .with_description("编程语言")
75                .with_default("代码"),
76        )
77        .with_variable(PromptVariable::new("code").with_description("要审查的代码"))
78        .with_tag("code")
79        .with_tag("review")
80}
81
82/// 代码解释模板
83pub fn code_explain() -> PromptTemplate {
84    PromptTemplate::new("code-explain")
85        .with_name("代码解释")
86        .with_description("解释代码功能和原理")
87        .with_content(
88            "请详细解释以下{language}代码的功能和工作原理:\n\n\
89            ```{language}\n{code}\n```\n\n\
90            请包含:\n\
91            1. **功能概述**:代码的主要用途\n\
92            2. **逐行/逐块解释**:关键部分的详细说明\n\
93            3. **使用示例**:如何调用或使用这段代码\n\
94            4. **注意事项**:使用时需要注意的问题",
95        )
96        .with_variable(
97            PromptVariable::new("language")
98                .with_description("编程语言")
99                .with_default("代码"),
100        )
101        .with_variable(PromptVariable::new("code").with_description("要解释的代码"))
102        .with_tag("code")
103        .with_tag("explain")
104}
105
106/// 代码生成模板
107pub fn code_generate() -> PromptTemplate {
108    PromptTemplate::new("code-generate")
109        .with_name("代码生成")
110        .with_description("根据需求生成代码")
111        .with_content(
112            "请使用 {language} 编写代码实现以下功能:\n\n\
113            **需求描述**:\n{requirement}\n\n\
114            **要求**:\n\
115            1. 代码应该简洁、高效、可读性强\n\
116            2. 包含必要的注释说明\n\
117            3. 考虑边界情况和错误处理\n\
118            4. 遵循 {language} 的最佳实践",
119        )
120        .with_variable(PromptVariable::new("language").with_description("编程语言"))
121        .with_variable(PromptVariable::new("requirement").with_description("功能需求描述"))
122        .with_tag("code")
123        .with_tag("generate")
124}
125
126/// 代码重构模板
127pub fn code_refactor() -> PromptTemplate {
128    PromptTemplate::new("code-refactor")
129        .with_name("代码重构")
130        .with_description("重构和优化代码")
131        .with_content(
132            "请重构以下{language}代码,{goal}:\n\n\
133            ```{language}\n{code}\n```\n\n\
134            重构时请:\n\
135            1. 保持功能不变\n\
136            2. 提高代码质量\n\
137            3. 解释你的改动及原因\n\
138            4. 提供重构后的完整代码",
139        )
140        .with_variable(
141            PromptVariable::new("language")
142                .with_description("编程语言")
143                .with_default("代码"),
144        )
145        .with_variable(PromptVariable::new("code").with_description("要重构的代码"))
146        .with_variable(
147            PromptVariable::new("goal")
148                .with_description("重构目标")
149                .with_default("使其更加清晰、高效"),
150        )
151        .with_tag("code")
152        .with_tag("refactor")
153}
154
155/// 单元测试生成模板
156pub fn code_test() -> PromptTemplate {
157    PromptTemplate::new("code-test")
158        .with_name("测试生成")
159        .with_description("为代码生成单元测试")
160        .with_content(
161            "请为以下{language}代码编写单元测试:\n\n\
162            ```{language}\n{code}\n```\n\n\
163            测试要求:\n\
164            1. 覆盖主要功能路径\n\
165            2. 包含边界情况测试\n\
166            3. 包含错误情况测试\n\
167            4. 使用 {test_framework} 测试框架",
168        )
169        .with_variable(PromptVariable::new("language").with_description("编程语言"))
170        .with_variable(PromptVariable::new("code").with_description("要测试的代码"))
171        .with_variable(
172            PromptVariable::new("test_framework")
173                .with_description("测试框架")
174                .with_default("标准"),
175        )
176        .with_tag("code")
177        .with_tag("test")
178}
179
180// ============================================================================
181// 写作和文档模板
182// ============================================================================
183
184/// 技术文档模板
185pub fn tech_doc() -> PromptTemplate {
186    PromptTemplate::new("tech-doc")
187        .with_name("技术文档")
188        .with_description("撰写技术文档")
189        .with_content(
190            "请为 {subject} 撰写技术文档。\n\n\
191            文档应包含:\n\
192            1. **概述**:简要介绍主题\n\
193            2. **安装/配置**:如何开始使用\n\
194            3. **使用指南**:基本用法和示例\n\
195            4. **API 参考**:详细接口说明(如适用)\n\
196            5. **常见问题**:FAQ 和故障排除\n\n\
197            目标受众:{audience}",
198        )
199        .with_variable(PromptVariable::new("subject").with_description("文档主题"))
200        .with_variable(
201            PromptVariable::new("audience")
202                .with_description("目标读者")
203                .with_default("开发者"),
204        )
205        .with_tag("doc")
206        .with_tag("writing")
207}
208
209/// 总结模板
210pub fn summarize() -> PromptTemplate {
211    PromptTemplate::new("summarize")
212        .with_name("内容总结")
213        .with_description("总结长文本内容")
214        .with_content(
215            "请总结以下内容:\n\n{content}\n\n\
216            总结要求:\n\
217            1. 长度约 {length}\n\
218            2. 保留关键信息和主要观点\n\
219            3. 使用清晰的结构组织\n\
220            4. 语言简练准确",
221        )
222        .with_variable(PromptVariable::new("content").with_description("要总结的内容"))
223        .with_variable(
224            PromptVariable::new("length")
225                .with_description("目标长度")
226                .with_default("200-300字"),
227        )
228        .with_tag("writing")
229        .with_tag("summary")
230}
231
232/// 翻译模板
233pub fn translate() -> PromptTemplate {
234    PromptTemplate::new("translate")
235        .with_name("翻译")
236        .with_description("翻译文本")
237        .with_content(
238            "请将以下内容从{source_lang}翻译成{target_lang}:\n\n\
239            {content}\n\n\
240            翻译要求:\n\
241            1. 保持原文含义准确\n\
242            2. 符合目标语言的表达习惯\n\
243            3. 保持专业术语的准确性\n\
244            4. 保持原文的语气和风格",
245        )
246        .with_variable(
247            PromptVariable::new("source_lang")
248                .with_description("源语言")
249                .with_default("英文"),
250        )
251        .with_variable(
252            PromptVariable::new("target_lang")
253                .with_description("目标语言")
254                .with_default("中文"),
255        )
256        .with_variable(PromptVariable::new("content").with_description("要翻译的内容"))
257        .with_tag("writing")
258        .with_tag("translation")
259}
260
261// ============================================================================
262// 分析和推理模板
263// ============================================================================
264
265/// 问题分析模板
266pub fn analyze() -> PromptTemplate {
267    PromptTemplate::new("analyze")
268        .with_name("问题分析")
269        .with_description("分析问题并给出解决方案")
270        .with_content(
271            "请分析以下问题并给出解决方案:\n\n\
272            **问题描述**:\n{problem}\n\n\
273            **上下文**:\n{context}\n\n\
274            请提供:\n\
275            1. **问题分析**:问题的根本原因\n\
276            2. **解决方案**:可行的解决方法\n\
277            3. **优劣分析**:各方案的优缺点\n\
278            4. **推荐方案**:最佳建议及理由",
279        )
280        .with_variable(PromptVariable::new("problem").with_description("问题描述"))
281        .with_variable(
282            PromptVariable::new("context")
283                .with_description("相关背景信息")
284                .with_default("无额外上下文"),
285        )
286        .with_tag("analysis")
287        .with_tag("problem-solving")
288}
289
290/// 对比分析模板
291pub fn compare() -> PromptTemplate {
292    PromptTemplate::new("compare")
293        .with_name("对比分析")
294        .with_description("对比多个选项")
295        .with_content(
296            "请对比分析以下选项:\n\n\
297            {options}\n\n\
298            对比维度:{dimensions}\n\n\
299            请提供:\n\
300            1. **详细对比表格**\n\
301            2. **各选项优缺点**\n\
302            3. **适用场景分析**\n\
303            4. **推荐建议**",
304        )
305        .with_variable(PromptVariable::new("options").with_description("要对比的选项列表"))
306        .with_variable(
307            PromptVariable::new("dimensions")
308                .with_description("对比维度")
309                .with_default("功能、性能、易用性、成本"),
310        )
311        .with_tag("analysis")
312        .with_tag("comparison")
313}
314
315// ============================================================================
316// ReAct Agent 模板
317// ============================================================================
318
319/// ReAct 推理系统提示
320pub fn react_system() -> PromptTemplate {
321    PromptTemplate::new("react-system")
322        .with_name("ReAct 系统提示")
323        .with_description("ReAct Agent 的系统提示")
324        .with_content(
325            "你是一个使用 ReAct(Reasoning + Acting)方法解决问题的 AI Agent。\n\n\
326            你可以使用以下工具:\n{tools}\n\n\
327            解决问题时,请遵循以下步骤:\n\n\
328            1. **Thought(思考)**:分析当前情况,决定下一步行动\n\
329            2. **Action(行动)**:选择合适的工具并执行\n\
330            3. **Observation(观察)**:分析工具返回的结果\n\
331            4. 重复上述步骤直到得到答案\n\
332            5. **Final Answer(最终答案)**:给出完整的回答\n\n\
333            格式要求:\n\
334            - Thought: 你的思考过程\n\
335            - Action: 工具名称[参数]\n\
336            - Observation: 工具返回的结果\n\
337            - Final Answer: 你的最终回答",
338        )
339        .with_variable(PromptVariable::new("tools").with_description("可用工具列表"))
340        .with_tag("react")
341        .with_tag("agent")
342}
343
344/// ReAct 任务模板
345pub fn react_task() -> PromptTemplate {
346    PromptTemplate::new("react-task")
347        .with_name("ReAct 任务")
348        .with_description("ReAct Agent 的任务模板")
349        .with_content("请完成以下任务:\n\n{task}\n\n开始你的推理和行动:")
350        .with_variable(PromptVariable::new("task").with_description("任务描述"))
351        .with_tag("react")
352        .with_tag("agent")
353}
354
355// ============================================================================
356// 多 Agent 协作模板
357// ============================================================================
358
359/// 辩论者模板
360pub fn debater() -> PromptTemplate {
361    PromptTemplate::new("debater")
362        .with_name("辩论者")
363        .with_description("辩论模式中的辩论者角色")
364        .with_content(
365            "你是辩论中的{position}方。\n\n\
366            辩题:{topic}\n\n\
367            前述观点:\n{previous}\n\n\
368            请从你的立场出发,提供有力的论点。注意:\n\
369            1. 基于事实和逻辑论证\n\
370            2. 回应对方的观点\n\
371            3. 保持专业和理性",
372        )
373        .with_variable(
374            PromptVariable::new("position")
375                .with_description("辩论立场")
376                .with_enum(vec!["正".to_string(), "反".to_string()]),
377        )
378        .with_variable(PromptVariable::new("topic").with_description("辩论话题"))
379        .with_variable(
380            PromptVariable::new("previous")
381                .with_description("之前的辩论内容")
382                .with_default("这是辩论的开始"),
383        )
384        .with_tag("multi-agent")
385        .with_tag("debate")
386}
387
388/// 监督者模板
389pub fn supervisor() -> PromptTemplate {
390    PromptTemplate::new("supervisor")
391        .with_name("监督者")
392        .with_description("监督模式中的监督者角色")
393        .with_content(
394            "你是一个团队监督者,负责评估团队成员的工作成果。\n\n\
395            **任务**:{task}\n\n\
396            **团队成员的回答**:\n{responses}\n\n\
397            请:\n\
398            1. 评估每个回答的质量\n\
399            2. 指出各自的优点和不足\n\
400            3. 综合最佳内容给出最终答案\n\
401            4. 给出改进建议",
402        )
403        .with_variable(PromptVariable::new("task").with_description("任务描述"))
404        .with_variable(PromptVariable::new("responses").with_description("团队成员的回答"))
405        .with_tag("multi-agent")
406        .with_tag("supervisor")
407}
408
409/// 聚合者模板
410pub fn aggregator() -> PromptTemplate {
411    PromptTemplate::new("aggregator")
412        .with_name("聚合者")
413        .with_description("并行模式中的结果聚合角色")
414        .with_content(
415            "多个 Agent 已经分别处理了以下任务:\n\n\
416            **原始任务**:{task}\n\n\
417            **各 Agent 的结果**:\n{results}\n\n\
418            请综合以上结果:\n\
419            1. 找出共同点和差异点\n\
420            2. 整合各方优势\n\
421            3. 形成一个完整、准确的最终答案",
422        )
423        .with_variable(PromptVariable::new("task").with_description("原始任务"))
424        .with_variable(PromptVariable::new("results").with_description("各 Agent 的结果"))
425        .with_tag("multi-agent")
426        .with_tag("aggregation")
427}
428
429// ============================================================================
430// 注册表预加载
431// ============================================================================
432
433/// 创建包含所有预置模板的注册中心
434pub fn create_preset_registry() -> PromptRegistry {
435    let mut registry = PromptRegistry::new();
436
437    // 通用助手
438    registry.register(general_assistant());
439    registry.register(role_assistant());
440
441    // 代码相关
442    registry.register(code_review());
443    registry.register(code_explain());
444    registry.register(code_generate());
445    registry.register(code_refactor());
446    registry.register(code_test());
447
448    // 写作和文档
449    registry.register(tech_doc());
450    registry.register(summarize());
451    registry.register(translate());
452
453    // 分析和推理
454    registry.register(analyze());
455    registry.register(compare());
456
457    // ReAct Agent
458    registry.register(react_system());
459    registry.register(react_task());
460
461    // 多 Agent 协作
462    registry.register(debater());
463    registry.register(supervisor());
464    registry.register(aggregator());
465
466    registry
467}
468
469/// 将预置模板加载到现有注册中心
470pub fn load_presets(registry: &mut PromptRegistry) {
471    let presets = create_preset_registry();
472    registry.merge(presets);
473}
474
475#[cfg(test)]
476mod tests {
477    use super::*;
478
479    #[test]
480    fn test_preset_templates() {
481        let registry = create_preset_registry();
482
483        // 验证模板存在
484        assert!(registry.contains("general-assistant"));
485        assert!(registry.contains("code-review"));
486        assert!(registry.contains("react-system"));
487        assert!(registry.contains("supervisor"));
488
489        // 验证标签
490        let code_templates = registry.find_by_tag("code");
491        assert!(code_templates.len() >= 4);
492
493        let agent_templates = registry.find_by_tag("agent");
494        assert!(agent_templates.len() >= 2);
495    }
496
497    #[test]
498    fn test_code_review_template() {
499        let template = code_review();
500
501        let result = template
502            .render(&[
503                ("language", "rust"),
504                ("code", "fn main() { info!(\"Hello\"); }"),
505            ])
506            .unwrap();
507
508        assert!(result.contains("rust"));
509        assert!(result.contains("fn main"));
510    }
511
512    #[test]
513    fn test_role_assistant_template() {
514        let template = role_assistant();
515
516        // 使用默认值
517        let result = template.render(&[("role", "数据分析师")]).unwrap();
518
519        assert!(result.contains("数据分析师"));
520        assert!(result.contains("解决问题和提供帮助")); // 默认值
521    }
522
523    #[test]
524    fn test_react_system_template() {
525        let template = react_system();
526
527        let tools = "1. search: 搜索网页\n2. calculator: 计算器";
528        let result = template.render(&[("tools", tools)]).unwrap();
529
530        assert!(result.contains("ReAct"));
531        assert!(result.contains("search"));
532        assert!(result.contains("Thought"));
533    }
534
535    #[test]
536    fn test_translate_template() {
537        let template = translate();
538
539        let result = template.render(&[("content", "Hello, World!")]).unwrap();
540
541        assert!(result.contains("Hello, World!"));
542        assert!(result.contains("英文")); // 默认值
543        assert!(result.contains("中文")); // 默认值
544    }
545
546    #[test]
547    fn test_load_presets() {
548        let mut registry = PromptRegistry::new();
549        assert!(registry.is_empty());
550
551        load_presets(&mut registry);
552
553        assert!(!registry.is_empty());
554        assert!(registry.len() >= 15);
555    }
556}