Here is my api/openai route.jsx file
import { NextResponse } from "next/server"
export async function POST(request) {
const body = await request.json()
conosle.log('body' body)
const options = {
method: 'POST',
headers: {
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: 'user', content: body.message
}
],
max_tokens: 100,
})
}
const response = await fetch('https://api.openai.com/v1/chat/completions', options)
const data = await response.json()
return NextResponse.json(data)
}
Here is my frontend page.jsx file
'use client'
import { useState } from "react"
const page = () => {
const [message, setMessage] = useState(null)
const [value, setValue] = useState('')
const getMessages = async () => {
const options = {
method: 'POST',
body:JSON.stringify({message: value}),
headers: {
"Content-Type": "application/json"
}
}
const response = await fetch('/api/openai', options)
const data = await response.json()
setMessage (data.choices[0].message)
}
return (
<div>
<div>
<input type="text" placeholder="what do you want" value={value} onChange={(e) => setValue(e.target.value)} />
<div onClick={getMessages}>
Send
</div>
</div>
</div>
)
}
export default page
I am receiving a 200 POST status of OK and now getting the response form the AI but now I get an error trying to display it in the setMessage value. I am trying to now currently display the information from the API on the screen. Getting objects are not valid as a React child.