#"Undefined" Chat completion

44 messages · Page 1 of 1 (latest)

final blaze
#

Hey, so im making a call to the openai api to create a chat completion, the thing is, its always giving me the responde "Undefined", do any one of you know what could be happening, the code is listed in the image. Thanks.

#

the text passed to openai is in spanish, but that shoudnt make any difference, right?

glacial jungle
#

It's choices[0].content for chat completion 🙂

final blaze
#

oh, thanks man

#

mnn, seems that it stills gives me the same issue

glacial jungle
#

How does the gptResponse object look like? Have you tried printing it your console?

final blaze
#

Yes, its the standart response with all of the parameters correct as shown in the docs but the answer is just “Undefined”

#

I also checked my api key, org identificator and everything of that sort

gleaming thicket
#

Example response from their site

   "id":"chatcmpl-abc123",
   "object":"chat.completion",
   "created":1677858242,
   "model":"gpt-3.5-turbo-0301",
   "usage":{
      "prompt_tokens":13,
      "completion_tokens":7,
      "total_tokens":20
   },
   "choices":[
      {
         "message":{
            "role":"assistant",
            "content":"\n\nThis is a test!"
         },
         "finish_reason":"stop",
         "index":0
      }
   ]
}
final blaze
#

Oh, didn’t even noticed that, thank you, i cant try it right now, but ill update you as soon as possible, however, it looks as if this was the error

#

As rocketman said, i changed it to end in .content, however, i forgot to delete the .data

gleaming thicket
#

its an easy thing to make a mistake in to be fair. most async / fetch / api calls in JS e.g. you have to get the data attribute first

final blaze
#

hey, just took a look, and it gave me this error: TypeError: Cannot read properties of undefined (reading '0'), on line 40, the one which was just modified, ill be taking a look in the meanwhile as i just tested it 5min ago, sorry to ask for help, i know the support is quite crowded, but its the first time i try integrating openai into my code and so im still getting along with the docs.

gleaming thicket
#

nps

final blaze
#

message.reply(${gptResponse.choices[0].content});

#

why does it ignore this symbol `?

gleaming thicket
#

cuz its considered markdown in discord

final blaze
#

oh, sorry

gleaming thicket
#

you'd have to use this;
```js
message.reply(${gptResponse.choices[0].content});
```

message.reply(`${gptResponse.choices[0].content`});
#

wuttt

#
message.reply(`${gptResponse.choices[0].content`});
#

oh its cuz i used slashes in my message 🤔 funky discord

#

there we go, had to add more slashes.

#

lol

#

anyhow, have you tried turning on the debugger on that line, and checking whats inside gptResponse?

final blaze
#

i tried when it just said Undefined, however, i havent tried now, let me try your code and if that dosnt work ill debug it

gleaming thicket
#

also in my code i check if its successful / has a header " OK 200 " before i try reading it

#

sortof like a safeguard

final blaze
#

its also giving me a sintax error

#

just debugged it, it most likely haves to do with the discord part, as changing it to console.log(gptResponse.data.choices[0].message); gives me the correct answer

#

let my try a thing and maybe fix it

gleaming thicket
final blaze
#

ok, its fixed

#

thanks for the help

#

Bye!

gleaming thicket
#

so what was the issue? 😄

final blaze
#

not really sure, just regenerated my discord bot key to a new one and it worked

gleaming thicket
#

also for future readers 😄

final blaze
#

discord at its finest I supose

gleaming thicket
#

ah mkay thats , an edge case lol