Why does the ChatGPT API return empty results when using certain types of prompts in Python?

@ruby_coder is my code correct now?

My code:

response = openai.Completion.create(
    model="text-davinci-003",
    prompt=prompt,
    max_tokens=256,
    n=1,
    stop=None,
    temperature=0.0,
)

Yes, but you should add a stop for good measure, something like "####".

Also, you should print your completion response as follows, per the Python docs:

# print the completion
print(response.choices[0].text)

HTH

:slight_smile:


Note: If you still get a blank response, please post your prompt you are using so I can test it for you. My code works fine so it’s easy to test and I’m happy to test if for you.

OR

You can first try this to see if there is an error message (also wisely suggested by @dhiaeddine.khalfalla earlier, BTW):

print(response)

@ruby_coder this is prompt:

prompt = (
    "Extract the location, category, and keywords from the following sentence:\n\n"
    "{sentence}\n\n"
    "Location: [input location]\n"
    "Categories: {category_options}\n"
    "Keywords: [input keywords]"
)
category_options = "|".join(categories)
prompt = prompt.format(sentence=sentence, category_options=category_options)

After my tests, if I change the prompt to this, it doesn’t return blank:

prompt = (
    "Extract the location, category, and keywords from the following sentence:\n\n"
    "{sentence}\n\n"
    "Location: [input location]\n"
    "Categories: [input categories]\n"
    "Keywords: [input keywords]"
)
prompt = prompt.format(sentence=sentence)

Assuming that the test above is your single prompt (not sure that it is, but that’s another topic), I do not get a blank reply:

Appendix: Completion Setup

@Rube_coder your test code will not respond blank (second code). Try the first code (above the code you tested).

Additionally, the categories variable is a list of category strings.

I am not following you. I need your prompt text only, not the Python variable which stores your prompt.

Please post only the prompt. No python code. THE PROMPT :slight_smile: NO CODE, no Python variables :slight_smile:

Thanks.

@ruby_coder ok here’s the prompt:

Extract the location, category, and keywords from the following sentence:

I'm looking for a good italian restaurant in New York City.

Location: [input location]
Categories: Abruzzo Restaurant|Accessories|Accountant|Acupuncturist|Aerospace Company|Afghan Restaurant|African Restaurant|Agricultural Service|Agriculture|Airline Company|…
Keywords: [input keywords]

You can add more categories to your tests.

It’s ok… this is what I got… which seems like what you were looking for.

Appendix: Set up

@ruby_coder sorry, I don’t understand seeing those images. What’s the result?

The result is called “the completion”, BTW…

1 Like

@ruby_coder why does it succeed but mine doesn’t? Do you have any idea?

Well, I’m a great coder, (hahaha) and wrote a test lab which does the entire OpenAI API so I can help folks here who have problems and need “real” help in a public OpenAI developer community (like you) :slight_smile:

First of all you started off with OpenAI API code written by ChatGPT, that was a mistake.

Second, you did not follow @dhiaeddine.khalfalla correct advice to print your entire response out so others can see what the entire response looks like (part of debugging).

Run your code again and do not use the print line you got from ChatGPT. Use this and post back what you get

print(response)

:slight_smile:

1 Like

I ran into this issue and never figured out the why. But it seems to do something with the question asked after the initial prompt.

In my case I get around this by checking for an empty response, and then have the code request to ask another way, that way the code doesn’t fail in place. Here is an example of how I am passing this with Javascript:

    var s = oJson.choices[0].text;
      // Empty Response Handling	     
        if (s == "") {
    	   txtOutput.value += "Eva: I'm sorry can you please ask me in another way?";
	    } else {
    	   txtOutput.value += "Eva: " + s.trim();
	      }
1 Like

@ruby_coder here’s the terminal:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": ""
    }
  ],
  "created": created,
  "id": id,
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "prompt_tokens": 3384,
    "total_tokens": 3384
  }
}

Hi @lee19619

What was the stop param you sent to the API?

:slight_smile:

@ruby_coder I passed “####” to it as you said.

Please post the code you used.

Thanks

:slight_smile:

Here is my code:

response = openai.Completion.create(
    model="text-davinci-003",
    prompt=prompt,
    max_tokens=256,
    n=1,
    stop="####",
    temperature=0.0,
)

Good.

So why is the API saying you sent 3384 tokens to the API?

When I used the prompt text you shared, the prompt was like 100 or so tokens only.

101 tokens to be exact.

:slight_smile:

My category list is very long, about 700 categories. That’s why I typed “…” when I replied to you earlier.