Spaces:
Runtime error
Runtime error
Download apps/api/src/services/chat/chat.controller.ts from ClaraSu/llm-router: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/ClaraSu/llm-router/resolve/main/apps/api/src/services/chat/chat.controller.ts
- Command line
-
hf download hf://spaces/ClaraSu/llm-router/apps/api/src/services/chat/chat.controller.ts
-
curl -L -o chat.controller.ts https://huggingface.co/spaces/ClaraSu/llm-router/resolve/main/apps/api/src/services/chat/chat.controller.ts
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.' }) | |
| } | |
| } | |