diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 4f63d1e..3a9f472 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -7,6 +7,12 @@ import { nanoid } from '@/lib/utils' export const runtime = 'edge' +const configuration = new Configuration({ + apiKey: process.env.OPENAI_API_KEY +}) + +const openai = new OpenAIApi(configuration) + export async function POST(req: Request) { const json = await req.json() const { messages, previewToken } = json @@ -18,11 +24,9 @@ export async function POST(req: Request) { } } - const configuration = new Configuration({ - apiKey: previewToken || process.env.OPENAI_API_KEY - }) - - const openai = new OpenAIApi(configuration) + if (previewToken) { + configuration.apiKey = previewToken + } const res = await openai.createChatCompletion({ model: 'gpt-3.5-turbo', @@ -34,7 +38,7 @@ export async function POST(req: Request) { const stream = OpenAIStream(res, { async onCompletion(completion) { const title = json.messages[0].content.substring(0, 100) - const userId = session?.user.id + const userId = session?.user?.id if (userId) { const id = json.id ?? nanoid() const createdAt = Date.now() diff --git a/auth.ts b/auth.ts index a73db04..0b714d6 100644 --- a/auth.ts +++ b/auth.ts @@ -15,6 +15,7 @@ export const { auth, CSRF_experimental } = NextAuth({ + // @ts-ignore providers: [GitHub], callbacks: { jwt: async ({ token, profile }) => { diff --git a/package.json b/package.json index 918200c..ebd936c 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "@vercel/analytics": "^1.0.0", "@vercel/kv": "^0.2.1", "@vercel/og": "^0.5.7", - "ai": "^2.1.2", + "ai": "^2.1.6", "body-scroll-lock": "4.0.0-beta.0", "class-variance-authority": "^0.4.0", "clsx": "^1.2.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e6d3739..a16fd5b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,8 +42,8 @@ dependencies: specifier: ^0.5.7 version: 0.5.7 ai: - specifier: ^2.1.2 - version: 2.1.2(react@18.2.0)(svelte@3.59.2)(vue@3.3.4) + specifier: ^2.1.6 + version: 2.1.6(react@18.2.0)(svelte@3.59.2)(vue@3.3.4) body-scroll-lock: specifier: 4.0.0-beta.0 version: 4.0.0-beta.0 @@ -1481,8 +1481,8 @@ packages: hasBin: true dev: true - /ai@2.1.2(react@18.2.0)(svelte@3.59.2)(vue@3.3.4): - resolution: {integrity: sha512-WhyGH95Pv/gpgVMQT5eCyKXJh7irozr+bmpJ94somfRYBcAN2U270ii/OOHzGLrPM3liTbBilGQL2Ds7/5sz9Q==} + /ai@2.1.6(react@18.2.0)(svelte@3.59.2)(vue@3.3.4): + resolution: {integrity: sha512-YFCy7KEUDvuNGNuD8Bz7BynXB/eW2jJRAxAIM8Hb5/gpppZJs6/nTSdpocROxoGESJchtkFgYdygieG/wciEEQ==} engines: {node: '>=14.6'} peerDependencies: react: ^18.0.0