Spaces:
Build error
Build error
Fix: Build errors and text generation API
#3
by
harvesthealth
- opened
- app/api/text-generation/route.ts +24 -19
app/api/text-generation/route.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
| 1 |
-
import {
|
| 2 |
-
import { createOpenAI } from '@ai-sdk/openai';
|
| 3 |
-
|
| 4 |
-
// Create an OpenAI API client (that's edge friendly!)
|
| 5 |
-
const customOpenAI = createOpenAI({
|
| 6 |
-
apiKey: process.env.BLABLADOR_API_KEY || '',
|
| 7 |
-
baseURL: 'https://api.helmholtz-blablador.fz-juelich.de/v1',
|
| 8 |
-
compatibility: 'strict',
|
| 9 |
-
});
|
| 10 |
|
| 11 |
// IMPORTANT! Set the runtime to edge
|
| 12 |
export const runtime = 'edge';
|
|
@@ -14,15 +6,28 @@ export const runtime = 'edge';
|
|
| 14 |
export async function POST(req: Request) {
|
| 15 |
const { prompt, model } = await req.json();
|
| 16 |
|
| 17 |
-
const
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
});
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { OpenAIStream, StreamingTextResponse } from 'ai';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
// IMPORTANT! Set the runtime to edge
|
| 4 |
export const runtime = 'edge';
|
|
|
|
| 6 |
export async function POST(req: Request) {
|
| 7 |
const { prompt, model } = await req.json();
|
| 8 |
|
| 9 |
+
const response = await fetch('https://api.helmholtz-blablador.fz-juelich.de/v1/chat/completions', {
|
| 10 |
+
method: 'POST',
|
| 11 |
+
headers: {
|
| 12 |
+
'Content-Type': 'application/json',
|
| 13 |
+
'Authorization': `Bearer ${process.env.BLABLADOR_API_KEY || ''}`,
|
| 14 |
+
},
|
| 15 |
+
body: JSON.stringify({
|
| 16 |
+
model: model || 'alias-code',
|
| 17 |
+
messages: [{ role: 'user', content: prompt }],
|
| 18 |
+
stream: true,
|
| 19 |
+
}),
|
| 20 |
});
|
| 21 |
|
| 22 |
+
// Check for errors
|
| 23 |
+
if (!response.ok) {
|
| 24 |
+
return new Response(await response.text(), {
|
| 25 |
+
status: response.status,
|
| 26 |
+
statusText: response.statusText,
|
| 27 |
+
});
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Convert the response into a friendly text-stream
|
| 31 |
+
const stream = OpenAIStream(response);
|
| 32 |
+
return new StreamingTextResponse(stream);
|
| 33 |
+
}
|