feat(ai): switch from DeepSeek to local Ollama (qwen3:14b)

- Point AI client to localhost:11434/v1 (Ollama)
- Remove DeepSeek API key dependency
- Update model name to qwen3:14b across all routes
- Update test mocks to use createLocalLLMClient
- Removes need for DEEPSEEK_API_KEY setting

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 11:34:05 +08:00
co-authored by Claude Haiku 4.5
parent 69e510af74
commit 4f75dd5887
8 changed files with 24 additions and 40 deletions
+4 -6
View File
@@ -6,8 +6,7 @@ import { withUser, asAdmin } from '@/lib/db/with-user'
import { writeAuditLog } from '@/lib/db/audit'
import { incidents, sites, zones, auditLog } from '@/lib/db/schema'
import { eq, and, gte, desc } from 'drizzle-orm'
import { createDeepSeekClient } from '@/lib/claude/client'
import { getApiKey } from '@/lib/settings'
import { createLocalLLMClient } from '@/lib/claude/client'
type ZoneAggregate = {
zone: string
@@ -103,13 +102,12 @@ export async function POST() {
return NextResponse.json({ error: 'Rate limit: wait 60 seconds between AI requests' }, { status: 429 })
}
const deepseekKey = await getApiKey('DEEPSEEK_API_KEY')
const client = createDeepSeekClient(deepseekKey)
const client = createLocalLLMClient()
let res: Awaited<ReturnType<typeof client.chat.completions.create>>
try {
res = await client.chat.completions.create({
model: 'deepseek-v4-pro',
model: 'qwen3:14b',
max_tokens: 2048,
messages: [
{
@@ -168,7 +166,7 @@ Flag only zones with rising or elevated risk (at most 5 flags; do not flag healt
await withUser(session.sub, async tx =>
writeAuditLog(tx, 'incidents', session.sub, 'ai_risk_flags',
{ flags, summary: input.summary, model: 'deepseek-v4-pro' })
{ flags, summary: input.summary, model: 'qwen3:14b' })
)
return NextResponse.json({ flags, summary: input.summary })
+4 -6
View File
@@ -6,8 +6,7 @@ import { withUser, asAdmin } from '@/lib/db/with-user'
import { writeAuditLog } from '@/lib/db/audit'
import { incidents, auditLog, sites, zones } from '@/lib/db/schema'
import { eq, and, gte, sql } from 'drizzle-orm'
import { createDeepSeekClient } from '@/lib/claude/client'
import { getApiKey } from '@/lib/settings'
import { createLocalLLMClient } from '@/lib/claude/client'
export async function POST(
_request: NextRequest,
@@ -32,8 +31,7 @@ export async function POST(
if (Number(rateRow?.cnt ?? 0) > 0)
return NextResponse.json({ error: 'Rate limited — please wait 60 seconds' }, { status: 429 })
const deepseekKey = await getApiKey('DEEPSEEK_API_KEY')
const client = createDeepSeekClient(deepseekKey)
const client = createLocalLLMClient()
const [incident] = await withUser(session.sub, async tx =>
tx.select({
@@ -59,7 +57,7 @@ export async function POST(
let res: Awaited<ReturnType<typeof client.chat.completions.create>>
try {
res = await client.chat.completions.create({
model: 'deepseek-v4-pro',
model: 'qwen3:14b',
max_tokens: 2048,
messages: [
{
@@ -107,7 +105,7 @@ Provide 3–5 Why steps drilling from immediate cause to root cause. Frame CAPA
await withUser(session.sub, async tx => {
await writeAuditLog(tx, 'incidents', id, 'ai_rca_draft', {
root_cause_summary: draft.root_cause_summary as string,
model: 'deepseek-v4-pro',
model: 'qwen3:14b',
})
})
@@ -6,8 +6,7 @@ import { withUser, asAdmin } from '@/lib/db/with-user'
import { writeAuditLog } from '@/lib/db/audit'
import { incidents, auditLog } from '@/lib/db/schema'
import { eq, and, gte, sql } from 'drizzle-orm'
import { createDeepSeekClient } from '@/lib/claude/client'
import { getApiKey } from '@/lib/settings'
import { createLocalLLMClient } from '@/lib/claude/client'
export async function POST(
_request: NextRequest,
@@ -32,8 +31,7 @@ export async function POST(
if (Number(rateRow?.cnt ?? 0) > 0)
return NextResponse.json({ error: 'Rate limited — please wait 60 seconds' }, { status: 429 })
const deepseekKey = await getApiKey('DEEPSEEK_API_KEY')
const client = createDeepSeekClient(deepseekKey)
const client = createLocalLLMClient()
const [incident] = await withUser(session.sub, async tx =>
tx.select({
@@ -50,7 +48,7 @@ export async function POST(
let res: Awaited<ReturnType<typeof client.chat.completions.create>>
try {
res = await client.chat.completions.create({
model: 'deepseek-v4-pro',
model: 'qwen3:14b',
max_tokens: 1024,
messages: [
{
@@ -105,7 +103,7 @@ Output exactly this JSON structure:
}
await withUser(session.sub, async tx => {
await writeAuditLog(tx, 'incidents', id, 'ai_triage_suggest', { suggestion: input, model: 'deepseek-v4-pro' })
await writeAuditLog(tx, 'incidents', id, 'ai_triage_suggest', { suggestion: input, model: 'qwen3:14b' })
})
return NextResponse.json(input)
+4 -6
View File
@@ -6,8 +6,7 @@ import { withUser, asAdmin } from '@/lib/db/with-user'
import { writeAuditLog } from '@/lib/db/audit'
import { auditLog } from '@/lib/db/schema'
import { eq, and, gte, sql } from 'drizzle-orm'
import { createDeepSeekClient } from '@/lib/claude/client'
import { getApiKey } from '@/lib/settings'
import { createLocalLLMClient } from '@/lib/claude/client'
export async function POST(request: NextRequest) {
const session = await getSession()
@@ -26,8 +25,7 @@ export async function POST(request: NextRequest) {
if (Number(rateRow?.cnt ?? 0) > 0)
return NextResponse.json({ error: 'Rate limited — please wait 60 seconds' }, { status: 429 })
const deepseekKey = await getApiKey('DEEPSEEK_API_KEY')
const client = createDeepSeekClient(deepseekKey)
const client = createLocalLLMClient()
let body: { description?: string; incident_type?: string }
try {
@@ -42,7 +40,7 @@ export async function POST(request: NextRequest) {
let res: Awaited<ReturnType<typeof client.chat.completions.create>>
try {
res = await client.chat.completions.create({
model: 'deepseek-v4-pro',
model: 'qwen3:14b',
max_tokens: 1024,
messages: [
{
@@ -89,7 +87,7 @@ Scoring criteria: specificity (location, time, persons involved), completeness (
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',
score: input.score, passes: input.passes, model: 'qwen3:14b',
})
})
+4
View File
@@ -3,3 +3,7 @@ import OpenAI from 'openai'
export function createDeepSeekClient(apiKey: string): OpenAI {
return new OpenAI({ apiKey, baseURL: 'https://api.deepseek.com' })
}
export function createLocalLLMClient(): OpenAI {
return new OpenAI({ apiKey: 'local', baseURL: 'http://localhost:11434/v1' })
}
+2 -6
View File
@@ -17,7 +17,7 @@ vi.mock('@/lib/supabase/server', () => ({
}))
vi.mock('@/lib/claude/client', () => ({
createDeepSeekClient: vi.fn().mockReturnValue({
createLocalLLMClient: vi.fn().mockReturnValue({
chat: {
completions: {
create: vi.fn().mockResolvedValue({
@@ -38,10 +38,6 @@ vi.mock('@/lib/claude/client', () => ({
}),
}))
vi.mock('@/lib/settings', () => ({
getApiKey: vi.fn().mockResolvedValue('sk-test-key'),
}))
describe('POST /api/incidents/ai/quality-check', () => {
it('returns 422 when description is missing', async () => {
const { POST } = await import('@/app/api/incidents/ai/quality-check/route')
@@ -54,7 +50,7 @@ describe('POST /api/incidents/ai/quality-check', () => {
expect(res.status).toBe(422)
})
it('returns quality assessment from DeepSeek', async () => {
it('returns quality assessment from local LLM', async () => {
const { POST } = await import('@/app/api/incidents/ai/quality-check/route')
const req = new Request('http://localhost/api/incidents/ai/quality-check', {
method: 'POST',
+1 -5
View File
@@ -46,7 +46,7 @@ vi.mock('@/lib/supabase/server', () => ({
}))
vi.mock('@/lib/claude/client', () => ({
createDeepSeekClient: vi.fn().mockReturnValue({
createLocalLLMClient: vi.fn().mockReturnValue({
chat: {
completions: {
create: vi.fn().mockResolvedValue({
@@ -67,10 +67,6 @@ vi.mock('@/lib/claude/client', () => ({
}),
}))
vi.mock('@/lib/settings', () => ({
getApiKey: vi.fn().mockResolvedValue('sk-test-key'),
}))
describe('POST /api/incidents/[id]/ai/rca-draft', () => {
it('returns five_why_steps, root_cause_summary, and capa_suggestions', async () => {
const { POST } = await import('@/app/api/incidents/[id]/ai/rca-draft/route')
+1 -5
View File
@@ -36,7 +36,7 @@ vi.mock('@/lib/supabase/server', () => ({
}))
vi.mock('@/lib/claude/client', () => ({
createDeepSeekClient: vi.fn().mockReturnValue({
createLocalLLMClient: vi.fn().mockReturnValue({
chat: {
completions: {
create: vi.fn().mockResolvedValue({
@@ -57,10 +57,6 @@ vi.mock('@/lib/claude/client', () => ({
}),
}))
vi.mock('@/lib/settings', () => ({
getApiKey: vi.fn().mockResolvedValue('sk-test-key'),
}))
describe('POST /api/incidents/[id]/ai/triage-suggest', () => {
it('returns triage suggestion with severity and DOSH flags', async () => {
const { POST } = await import('@/app/api/incidents/[id]/ai/triage-suggest/route')