
关于
适用于改进任何 LLM 的提示词。应用经过验证的提示工程技术来提升输出质量、减少幻觉和降低 token 使用量。
name: llm-prompt-optimizer description: "用于改进任何 LLM 的提示词。应用经过验证的提示工程技术来提升输出质量、减少幻觉并降低 token 用量。" risk: safe source: community date_added: "2026-03-04"
LLM 提示词优化器
概述
此技能将模糊、含混或不一致的提示词转化为精确工程化的指令,能够从任何 LLM(Claude、Gemini、GPT-4、Llama 等)可靠地产出高质量输出。它应用系统化的提示工程框架——从零样本到少样本、思维链和结构化输出模式。
何时使用此技能
- 当提示词返回不一致、模糊或幻觉结果时使用
- 当你需要从 LLM 可靠地获取结构化/JSON 输出时使用
- 当为 AI 代理或聊天机器人设计系统提示词时使用
- 当你想在不牺牲质量的情况下减少 token 用量时使用
- 当为复杂任务实现思维链推理时使用
- 当提示词在一个模型上有效但在另一个模型上失败时使用
分步指南
1. 诊断弱提示词
优化之前,确定适用的问题模式:
| 问题 | 症状 | 修复方法 | |---------|---------|-----| | 太模糊 | 通用、无用的回答 | 添加角色 + 上下文 + 约束 | | 无结构 | 未格式化、难以解析的输出 | 明确指定输出格式 | | 幻觉 | 自信的错误回答 | 添加"如果不确定就说不知道" | | 不一致 | 每次运行答案不同 | 添加少样本示例 | | 太长 | 冗长、填充的回复 | 添加长度约束 |
2. 应用 RSCIT 框架
每个优化后的提示词应包含:
- R — 角色(Role):AI 在此交互中是谁?
- S — 情境(Situation):它需要什么上下文?
- C — 约束(Constraints):规则和限制是什么?
- I — 指令(Instructions):它具体应该做什么?
- T — 模板(Template):输出应该是什么样的?
优化前(弱提示词):
Explain machine learning.
优化后:
You are a senior ML engineer explaining concepts to a junior developer.
Context: The developer has 1 year of Python experience but no ML background.
Task: Explain supervised machine learning in simple terms.
Constraints:
- Use an analogy from everyday life
- Maximum 200 words
- No mathematical formulas
- End with one actionable next step
Format: Plain prose, no bullet points.
3. 思维链(CoT)模式
对于推理任务,指示模型逐步思考:
Solve this problem step by step, showing your work at each stage.
Only provide the final answer after completing all reasoning steps.
Problem: [your problem here]
Thinking process:
Step 1: [identify what's given]
Step 2: [identify what's needed]
Step 3: [apply logic or formula]
Step 4: [verify the answer]
Final Answer:
4. 少样本示例模式
提供 2-3 个示例来建立模式:
Classify the sentiment of customer reviews as POSITIVE, NEGATIVE, or NEUTRAL.
Examples:
Review: "This product exceeded my expectations!" -> POSITIVE
Review: "It arrived broken and support was useless." -> NEGATIVE
Review: "Product works as described, nothing special." -> NEUTRAL
Now classify:
Review: "[your review here]" ->
5. 结构化 JSON 输出模式
Extract the following information from the text below and return it as valid JSON only.
Do not include any explanation or markdown — just the raw JSON object.
Schema:
{
"name": string,
"email": string | null,
"company": string | null,
"role": string | null
}
Text: [input text here]
6. 减少幻觉模式
Answer the following question based ONLY on the provided context.
If the answer is not contained in the context, respond with exactly: "I don't have enough information to answer this."
Do not make up or infer information not present in the context.
Context:
[your context here]
Question: [your question here]
7. 提示词压缩技术
在不损失效果的情况下减少 token 数量:
# Verbose (expensive)
"Please carefully analyze the following code and provide a detailed explanation of
what it does, how it works, and any potential issues you might find."
# Compressed (efficient, same quality)
"Analyze this code: explain what it does, how it works, and flag any issues."
最佳实践
- 始终指定输出格式(JSON、markdown、纯文本、项目符号列表)
- 使用分隔符(```、---)将指令与内容分开
- 用边缘情况(空输入、异常数据)测试提示词
- 在源代码控制中版本化你的系统提示词
- 对数学、逻辑或多步骤任务添加"逐步思考"
- 不要只使用否定指令("不要冗长")——添加正面替代方案
- 不要假设模型了解你的代码库上下文——始终包含它
- 不要在不同模型之间使用相同的提示词而不做调整
兼容工具
Claude CodeCursor
标签
AI与机器学习