68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
import { kv } from '@vercel/kv'
|
|
import { OpenAIStream, StreamingTextResponse } from 'ai-connector'
|
|
import { Configuration, OpenAIApi } from 'openai-edge'
|
|
|
|
import { nanoid } from '@/lib/utils'
|
|
import { currentUser } from '@clerk/nextjs'
|
|
|
|
export const runtime = 'edge'
|
|
|
|
const configuration = new Configuration({
|
|
apiKey: process.env.OPENAI_API_KEY
|
|
})
|
|
|
|
const openai = new OpenAIApi(configuration)
|
|
|
|
if (!process.env.OPENAI_API_KEY) {
|
|
throw new Error('Missing env var from OpenAI')
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
const user = await currentUser()
|
|
if (user == null) {
|
|
return new Response('Unauthorized', { status: 401 })
|
|
}
|
|
|
|
const json = await req.json()
|
|
const { messages } = json
|
|
|
|
const res = await openai.createChatCompletion({
|
|
model: 'gpt-3.5-turbo',
|
|
messages,
|
|
temperature: 0.7,
|
|
top_p: 1,
|
|
frequency_penalty: 1,
|
|
max_tokens: 500,
|
|
n: 1,
|
|
stream: true
|
|
})
|
|
|
|
const stream = OpenAIStream(res, {
|
|
async onCompletion(completion) {
|
|
const title = json.messages[0].content.substring(0, 100)
|
|
const userId = user.id
|
|
const id = json.id ?? nanoid()
|
|
const createdAt = Date.now()
|
|
const payload = {
|
|
id,
|
|
title,
|
|
userId,
|
|
createdAt,
|
|
messages: [
|
|
...messages,
|
|
{
|
|
content: completion,
|
|
role: 'assistant'
|
|
}
|
|
]
|
|
}
|
|
await kv.hmset(`chat:${id}`, payload)
|
|
await kv.zadd(`user:chat:${userId}`, {
|
|
score: createdAt,
|
|
member: `chat:${id}`
|
|
})
|
|
}
|
|
})
|
|
|
|
return new StreamingTextResponse(stream)
|
|
}
|