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
+24 -34
View File
@@ -44,47 +44,38 @@ export async function POST(request: NextRequest) {
res = await client.chat.completions.create({
model: 'deepseek-v4-pro',
max_tokens: 1024,
tools: [{
type: 'function',
function: {
name: 'assess_quality',
description: 'Assess HSE incident report description quality',
parameters: {
type: 'object',
properties: {
score: { type: 'number', description: '1-10 quality score' },
passes: { type: 'boolean', description: 'True when score is 6 or above' },
feedback: { type: 'string', description: 'One-sentence quality summary' },
suggestions: {
type: 'array',
items: { type: 'string' },
description: 'Up to 3 concrete suggestions to improve the description',
},
},
required: ['score', 'passes', 'feedback', 'suggestions'],
},
messages: [
{
role: 'system',
content: `You are an HSE reporting assistant for a Malaysian 3PL warehouse. You must respond with valid JSON only — no markdown, no explanation outside the JSON object.
Output exactly this JSON structure:
{
"score": <number 1-10>,
"passes": <boolean, true when score >= 6>,
"feedback": "<one-sentence quality summary>",
"suggestions": ["<up to 3 concrete suggestions, empty array if score >= 6>"]
}
Scoring criteria: specificity (location, time, persons involved), completeness (what happened + immediate actions), clarity. Score 6 or above passes.`,
},
}],
tool_choice: { type: 'function', function: { name: 'assess_quality' } },
messages: [{
role: 'user',
content: `You are an HSE reporting assistant for a Malaysian 3PL warehouse. Assess this incident report description.
Incident type: ${body.incident_type}
Description: ${body.description}
Score 110 based on: specificity (location, time, persons involved), completeness (what happened + immediate actions), and clarity. Score 6 or above passes. If score is below 6, give up to 3 actionable suggestions.`,
}],
{
role: 'user',
content: `Assess this incident report description. Respond with JSON only.\n\nIncident type: ${body.incident_type}\nDescription: ${body.description}`,
},
],
})
} 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 assessment 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: { score?: unknown; passes?: unknown; feedback?: unknown; suggestions?: 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 (
@@ -96,7 +87,6 @@ Score 110 based on: specificity (location, time, persons involved), completen
return NextResponse.json({ error: 'AI returned unexpected structure' }, { status: 500 })
}
// audit write uses session.sub as a pseudo record-id (no incident_id at this stage)
await withUser(session.sub, async tx => {
await writeAuditLog(tx, 'incidents', session.sub, 'ai_quality_check', {
score: input.score, passes: input.passes, model: 'deepseek-v4-pro',