zhizhi/modules/orders/ai-intake.js

80 lines
2.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* ai-intake.js - AI 辅助需求录入
* 使用 DeepSeek 分析用户提交的原始需求,结构化为标准表单
*/
const DEEPSEEK_KEY = 'sk-a9b69e9cd2dc4ca68d6aceaa84f22afb';
async function callDeepSeek(messages, options = {}) {
const body = {
model: options.model || 'deepseek-chat',
messages,
temperature: options.temperature ?? 0.3,
max_tokens: options.max_tokens ?? 2000,
stream: false,
};
const resp = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${DEEPSEEK_KEY}`,
},
body: JSON.stringify(body),
});
if (!resp.ok) throw new Error(`DeepSeek error ${resp.status}`);
const data = await resp.json();
return data.choices[0].message.content;
}
/** 用 AI 分析原始需求,提取结构化信息 */
export async function analyzeRequirement(rawText) {
const prompt = `你是一个技术项目经理助理。请分析以下客户提交的原始需求,提取关键信息并以 JSON 格式输出。
原始需求:
${rawText}
请提取:
1. projectType: 项目类型(网站/小程序/API/工具/其他)
2. summary: 一句话需求摘要20字以内
3. techStack: 可能需要的技术栈(数组,如 ["Node.js", "React"]
4. complexity: 复杂度评估(简单/中等/复杂)
5. estimatedDays: 预估工时(天数,数字)
6. clarity: 需求清晰度(清晰/模糊/非常模糊)
7. suggestions: 建议用户补充的信息(数组,如 ["需要明确前端框架", "需要数据库类型"]
只输出 JSON不要其他文字。格式
{
"projectType": "",
"summary": "",
"techStack": [],
"complexity": "",
"estimatedDays": 0,
"clarity": "",
"suggestions": []
}`;
try {
const result = await callDeepSeek([{ role: 'user', content: prompt }], { temperature: 0.2 });
return JSON.parse(result.replace(/```json\s*|\s*```/g, ''));
} catch (e) {
console.error('AI analyze failed:', e.message);
return null;
}
}
/** AI 自动回复确认(提交后给客户看到的信息) */
export async function generateConfirmation(order) {
const prompt = `你是一个温和友好的客服,请根据以下订单信息生成一段确认回复,告诉客户订单已收到并给出预期。
客户: ${order.name}
需求: ${order.requirements}
订单编号: ${order.id}
回复要求:简短、温暖、专业。告知订单编号已生成,开发者会尽快联系。`;
try {
return await callDeepSeek([{ role: 'user', content: prompt }], { temperature: 0.6, max_tokens: 500 });
} catch {
return `感谢您的提交!您的订单编号为 ${order.id},我们会尽快与您联系。`;
}
}