File size: 1,454 Bytes
e3b7b69
e481638
e3b7b69
5942971
e3b7b69
 
 
 
 
 
5942971
e3b7b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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.' })
  }
}