fix: 优化AI解析和添加调试日志
- 简化SYSTEM_PROMPT,使AI更容易理解并返回正确的JSON格式 - 添加详细日志帮助调试AI解析问题 - 改进错误处理 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
1ff0693901
commit
d862203140
@ -11,22 +11,25 @@ const SYSTEM_PROMPT = `你是一个帮助用户创建事件(纪念日或提醒
|
|||||||
|
|
||||||
当前时间:${new Date().toISOString()}
|
当前时间:${new Date().toISOString()}
|
||||||
|
|
||||||
任务:从自然语言中解析用户输入,并严格按照以下JSON格式返回:
|
任务:从自然语言中解析用户输入,并直接返回以下JSON格式(不要有其他文字解释):
|
||||||
|
|
||||||
{
|
{"type": "anniversary", "title": "事件标题", "content": "详细内容", "date": "2026-02-13T09:00:00Z", "timezone": "Asia/Shanghai", "is_lunar": false, "repeat_type": "yearly", "priority": "none", "reminder_times": []}
|
||||||
"parsed": {
|
|
||||||
"type": "anniversary 或 reminder",
|
重要字段说明:
|
||||||
"title": "事件标题(简洁准确,去除"提醒"、"帮我"等前缀)",
|
- type: "anniversary"(纪念日) 或 "reminder"(提醒)
|
||||||
"content": "详细内容(仅提醒类型,如会议主题、待办事项等)",
|
- title: 简洁的事件标题,去除"提醒"、"帮我"等前缀
|
||||||
"date": "2026-02-13T15:00:00Z",
|
- content: 仅提醒类型需要,事件的详细内容
|
||||||
"timezone": "Asia/Shanghai",
|
- date: ISO 8601 格式的日期时间
|
||||||
"is_lunar": true 或 false,
|
- timezone: "Asia/Shanghai"
|
||||||
"repeat_type": "daily, weekly, monthly, yearly 或 none",
|
- is_lunar: 是否农历日期
|
||||||
"priority": "none, red, green 或 yellow(仅提醒,red=重要紧急,green=绿色,yellow=黄色)",
|
- repeat_type: "daily"(每天), "weekly"(每周), "monthly"(每月), "yearly"(每年), "none"(不重复)
|
||||||
"reminder_times": ["提前提醒时间点数组,如 2026-02-12T09:00:00Z"](仅提醒)
|
- priority: "none", "red", "green", "yellow"
|
||||||
},
|
- reminder_times: 提前提醒时间数组
|
||||||
"response": "友好的确认消息"
|
|
||||||
}
|
标题提取示例:
|
||||||
|
- "提醒我明天上午9点开会" → title = "开会"
|
||||||
|
- "提醒我春节回家" → title = "春节回家"
|
||||||
|
- "帮我记一下妈妈的生日" → title = "妈妈的生日"
|
||||||
|
|
||||||
## 意图识别规则(非常重要):
|
## 意图识别规则(非常重要):
|
||||||
|
|
||||||
@ -200,14 +203,19 @@ async function callDeepSeek(message: string): Promise<{
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
const content = data.choices[0]?.message?.content || '';
|
const content = data.choices[0]?.message?.content || '';
|
||||||
|
|
||||||
|
// Log the raw response for debugging
|
||||||
|
console.log('[AI] Raw response:', content);
|
||||||
|
|
||||||
// Extract JSON from response
|
// Extract JSON from response
|
||||||
const jsonMatch = content.match(/\{[\s\S]*\}/);
|
const jsonMatch = content.match(/\{[\s\S]*\}/);
|
||||||
if (jsonMatch) {
|
if (jsonMatch) {
|
||||||
try {
|
try {
|
||||||
const rawParsed = JSON.parse(jsonMatch[0]);
|
const rawParsed = JSON.parse(jsonMatch[0]);
|
||||||
|
console.log('[AI] Parsed JSON:', rawParsed);
|
||||||
const parsed = rawParsed.parsed || rawParsed;
|
const parsed = rawParsed.parsed || rawParsed;
|
||||||
// Validate parsed data with Zod schema
|
// Validate parsed data with Zod schema
|
||||||
const validated = parsedEventSchema.parse(parsed);
|
const validated = parsedEventSchema.parse(parsed);
|
||||||
|
console.log('[AI] Validated:', validated);
|
||||||
// 构建友好的用户消息
|
// 构建友好的用户消息
|
||||||
const typeText = validated.type === 'anniversary' ? '纪念日' : '提醒';
|
const typeText = validated.type === 'anniversary' ? '纪念日' : '提醒';
|
||||||
const dateObj = new Date(validated.date);
|
const dateObj = new Date(validated.date);
|
||||||
@ -223,11 +231,13 @@ async function callDeepSeek(message: string): Promise<{
|
|||||||
response: `已为你创建${typeText}「${validated.title}」,日期:${dateText}${repeatText}`,
|
response: `已为你创建${typeText}「${validated.title}」,日期:${dateText}${repeatText}`,
|
||||||
};
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error('[AI] Parse error:', e);
|
||||||
// Schema validation failed, use mock response
|
// Schema validation failed, use mock response
|
||||||
return mockParseResponse(message);
|
return mockParseResponse(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.warn('[AI] No JSON found in response, using mock');
|
||||||
return mockParseResponse(message);
|
return mockParseResponse(message);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('DeepSeek API error:', error);
|
console.error('DeepSeek API error:', error);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user