بنقرة واحدة
prompt-engineering
Prompt 设计与优化——RAG 模板、Few-shot、Chain-of-Thought、结构化输出、Prompt Injection 防护
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Prompt 设计与优化——RAG 模板、Few-shot、Chain-of-Thought、结构化输出、Prompt Injection 防护
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Execute the check workflow from this AI development team preset. Use when the user writes /check, asks for check, or wants the corresponding team process in Codex.
Execute the dev workflow from this AI development team preset. Use when the user writes /dev, asks for dev, or wants the corresponding team process in Codex.
Execute the fix workflow from this AI development team preset. Use when the user writes /fix, asks for fix, or wants the corresponding team process in Codex.
Execute the plan workflow from this AI development team preset. Use when the user writes /plan, asks for plan, or wants the corresponding team process in Codex.
Execute the project-preset workflow from this AI development team preset. Use when the user writes /project-preset, asks for project-preset, or wants the corresponding team process in Codex.
Execute the review-all workflow from this AI development team preset. Use when the user writes /review-all, asks for review-all, or wants the corresponding team process in Codex.
| name | prompt-engineering |
| description | Prompt 设计与优化——RAG 模板、Few-shot、Chain-of-Thought、结构化输出、Prompt Injection 防护 |
设计、优化和版本化 LLM Prompt 模板。
[System Prompt]
1. 角色定义(你是什么,你做什么)
2. 行为约束(你不做什么,边界在哪)
3. 输出格式要求(结构、长度、语言)
[User Prompt]
1. 上下文(检索到的内容)
2. 用户问题
3. 输出指令
# prompts/rag.py
RAG_SYSTEM = """\
你是一个精确的知识库问答助手。
规则:
1. 只基于"参考资料"部分的内容回答
2. 如果资料不足以回答,说"根据现有资料无法回答此问题"
3. 不要编造、推断或补充资料中没有的信息
4. 引用具体来源(如:根据[来源]...)
5. 使用中文回答,保持简洁
"""
RAG_USER = """\
参考资料:
{context}
---
问题:{question}
"""
# 结构化输出模板
RAG_JSON_SYSTEM = """\
你是一个知识库问答助手。请以 JSON 格式回答。
输出格式:
{{
"answer": "回答内容",
"confidence": 0.0-1.0,
"sources": ["来源1", "来源2"],
"can_answer": true/false
}}
"""
# 1. Few-shot 示例(稳定输出格式)
FEW_SHOT_EXAMPLES = """
示例1:
问题:Python 中如何读取文件?
资料:Python 使用 open() 函数读取文件,with 语句确保文件自动关闭。
回答:使用 `with open('file.txt', 'r') as f: content = f.read()`
示例2:
问题:什么是量子计算?
资料:本知识库包含 Python 和 Web 开发相关内容。
回答:根据现有资料无法回答此问题。
"""
# 2. Chain-of-Thought(复杂推理)
COT_SUFFIX = """
请先分析相关资料,再给出答案。
思考过程:
答案:
"""
# 3. 限制输出长度
LENGTH_CONSTRAINT = "回答控制在 200 字以内,重点突出。"
import re
INJECTION_PATTERNS = [
r'ignore (all |previous |above )?instructions?',
r'you are now',
r'new (system |role |persona)',
r'<\|.*?\|>', # special tokens
r'\[INST\]', # llama tokens
r'###\s*(System|Human|Assistant)',
]
def sanitize_input(text: str, max_length: int = 2000) -> str:
for pattern in INJECTION_PATTERNS:
text = re.sub(pattern, '[REMOVED]', text, flags=re.IGNORECASE)
return text[:max_length].strip()
# prompts/__init__.py
from dataclasses import dataclass
@dataclass
class PromptVersion:
version: str
system: str
user_template: str
notes: str = ""
PROMPTS: dict[str, PromptVersion] = {
"rag_v1": PromptVersion(
version="1.0",
system=RAG_SYSTEM,
user_template=RAG_USER,
notes="基础 RAG 模板",
),
"rag_v2": PromptVersion(
version="2.0",
system=RAG_SYSTEM_V2,
user_template=RAG_USER_V2,
notes="加入引用来源,改善幻觉问题",
),
}
def get_prompt(name: str) -> PromptVersion:
if name not in PROMPTS:
raise ValueError(f"Unknown prompt: {name}")
return PROMPTS[name]