Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python ChatGPT: module 'openai' has no attribute 'Completion'. Did you mean: 'completions'?

I try to make a BOT for my website, in Python. But I have an error:

The Error:

You: hello
Traceback (most recent call last):
  File "D:\module2.py", line 20, in <module>
    response = chat_with_gpt(user_input)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\module2.py", line 6, in chat_with_gpt
    response = openai.Completion.create(
               ^^^^^^^^^^^^^^^^^
AttributeError: module 'openai' has no attribute 'Completion'. Did you mean: 'completions'?

This is the Python code: I try many combinations, also I try with text-davinci-003, but nothing works

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import openai

openai.api_key = "API-KEY"

def chat_with_gpt(prompt):
    response = openai.Completion.create(
        # engine="text-davinci-003",  # Adjust the engine as necessary
        engine="gpt-3.5-turbo",  # Adjust the engine as necessary
        prompt=prompt,
        max_tokens=150
    )
    return response.choices[0].text.strip()

if __name__ == "__main__":
    while True:
        user_input = input("You: ")
        user_input = user_input.lower()
        if user_input in ["quit", "exit", "bye"]:
            break
        response = chat_with_gpt(user_input)
        print("Chatbot:", response)

>Solution :

There are a few problems in your code:

  • using the wrong method name (i.e., Completion)
  • using the deprecated parameter (i.e., engine)
  • using the incompatible model with the Completions API

The following code should work:

response = openai.completions.create( # Changed
    model="gpt-3.5-turbo-instruct", # Changed
    prompt=prompt,
    max_tokens=150
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading