chatbot-template/app/api/chat/route.ts

27 lines
688 B
TypeScript
Raw Normal View History

2023-06-02 09:52:51 -04:00
import { auth } from "@/auth";
2023-06-02 10:05:21 -04:00
import { Message, OpenAIStream, StreamingTextResponse } from "ai-connector";
2023-06-02 09:52:51 -04:00
import { openai } from "@/lib/openai";
export const POST = auth(async function POST(req: Request) {
const json = await req.json();
// @ts-ignore
console.log(req.auth); // todo fix types
const res = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
2023-06-02 10:05:21 -04:00
messages: json.messages.map((m: Message) => ({
content: m.content,
role: m.role,
})),
2023-06-02 09:52:51 -04:00
temperature: 0.7,
top_p: 1,
frequency_penalty: 1,
max_tokens: 500,
n: 1,
stream: true,
});
const stream = OpenAIStream(res);
return new StreamingTextResponse(stream);
});