-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroute.ts
40 lines (34 loc) · 1.23 KB
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Configuration, OpenAIApi } from 'openai-edge';
import { OpenAIStream, StreamingTextResponse } from 'ai';
// Create an OpenAI API client (that's edge friendly!)
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);
// Set the runtime to edge for best performance
export const runtime = 'edge';
export async function POST(req: Request) {
const { vibe, bio } = await req.json();
// Ask OpenAI for a streaming completion given the prompt
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
stream: true,
messages: [
{
role: 'user',
content: `Imitating the voice of ${vibe}, generate one recipe with only these ingredients: ${bio}, giving step by step directions. ${
vibe === 'Gordon Ramsay'
? "Give instructions in the voice of Gordon Ramsay."
: null
}
Make sure to use only the following ingredients and no others: ${bio}${
bio.slice(-1) === '.' ? '' : '.'
}`,
},
],
});
// Convert the response into a friendly text-stream
const stream = OpenAIStream(response);
// Respond with the stream
return new StreamingTextResponse(stream);
}