Spaces:
Sleeping
Sleeping
| import { OpenAIStream, StreamingTextResponse } from 'ai'; | |
| // IMPORTANT! Set the runtime to edge | |
| export const runtime = 'edge'; | |
| export async function POST(req: Request) { | |
| const { prompt, model } = await req.json(); | |
| const response = await fetch('https://api.helmholtz-blablador.fz-juelich.de/v1/chat/completions', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': `Bearer ${process.env.BLABLADOR_API_KEY || ''}`, | |
| }, | |
| body: JSON.stringify({ | |
| model: model || 'alias-code', | |
| messages: [{ role: 'user', content: prompt }], | |
| stream: true, | |
| }), | |
| }); | |
| // Check for errors | |
| if (!response.ok) { | |
| return new Response(await response.text(), { | |
| status: response.status, | |
| statusText: response.statusText, | |
| }); | |
| } | |
| // Convert the response into a friendly text-stream | |
| const stream = OpenAIStream(response); | |
| return new StreamingTextResponse(stream); | |
| } |