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

I’m using ChatGPT API in my Python project.
I’m using the text-davinci-003 model. I have passed a prompt to the ChatGPT API.
However, it returns empty results to me.

Here is my Python code:

response = openai.Completion.create(
    engine="text-davinci-003",
    prompt=prompt,
    max_tokens=256,
    n=1,
    stop=None,
    temperature=0.0,
)
print(" ".join(["Response:", response["choices"][0]["text"]]))

This is the result it gives me:

Response: 

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

1 Like

Try to change the engine="text-davinci-003" to model="text-davinci-003" and rerun the inference.

1 Like

Hello @dhiaeddine.khalfalla , thank you for your reply. Unfortunately I tried it and it doesn’t work.

is the empty response problem persistent with different prompts or specific to this one ? and can you share the response object ?

1 Like

Maybe consider a different Python OpenAI lib?

:slight_smile:

@dhiaeddine.khalfalla if I use a different prompt, the empty response problem doesn’t persist. The response variable is shared.

@ruby_coder I am using openai package

Then, if that is the case, your basic params look like this:

completion = openai.Completion.create(model="text-davinci-003", prompt="Hello world")

As mentioned by @dhiaeddine.khalfalla

There is no engine param. That cannot work.

Your Original Post @lee19619

Did you use ChatGPT to write that code for you, @lee19619 ?

:slight_smile:

@ruby_coder yes, the code is generated by ChatGPT. But I have tried using model instead of engine and it doesn’t work.

Yes, but the code generated by ChatGPT is wrong. So you should go to the GitHub page and follow the code examples there, first (and please stop using ChatGPT to write code for you if you are not going to verify it):

ChatGPT writes very bad OpenAI API code based on long (close to two years) deprecated libs.

As a “novice” software developer, it’s best, I assure you, to code from the current docs of the libs you are using.

:slight_smile:

@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