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:
2026-07-26 10:49:44 +08:00
co-authored by Claude
parent 7bf3b6a409
commit ed2a2f65e4
4 changed files with 104 additions and 165 deletions
@@ -52,48 +52,35 @@ export async function POST(
res = await client.chat.completions.create({
model: 'deepseek-v4-pro',
max_tokens: 1024,
tools: [{
type: 'function',
function: {
name: 'suggest_triage',
description: 'Suggest severity rating and NADOPOD 2004 DOSH classification for a warehouse incident',
parameters: {
type: 'object',
properties: {
severity: { type: 'number', description: '1=minor, 2=low, 3=moderate, 4=serious, 5=critical/fatality' },
is_fatality: { type: 'boolean' },
is_serious_bodily_injury: { type: 'boolean', description: 'Fracture, amputation, blindness, serious burn, or similar' },
is_dangerous_occurrence: { type: 'boolean', description: 'Structural collapse, explosion, fire, scaffold collapse, etc.' },
is_occupational_disease: { type: 'boolean', description: 'Disease arising from workplace exposure' },
rationale: { type: 'string', description: 'One-sentence rationale citing NADOPOD 2004 where applicable' },
},
required: [
'severity', 'is_fatality', 'is_serious_bodily_injury',
'is_dangerous_occurrence', 'is_occupational_disease', 'rationale',
],
},
messages: [
{
role: 'system',
content: `You are an HSE triage specialist for a Malaysian 3PL warehouse. Assess incidents under NADOPOD 2004. You must respond with valid JSON only — no markdown, no explanation outside the JSON object.
Output exactly this JSON structure:
{
"severity": <number 1-5, where 1=minor, 2=low, 3=moderate, 4=serious, 5=critical/fatality>,
"is_fatality": <boolean>,
"is_serious_bodily_injury": <boolean, true for fracture, amputation, blindness, serious burn, or similar>,
"is_dangerous_occurrence": <boolean, true for structural collapse, explosion, fire, scaffold collapse, etc.>,
"is_occupational_disease": <boolean, true for disease arising from workplace exposure>,
"rationale": "<one-sentence rationale citing NADOPOD 2004 where applicable>"
}`,
},
}],
tool_choice: { type: 'function', function: { name: 'suggest_triage' } },
messages: [{
role: 'user',
content: `You are an HSE triage specialist for a Malaysian 3PL warehouse. Assess this incident under NADOPOD 2004.
Incident type: ${incident.incidentType}
Description: ${incident.description}
Injury involved: ${incident.injuryInvolved ? 'yes' : 'no'}
Medical status: ${incident.medicalStatus ?? 'N/A'}
Asset/equipment involved: ${incident.assetInvolved ? 'yes' : 'no'}
Suggest severity (15) and tick the appropriate NADOPOD 2004 flags. Give a one-sentence rationale.`,
}],
{
role: 'user',
content: `Assess this incident under NADOPOD 2004. Respond with JSON only.\n\nIncident type: ${incident.incidentType}\nDescription: ${incident.description}\nInjury involved: ${incident.injuryInvolved ? 'yes' : 'no'}\nMedical status: ${incident.medicalStatus ?? 'N/A'}\nAsset/equipment involved: ${incident.assetInvolved ? 'yes' : 'no'}`,
},
],
})
} 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 suggestion 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 input: {
severity?: unknown
@@ -103,7 +90,7 @@ Suggest severity (15) and tick the appropriate NADOPOD 2004 flags. Give a one
is_occupational_disease?: unknown
rationale?: unknown
}
try { input = JSON.parse(call.function.arguments) }
try { input = JSON.parse(json) }
catch { return NextResponse.json({ error: 'AI returned unexpected structure' }, { status: 500 }) }
if (