fix: switch AI endpoints from tool-calling to JSON output mode
DeepSeek v4-pro reasoning model rejects tool_choice parameter. All 4 endpoints now use system prompts with JSON schema instructions and parse content as JSON instead of tool_calls. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -61,67 +61,39 @@ export async function POST(
|
||||
res = await client.chat.completions.create({
|
||||
model: 'deepseek-v4-pro',
|
||||
max_tokens: 2048,
|
||||
tools: [{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'draft_rca',
|
||||
description: 'Draft a 5-Why root cause analysis and CAPA suggestions for an HSE incident',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
five_why_steps: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
why: { type: 'string', description: 'The why question' },
|
||||
answer: { type: 'string', description: 'The finding or answer' },
|
||||
},
|
||||
required: ['why', 'answer'],
|
||||
},
|
||||
description: '3 to 5 why steps',
|
||||
},
|
||||
root_cause_summary: {
|
||||
type: 'string',
|
||||
description: 'One-sentence root cause statement',
|
||||
},
|
||||
capa_suggestions: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
description: 'Up to 3 corrective/preventive action suggestions',
|
||||
},
|
||||
},
|
||||
required: ['five_why_steps', 'root_cause_summary', 'capa_suggestions'],
|
||||
},
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: `You are an experienced HSE investigator for a Malaysian 3PL warehouse. Draft a 5-Why root cause analysis for incidents. You must respond with valid JSON only — no markdown, no explanation outside the JSON object.
|
||||
|
||||
Output exactly this JSON structure:
|
||||
{
|
||||
"five_why_steps": [
|
||||
{ "why": "<the why question>", "answer": "<the finding or answer>" }
|
||||
],
|
||||
"root_cause_summary": "<one-sentence root cause statement>",
|
||||
"capa_suggestions": ["<up to 3 corrective/preventive actions>"]
|
||||
}
|
||||
|
||||
Provide 3–5 Why steps drilling from immediate cause to root cause. Frame CAPA suggestions appropriate for a Malaysian warehouse context.`,
|
||||
},
|
||||
}],
|
||||
tool_choice: { type: 'function', function: { name: 'draft_rca' } },
|
||||
messages: [{
|
||||
role: 'user',
|
||||
content: `You are an experienced HSE investigator for a Malaysian 3PL warehouse. Draft a 5-Why root cause analysis for this incident.
|
||||
|
||||
Site: ${incident.siteName ?? 'Unknown'}
|
||||
Zone: ${incident.zoneName ?? 'Unknown'}
|
||||
Incident type: ${incident.incidentType}
|
||||
Description: ${incident.description}
|
||||
Severity: ${incident.severity ?? 'not yet assigned'}/5
|
||||
Injury involved: ${incident.injuryInvolved ? `yes — ${incident.medicalStatus}` : 'no'}
|
||||
Fatality: ${incident.isFatality ? 'yes' : 'no'}
|
||||
Serious bodily injury: ${incident.isSeriousBodilyInjury ? 'yes' : 'no'}
|
||||
Triage notes: ${incident.triageNotes ?? 'none'}
|
||||
|
||||
Provide 3–5 Why steps drilling from immediate cause to root cause. Give a one-sentence root cause statement. Suggest 3 corrective/preventive actions appropriate for a Malaysian warehouse context.`,
|
||||
}],
|
||||
{
|
||||
role: 'user',
|
||||
content: `Draft a 5-Why root cause analysis for this incident. Respond with JSON only.\n\nSite: ${incident.siteName ?? 'Unknown'}\nZone: ${incident.zoneName ?? 'Unknown'}\nIncident type: ${incident.incidentType}\nDescription: ${incident.description}\nSeverity: ${incident.severity ?? 'not yet assigned'}/5\nInjury involved: ${incident.injuryInvolved ? `yes — ${incident.medicalStatus}` : 'no'}\nFatality: ${incident.isFatality ? 'yes' : 'no'}\nSerious bodily injury: ${incident.isSeriousBodilyInjury ? 'yes' : 'no'}\nTriage notes: ${incident.triageNotes ?? 'none'}`,
|
||||
},
|
||||
],
|
||||
})
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'AI service unavailable' }, { status: 503 })
|
||||
}
|
||||
|
||||
const call = res.choices[0]?.message?.tool_calls?.[0]
|
||||
if (!call || call.type !== 'function') return NextResponse.json({ error: 'AI draft failed' }, { status: 500 })
|
||||
const raw = res.choices[0]?.message?.content
|
||||
if (!raw) return NextResponse.json({ error: 'AI returned empty response' }, { status: 500 })
|
||||
|
||||
const json = raw.replace(/^```(?:json)?\s*/i, '').replace(/\s*```$/i, '').trim()
|
||||
|
||||
let draft: { five_why_steps?: unknown; root_cause_summary?: unknown; capa_suggestions?: unknown }
|
||||
try { draft = JSON.parse(call.function.arguments) }
|
||||
try { draft = JSON.parse(json) }
|
||||
catch { return NextResponse.json({ error: 'AI returned unexpected structure' }, { status: 500 }) }
|
||||
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user