Skip to content

Instantly share code, notes, and snippets.

@ccthecode
Last active September 30, 2023 20:23
Show Gist options
  • Save ccthecode/2bae04066c84d4d285caaaae22111e9a to your computer and use it in GitHub Desktop.
Save ccthecode/2bae04066c84d4d285caaaae22111e9a to your computer and use it in GitHub Desktop.
FIX to Error in [1:56:03]: Youtube Video: Build a SaaS AI Platform with Next.js 13, React, Tailwind, Prisma, Stripe | Full Tutorial 2023
import { auth } from "@clerk/nextjs";
import { NextResponse } from "next/server";
// Old way (v3)
// import { Configuration, OpenAIApi } from "openai"
// New way (v4)
import OpenAI from "openai"
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export async function POST (
req: Request
) {
try {
const { userId } = auth();
const body = await req.json()
const { messages } = body
// if no userid, return unauthorized 401
if (!userId)
return new NextResponse("Unauthorized", {status: 401});
// if no openai configuration api key, return "Open ai api key not configure", status 500
if (!openai.apiKey)
return new NextResponse("OpenAI API key not assigned", {status: 500});
// if no messages from openai, return "Messages are required,status: 400"
if (!messages)
return new NextResponse("Messages are required",{status: 400})
// Old way (v3)
// const response = await openai.createChatCompletion({
// model: "gpt-3.5-turbo",
// messages
// })
// New way (v4)
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages
})
// Old way (v3)
// return NextResponse.json(response.data.choices[0].message);
// New way (v4)
return NextResponse.json(response.choices[0].message);
} catch (error) {
console.log("[CONVERSATION_ERROR]", error);
return new NextResponse("Internal Error", {status: 500});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment