I'm working with OpenAI api to create a chatbot using react typescript and next.js. I'm trying to interact with the chatbot in the UI but whenever i click send(button) nothing happens. I tried adding console.logs but even those aren't showing up in the console. Any suggestions on what is causing this?
Here is the code inside my src/app/api/route.ts:
import { NextResponse } from "next/server";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export async function POST(req: Request, res: NextResponse) {
const body = await req.json();
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: body.messages }],
});
console.log(completion.choices[0].message);
const theResponse = completion.choices[0].message;
return NextResponse.json({ output: theResponse }, { status: 200 });
}
And this is my src/app/page.tsx:
"use client";
import { useState } from "react";
export default function Home() {
const [theInput, setTheInput] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [messages, setMessages] = useState([
{
role: "assistant",
content: "Yo, this is ChatterBot! How can I help you today?",
},
]);
// below this
const callGetResponse = async () => {
setIsLoading(true);
let temp = messages;
temp.push({ role: "user", content: theInput });
setMessages(temp);
setTheInput("");
console.log("Calling OpenAI...");
const response = await fetch("/api", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ messages }),
});
const data = await response.json();
const { output } = data;
console.log("OpenAI replied...", output.content);
setMessages((prevMessages) => [...prevMessages, output]);
setIsLoading(false);
};
const Submit = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (event.key === "Enter") {
event.preventDefault();
callGetResponse();
}
};
return (
<main className="flex min-h-screen flex-col items-center justify-between px-24 py-5">
<h1 className="text-5xl font-sans">ChatterBot</h1>
<div className="flex h-[35rem] w-[40rem] flex-col items-center bg-gray-600 rounded-xl">
<div className=" h-full flex flex-col gap-2 overflow-y-auto py-8 px-3 w-full">
{messages.map((e) => {
return (
<div
key={e.content}
className={`w-max max-w-[18rem] rounded-md px-4 py-3 h-min ${
e.role === "assistant"
? "self-start bg-gray-200 text-gray-800"
: "self-end bg-gray-800 text-gray-50"
} `}
>
{e.content}
</div>
);
})}
{isLoading ? (
<div className="self-start bg-gray-200 text-gray-800 w-max max-w-[18rem] rounded-md px-4 py-3 h-min">
*thinking*
</div>
) : (
""
)}
</div>
<div className="relative w-[80%] bottom-4 flex justify-center">
<textarea
value={theInput}
onChange={(event) => setTheInput(event.target.value)}
className="w-[85%] h-10 px-3 py-2
resize-none overflow-y-auto text-black bg-gray-300 rounded-l outline-none"
onKeyDown={Submit}
/>
<button
onClick={callGetResponse}
className="w-[15%] bg-blue-500 px-4 py-2 rounded-r"
>
send
</button>
</div>
</div>
<div></div>
</main>
);
}
Make sure that you have the working OpenAI API key. When I tried with the expired key, the chatCompletionCreate API failed.
In the API route, you made a mistake when passing prompt messages. Since the request body has the assistant and user messages, you should pass request messages directly to the chat completion create API.
Here is the working version.
import { NextResponse } from "next/server";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export async function POST(req: Request, res: NextResponse) {
const body = await req.json();
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: body.messages,
});
console.log(completion.choices[0].message);
const theResponse = completion.choices[0].message;
return NextResponse.json({ output: theResponse }, { status: 200 });
}