pythonopenai-apigpt-4

OpenAI Chat Completions API: How do I solve the APIRemovedInV1 error when using the OpenAI Python library for GPT-4?


This is a snippet where I have been encountering the problem:

def generate_response(prompt):
    response = openai.chatcompletion.create(
        model="gpt-4",  # Use "gpt-4" as the model identifier if "gpt-4o" is deprecated
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=150
    )
    return response.choices[0].message['content'].strip()

I tried the following to solve the APIRemovedInV1 error:

  1. I have tried downgrading the library version.
  2. I have gone through the OpenAI documentation and tried the asynchronous method too but didn't work.

Solution

  • You have a double typo. You're missing a dot and the letter s.

    Change this...

    openai.chatcompletion.create
    

    ...to this.

    openai.chat.completions.create
    

    The full code is:

    from openai import OpenAI
    client = OpenAI()
    
    completion = client.chat.completions.create()
      model="gpt-4",
      messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
      ]
    )
    
    print(completion.choices[0].message)