File size: 880 Bytes
c2df9c2
 
 
f4e05b5
c2df9c2
f4e05b5
 
c2df9c2
 
 
 
 
 
 
f4e05b5
 
c2df9c2
 
 
 
 
 
d446a2d
c2df9c2
 
 
 
 
 
 
 
 
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
import { Configuration, OpenAIApi } from "openai-edge";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { createUrlSurfer } from "@/app/tools/surfer";
import { createSerpApi } from "@/app/tools/serp-api";

const [, urlSurferSchema] = createUrlSurfer();
const [, serpApiSchema] = createSerpApi({ apiKey: process.env.SERP_API_KEY || '' });

const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);

const functions: any[] = [
  urlSurferSchema,
  serpApiSchema
];

export async function POST(req: Request) {
  const { messages, function_call } = await req.json()

  const response = await openai.createChatCompletion({
    model: 'gpt-4-1106-preview',
    stream: true,
    messages,
    functions,
    function_call
  })

  const stream = OpenAIStream(response)
  return new StreamingTextResponse(stream)
}