llm-router / apps /api /src /services /chat /chat.controller.ts
ClaraSu's picture
Upload folder using huggingface_hub
e481638 verified
Raw History Blame Contribute Delete
1.45 kB
import type { Request, Response } from 'express'
import { route } from '../../llm-router'
import type { ChatInput } from './chat.validator'
import type { SimpleMessage } from '@llm-router/llm-router'
export async function chatComplete(req: Request, res: Response): Promise<void> {
try {
const { messages, model, stream } = req.body as ChatInput
const result = await route(messages as SimpleMessage[], model)
const id = `chatcmpl-${Date.now()}`
console.log(`[routed] → ${result.usedModel.toUpperCase()} (${result.model}) | PII: ${result.piiDetected}`)
if (stream) {
res.setHeader('Content-Type', 'text/event-stream')
res.setHeader('Cache-Control', 'no-cache')
res.setHeader('Connection', 'keep-alive')
const chunk = {
id, object: 'chat.completion.chunk', model,
choices: [{ index: 0, delta: { role: 'assistant', content: result.response }, finish_reason: 'stop' }],
}
res.write(`data: ${JSON.stringify(chunk)}\n\n`)
res.write('data: [DONE]\n\n')
res.end()
return
}
res.json({
id, object: 'chat.completion', model,
choices: [{ index: 0, message: { role: 'assistant', content: result.response }, finish_reason: 'stop' }],
})
} catch (err: unknown) {
console.error('[proxy error]', JSON.stringify(err, Object.getOwnPropertyNames(err as object)))
res.status(500).json({ error: 'Something went wrong! Please try again.' })
}
}