Spaces:
Build error
Build error
Fix: Use custom fetch to ensure correct OpenAI format
Browse files- app/api/text-generation/route.ts +21 -25
app/api/text-generation/route.ts
CHANGED
|
@@ -1,33 +1,29 @@
|
|
| 1 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
// IMPORTANT! Set the runtime to edge
|
| 4 |
export const runtime = 'edge';
|
| 5 |
|
| 6 |
export async function POST(req: Request) {
|
| 7 |
const { prompt, model } = await req.json();
|
| 8 |
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
messages: [{ role: 'user', content: prompt }],
|
| 18 |
-
stream: true,
|
| 19 |
-
}),
|
| 20 |
});
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
}
|
|
|
|
| 1 |
+
import { streamText } from 'ai';
|
| 2 |
+
import { createOpenAI } from '@ai-sdk/openai';
|
| 3 |
+
|
| 4 |
+
const customOpenAI = createOpenAI({
|
| 5 |
+
apiKey: process.env.BLABLADOR_API_KEY || '',
|
| 6 |
+
baseURL: 'https://api.openai.com/v1', // Trick the SDK to use the OpenAI format
|
| 7 |
+
fetch: async (url, options) => {
|
| 8 |
+
const newUrl = url.toString().replace('https://api.openai.com/v1', 'https://api.helmholtz-blablador.fz-juelich.de/v1');
|
| 9 |
+
return fetch(newUrl, options);
|
| 10 |
+
},
|
| 11 |
+
});
|
| 12 |
|
|
|
|
| 13 |
export const runtime = 'edge';
|
| 14 |
|
| 15 |
export async function POST(req: Request) {
|
| 16 |
const { prompt, model } = await req.json();
|
| 17 |
|
| 18 |
+
const result = await streamText({
|
| 19 |
+
model: customOpenAI(model || 'alias-code'),
|
| 20 |
+
messages: [
|
| 21 |
+
{
|
| 22 |
+
role: 'user',
|
| 23 |
+
content: prompt,
|
| 24 |
+
},
|
| 25 |
+
],
|
|
|
|
|
|
|
|
|
|
| 26 |
});
|
| 27 |
|
| 28 |
+
return result.toTextStreamResponse();
|
| 29 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|