openai-api - OpenAI API错误: "Property ' data' does not exist on type 'ChatCompletion' "

标签 openai-api chatgpt-api

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/

相关文章:

javascript - 聊天 GPT API key 故障排除

google-sheets - 让 ChatGPT 在 Google 表格中的所有行中回答

nlp - 带有嵌入的 OpenAI 提示中的上下文有限

python - 用openai图表txt文件编码到midi转换器

plotly-dash - 破折号 : Long-Running callback that streams tokens into a Markdown component

python - OpenAI API错误: "The model ' curie:ft-personal-2023-09-15-06-05-0 1' does not exist" when trying to delete a fine-tuned model

python - 如何从 GYM 制作手推车游戏,用户可以使用键盘玩

python - OpenAI ChatGPT (GPT-3.5) API 错误 429 : "You exceeded your current quota, please check your plan and billing details"

python - 在自定义环境(python、强化学习、openai)中应用 q-learning 的问题

python - OpenAI 检索文件内容