I’m followed a tutorial to T but unable to get a response from the API. I’m not sure if there was an update to OpenAI API or not.
Any help will be much appreciated.
/app/(main)/page.tsx
'use client'
import React from 'react';
import Navbar from './_components/navbar';
const HomePage = () => {
return (
<div className="w-full min-h-full">
<Navbar />
<div className="flex flex-col justify-center">
<h1 className="text-4xl">Welcome to Mood Palette</h1>
<button
onClick={async () => {
const response = await fetch('/api/chat-gpt', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Hello, World!',
}),
});
console.log("Response", response);
}}
>
Create Palette
</button>
</div>
</div>
);
}
export default HomePage;
/api/route.ts
import { NextRequest, NextResponse } from "next/server";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: `Bearer ${process.env.OPENAI_API_KEY}`,
project: 'project_id',
});
export async function POST(request: Request) {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are Jarvis from Iron Manl" },
{ role: "user", content: "Who are you?"}
],
temperature: 0,
max_tokens: 1024,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
});
return NextResponse.json(response);
}
I just followed a the Open AI doc’s ‘getting started’ and a tutorial but unable to progress after the API is not responding.