ChatGPT-Next-Web/app/api/chat/route.ts

32 lines
708 B
TypeScript
Raw Normal View History

2023-03-09 17:01:40 +00:00
import { OpenAIApi, Configuration } from "openai";
import { apiKey } from "./config";
2023-03-10 18:25:33 +00:00
import { ChatRequest } from "./typing";
2023-03-09 17:01:40 +00:00
// set up openai api client
const config = new Configuration({
apiKey,
});
const openai = new OpenAIApi(config);
2023-03-10 18:25:33 +00:00
export async function POST(req: Request) {
2023-03-09 17:01:40 +00:00
try {
2023-03-10 18:25:33 +00:00
const requestBody = (await req.json()) as ChatRequest;
2023-03-09 17:01:40 +00:00
const completion = await openai.createChatCompletion(
{
2023-03-10 18:25:33 +00:00
...requestBody,
2023-03-09 17:01:40 +00:00
},
{
proxy: {
protocol: "socks",
host: "127.0.0.1",
port: 7890,
},
}
);
return new Response(JSON.stringify(completion.data));
} catch (e) {
return new Response(JSON.stringify(e));
}
}