This commit is contained in:
Jared Palmer 2023-06-02 09:52:51 -04:00
parent 916a9a9659
commit c555ecd259
6 changed files with 144 additions and 138 deletions

23
app/api/chat/route.ts Normal file
View file

@ -0,0 +1,23 @@
import { auth } from "@/auth";
import { OpenAIStream, StreamingTextResponse } from "ai-connector";
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",
messages: json.messages.map((m) => ({ content: m.content, role: m.role })),
temperature: 0.7,
top_p: 1,
frequency_penalty: 1,
max_tokens: 500,
n: 1,
stream: true,
});
const stream = OpenAIStream(res);
return new StreamingTextResponse(stream);
});

View file

@ -1,45 +0,0 @@
import { auth } from "@/auth";
import { OpenAIStream, openai } from "@/lib/openai";
import { prisma } from "@/lib/prisma";
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",
messages: json.messages,
temperature: 0.7,
top_p: 1,
frequency_penalty: 1,
max_tokens: 500,
n: 1,
stream: true,
});
const stream = await OpenAIStream(res);
let fullResponse = "";
const decoder = new TextDecoder();
const saveToPrisma = new TransformStream({
transform: async (chunk, controller) => {
controller.enqueue(chunk);
fullResponse += decoder.decode(chunk);
},
flush: async () => {
await prisma.chat.upsert({
where: {
id: json.id,
},
create: json,
update: json,
});
},
});
return new Response(stream.pipeThrough(saveToPrisma), {
status: 200,
headers: { "Content-Type": "text/event-stream" },
});
});