Try to hack auth
This commit is contained in:
parent
d88eae7230
commit
030b23985d
19 changed files with 330 additions and 673 deletions
43
app/api/chat/route.ts
Normal file
43
app/api/chat/route.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { auth } from "@/auth";
|
||||
import { chats, db } from "@/lib/db/schema";
|
||||
import { openai } from "@/lib/openai";
|
||||
import { OpenAIStream, StreamingTextResponse } from "ai-connector";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
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: any) => ({
|
||||
content: m.content,
|
||||
role: m.role,
|
||||
})),
|
||||
temperature: 0.7,
|
||||
top_p: 1,
|
||||
frequency_penalty: 1,
|
||||
max_tokens: 500,
|
||||
n: 1,
|
||||
stream: true,
|
||||
});
|
||||
|
||||
const stream = await OpenAIStream(res, {
|
||||
async onCompletion(completion) {
|
||||
// @ts-ignore
|
||||
if (req.auth?.user?.email == null) {
|
||||
return;
|
||||
}
|
||||
const title = json.messages[0].content.substring(0, 20);
|
||||
await db.insert(chats).values({
|
||||
id: crypto.randomUUID(),
|
||||
title,
|
||||
userId: (req as any).auth?.user?.email,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return new StreamingTextResponse(stream);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue