Chat Completions API not returning a reponse

hello all. i am following a node.js / react tutorial thats summarizing a few paragraphs of text via a browser but i don’t seem to be getting a response back. i can see via my usage activity that the request is going thru. the original tutorial was using the text-davinci-003 model so i suspect my changed code for gpt-3…5-turbo is the problem. could someone look at my frontend backend code to see if they can spot what i’m doing wrong. thanks in advance.
oh yeah and when looking a the developer tools in browser my payload is { text : crap i want summaraized…} and network tab says stalled.

The problem would seem to be in your extraction of information from the return object from API. There is no “text”. there is message.content


Here’s an example (just open in an IDE window of mine) that uses the alternate ‘with_raw_response’ method, that gives you even more data to work with.

# Make API call to OpenAI
c = None
try:
    c = client.chat.completions.with_raw_response.create(**params)
except Exception as e:
    print(f"Error: {e}")

“c” is the pydantic response if the call was successful. Now we can get lots of information, where I mostly describe using variable names.

# If we got the response, load a whole bunch of demo variables
# This is different because of the 'with raw response' for obtaining headers
if c:
    headers_dict = c.headers.items().mapping.copy()  # first, get out all headers
    for key, value in headers_dict.items():
        variable_name = f'headers_{key.replace("-", "_")}'
        globals()[variable_name] = value
    remains = headers_x_ratelimit_remaining_tokens  # show we set variables with all headers
    
    api_return_dict = json.loads(c.content.decode())
    api_finish_str = api_return_dict.get('choices')[0].get('finish_reason')
    usage_dict = api_return_dict.get('usage')  # token usage info, only in non-streaming
    api_message_dict = api_return_dict.get('choices')[0].get('message')  # AI responses
    api_message_str = api_return_dict.get('choices')[0].get('message').get('content')
    api_tools_list = api_return_dict.get('choices')[0].get('message').get('tool_calls')

So what is useful? 1. text for the user to see; 2. tool calls being emitted (or functions, needing more variables). Here’s just displaying those.

    # print any response always
    if api_message_str:
        print(api_message_str)

    # print all tool functions pretty
    if api_tools_list:
        for tool_item in api_tools_list:
            print(json.dumps(tool_item, indent=2))

Hope this makes all of your wishes and dreams come true.

1 Like

ok thanks. i am a novice front end dev but my gut is telling my answer’s definitely within your post (also witinin chatgpt premium which i don’t have lol) so i just have to figure it out!!

This highlights your potential issue.

You are not querying ChatGPT premium. You are accessing the API that is billed on a token/usage basis.

Start by accessing the OpenAI Playground:
https://platform.openai.com/playground?mode=chat

Assure you have access to that. After you’ve assured access, copied your API key, then create a sample prompt and click the button on the top-right titled “View Code”
image

This will give you code that will help you gain an understanding of how to access the API in a very basic sense.

If this doesn’t make sense, please let me know specifically where it’s not clear and I am happy to elaborate.

1 Like

hmmm. sorry for the confusion. i know am querying the api and not chatgpt premium. what i was trying to express was that if i had premium, i’d most likely get the answer to my trivial problem. all though i do enjoy trying to work it for myself. i will check out the playground and see if i can come up with solution. thanks!!

Ah, that makes sense, thank you for the confirmation.

Two points to help:

  1. If you have access to the Playground (which you do), you don’t need GPT Premium. You can achieve similar results with more control by simply using the playground. Or if you like, you can use one of the prebuilt web UI’s (a few open sourced versions to emulate GPT) with your OpenAI API key.
  2. I can help clarify the help offered by @_j’s – they are explaining that a choices response/object eg-
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "\n\nHello World"
            },
            "logprobs": null,
            "finish_reason": "stop",
            "index": 0
        }

Therefore, @_j is suggesting you replace your code:

response.data.choices[0].text

With:

response.data.choices[0].message.content

Just one simple replacement in your code :slight_smile:
 


 
Sidenote: A list of web-based UI’s for OpenAI API:

3 Likes

thanks for your help. i’m getting close because i’m able to at least catch error messages now (way to many to list lol) i know it something simple, it just boils down to me not properly grabbing what i want summarized from the req body and passing it into the messages array to even get a response which i think is empty, because of the former. like i said in my original post i copied most of the code from a tutorial that was using davinci-text-003. i’ll get it at some point. :raised_hands:

@DevGirl i got it!! i changed

response.data.choices[0].message.content

to

response.choices[0].message.content

thanks everyone for the help!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.