/** * Phase IV — analyze 意图 Prompt 种子脚本 * * 写入 Prompt 模板: * 1. SSA_ANALYZE_PLAN — 指导 LLM 用自然语言解释分析方案 * * 运行方式: npx tsx scripts/seed-ssa-phase4-prompts.ts * 前置: 数据库已启动 */ import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); interface PromptDef { code: string; name: string; description: string; variables: string[]; content: string; modelConfig: Record; } const PROMPTS: PromptDef[] = [ { code: 'SSA_ANALYZE_PLAN', name: 'SSA 分析方案说明指令', description: 'Phase IV — analyze 意图,指导 LLM 向用户解释生成的分析方案', variables: [], content: `## 当前任务:解释分析方案 系统已根据用户的分析需求和数据特征,通过决策引擎生成了一份统计分析方案。方案详情在"工具执行结果"中给出。 ### 输出要求(严格遵守) 1. **首先用一句话总结推荐方案**(如:"建议对BMI和血压做Pearson相关分析,并配合散点图展示") 2. 然后简要说明: - **为什么选择这些方法** — 结合数据特征和研究目的解释 - **分析步骤的逻辑** — 用非技术语言说明各步骤的作用和顺序 - **需要注意的事项** — 如前提条件检验、可能的降级方案 3. 最后提示用户确认方案 ### 禁止事项 - 严禁重复列出步骤编号和工具代码(用户已在右侧面板看到) - 严禁使用过多统计术语,要用用户能理解的语言 - 严禁编造数据特征或假设用户未提供的信息 - 严禁直接执行分析,只解释方案`, modelConfig: { model: 'deepseek-v3', temperature: 0.5, maxTokens: 1200 }, }, ]; async function upsertPrompt(def: PromptDef): Promise { const existing = await prisma.prompt_templates.findUnique({ where: { code: def.code }, }); if (existing) { const latestVersion = await prisma.prompt_versions.findFirst({ where: { template_id: existing.id }, orderBy: { version: 'desc' }, }); const activeVersion = await prisma.prompt_versions.findFirst({ where: { template_id: existing.id, status: 'ACTIVE' }, }); if (activeVersion && activeVersion.content === def.content) { console.log(` ⏭️ ${def.code} 内容未变化,跳过`); return; } const newVersion = (latestVersion?.version ?? 0) + 1; await prisma.prompt_versions.updateMany({ where: { template_id: existing.id, status: 'ACTIVE' }, data: { status: 'ARCHIVED' }, }); await prisma.prompt_versions.create({ data: { template_id: existing.id, version: newVersion, content: def.content, model_config: def.modelConfig, status: 'ACTIVE', changelog: `Phase IV v${newVersion}: ${def.description}`, created_by: 'system-seed', }, }); console.log(` ✅ ${def.code} 更新到 v${newVersion}`); } else { const template = await prisma.prompt_templates.create({ data: { code: def.code, name: def.name, module: 'SSA', description: def.description, variables: def.variables, }, }); await prisma.prompt_versions.create({ data: { template_id: template.id, version: 1, content: def.content, model_config: def.modelConfig, status: 'ACTIVE', changelog: `Phase IV v1.0: ${def.description}`, created_by: 'system-seed', }, }); console.log(` ✅ ${def.code} 创建成功 (id=${template.id})`); } } async function main() { console.log('🚀 开始写入 SSA Phase IV Prompt 模板...\n'); for (const def of PROMPTS) { console.log(`📝 处理 ${def.code} (${def.name})...`); await upsertPrompt(def); } console.log(`\n✅ 全部 ${PROMPTS.length} 个 Prompt 模板写入完成!`); } main() .catch(e => { console.error('❌ Seed 失败:', e); process.exit(1); }) .finally(() => prisma.$disconnect());