harvesthealth commited on
Commit
97308d3
·
verified ·
1 Parent(s): 84b6201

Fix: Use custom fetch to ensure correct OpenAI format

Browse files
Files changed (1) hide show
  1. app/api/text-generation/route.ts +21 -25
app/api/text-generation/route.ts CHANGED
@@ -1,33 +1,29 @@
1
- import { OpenAIStream, StreamingTextResponse } from 'ai';
 
 
 
 
 
 
 
 
 
 
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 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
- }
 
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
+ }