0
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;
    
}

The last line shows the error "Property 'data' does not exist on type 'ChatCompletion'"

I wanted to destructure the object to fetch the data from the response object received.

eglease
  • 2,445
  • 11
  • 18
  • 28
  • have you tried the following? ```const {data} = response.data; const message = data[0].message.content;``` – Zee Sep 02 '23 at 06:13

1 Answers1

0

You’re using OpenAI NodeJS SKD v4.

Extract the message as follows:

console.log(response.choices[0].message);
Rok Benko
  • 14,265
  • 2
  • 24
  • 49