Implement preview token for oss (#23)

This commit is contained in:
Jared Palmer 2023-06-16 17:22:16 -04:00 committed by GitHub
commit 36a1861d01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 20 deletions

View file

@ -7,33 +7,27 @@ import { nanoid } from '@/lib/utils'
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 json = await req.json()
const { messages, previewToken } = json
const session = await auth()
if (session == null) {
return new Response('Unauthorized', { status: 401 })
if (process.env.VERCEL_ENV !== 'preview') {
if (session == null) {
return new Response('Unauthorized', { status: 401 })
}
}
const json = await req.json()
const { messages } = json
const configuration = new Configuration({
apiKey: previewToken || process.env.OPENAI_API_KEY
})
const openai = new OpenAIApi(configuration)
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
})