import openai from "@/openai";
import { NextResponse } from "next/server";
export async function POST(request: Request){
const {todos} = await request.json();
//communicate with openai api
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
temperature: 0.8,
n: 1,
stream: false,
messages: [
{
role: "system",
content: `When responding, welcome the user always as Hello User and say Welcome to the Todo List App!
Limit text to 200 characters.`
},
{
role: "user",
content: `Hi there, provide a summary of the following todos. Count how many todos are in each category such as To do, in progress and done,
then tell the user to have a productive day! Here's the data: ${JSON.stringify(
todos
)}`
}
]
});
const {data} = response;
}
最后一行显示错误“ChatCompletion”类型上不存在属性“data”
我想解构该对象以从收到的响应对象中获取数据。
最佳答案
您正在使用 OpenAI NodeJS SDK v4
。
按如下方式提取消息:
console.log(response.choices[0].message);
提取消息内容如下:
console.log(response.choices[0].message.content);
关于openai-api - OpenAI API错误: "Property ' data' does not exist on type 'ChatCompletion' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77025306/