Prompt Safe技能使用说明
2026-04-01
新闻来源:网淘吧
围观:24
电脑广告
手机广告
提示词组装框架
概述
一个标准化的、保障令牌安全的提示词组装框架,确保API稳定性。该框架实现了两阶段上下文构建与内存安全阀机制,在最大化相关上下文的同时防止令牌溢出。
设计目标:

- ✅ 永不因内存相关的令牌溢出而失败
- ✅ 内存始终是可丢弃的增强项,而非刚性依赖
- ✅ 令牌预算决策集中在提示词组装层
使用场景
在以下情况使用此技能:
- 构建或修改任何组装提示词的智能体时
- 实施记忆检索系统时
- 向现有智能体添加新的提示词相关逻辑时
- 任何需要保障令牌预算安全的场景
核心工作流程
User Input
↓
Need-Memory Decision
↓
Minimal Context Build
↓
Memory Retrieval (Optional)
↓
Memory Summarization
↓
Token Estimation
↓
Safety Valve Decision
↓
Final Prompt → LLM Call
阶段详情
阶段0:基础配置
# Model Context Windows (2026-02-04)
# - MiniMax-M2.1: 204,000 tokens (default)
# - Claude 3.5 Sonnet: 200,000 tokens
# - GPT-4o: 128,000 tokens
MAX_TOKENS = 204000 # Set to your model's context limit
SAFETY_MARGIN = 0.75 * MAX_TOKENS # Conservative: 75% threshold = 153,000 tokens
MEMORY_TOP_K = 3 # Max 3 memories
MEMORY_SUMMARY_MAX = 3 lines # Max 3 lines per memory
设计理念:
- 预留25%缓冲空间用于安全保障(模型开销、估算误差、峰值波动)
- 宁可容量利用率不足,也绝不能发生溢出
阶段一:最小上下文
- 系统提示
- 最近N条消息(N=3,已修剪)
- 当前用户输入
- 默认不保留记忆
阶段二:记忆需求判定
def need_memory(user_input):
triggers = [
"previously",
"earlier we discussed",
"do you remember",
"as I mentioned before",
"continuing from",
"before we",
"last time",
"previously mentioned"
]
for trigger in triggers:
if trigger.lower() in user_input.lower():
return True
return False
阶段三:记忆检索(可选)
memories = memory_search(query=user_input, top_k=MEMORY_TOP_K)
for mem in memories:
summarized_memories.append(summarize(mem, max_lines=MEMORY_SUMMARY_MAX))
阶段四:令牌估算
计算基础上下文与摘要记忆的预估令牌数
阶段五:安全阀机制(关键)
if estimated_tokens > SAFETY_MARGIN:
base_context.append("[System Notice] Relevant memory skipped due to token budget.")
return assemble(base_context)
硬性规则:
- ❌ 绝不降级系统提示
- ❌ 绝不截断用户输入
- ❌ 禁止“侥幸拼接”
- ✅ 仅记忆层可被舍弃
阶段六:最终组装
final_prompt = assemble(base_context + summarized_memories)
return final_prompt
记忆数据标准
允许存入长期记忆
- ✅ 用户偏好 / 身份 / 长期目标
- ✅ 已确认的重要结论
- ✅ 系统级设置与规则
禁止存入长期记忆
- ❌ 原始对话记录
- ❌ 推理过程痕迹
- ❌ 临时性讨论
- ❌ 可从聊天历史中恢复的信息
快速开始
复制scripts/prompt_assemble.py到您的智能体并运行:
from prompt_assemble import build_prompt
# In your agent's prompt construction:
final_prompt = build_prompt(user_input, memory_search_fn, get_recent_dialog_fn)
资源文件
scripts/
prompt_assemble.py- 包含所有阶段的完整实现(PromptAssembler类)
references/
memory_standards.md- 详细的内存内容指南token_estimation.md- 令牌计数策略
文章底部电脑广告
手机广告位-内容正文底部


微信扫一扫,打赏作者吧~