2

I'm trying to build a discord bot that uses the GPT-4 API to function as a chatbot on discord. I have the most recent version of the OpenAI library but when I run my code it tells me "An error occurred: module 'openai' has no attribute 'ChatCompletion'"

I tried uninstalling and reinstalling the OpenAI library, I tried using the completions endpoint and got the error "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"

This is the snippet of code thats giving me issues:

async def get_gpt_response(prompt, history):
    history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
    chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
    
    completions = openai.ChatCompletion.create(
        engine=config["model"],
        prompt=chat_prompt,
        max_tokens=config["max_tokens"],
        n=1,
        temperature=config["temperature"],
    )
    return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()
Anthony
  • 21
  • 1
  • 2
  • Yes in the code snippet I provided you can see it is using the ChatCompletions endpoint rather than the Completions endpoint – Anthony Mar 28 '23 at 00:36
  • First of all, did you get access to the GPT-4 API? Second of all, your code looks like you want to use the GPT-3 API. Which OpenAI model do you want to use? This question needs more focus. – Rok Benko Mar 28 '23 at 10:03

7 Answers7

7

Make sure you don’t have a file called “openai.py”

Ahmad Mousavi
  • 533
  • 1
  • 6
  • 17
2

I experienced exactly the same error, even after just installation the OpenAi library. I ran the script below:

pip install --upgrade openai

Which gave me that latest version with ChatCompletion as a method.

2

Yes i have the file nameed openai.py after changes the name it runs

  • I think the answer to remove the file (i.e. by renaming it) already exists [here](https://stackoverflow.com/a/76084452/4883195), no need to repeat it – Moritz Ringler May 11 '23 at 12:03
1

Make sure you have the latest OpenAI library. I have the same issue and resolved it by upgrade openai 26.5 to 27.2 version.

Hao Hu
  • 11
  • 1
0

The end-point strictly depends on the engine used for completion. So which engine did you used? I can't try GPT4 cause it's not already open to everyone but i suppose API are similar to GPT3.5 For example if you use

openai.Completion.create

you have to use a text-completion engine like text-davinci-003

If you use

openai.ChatCompletion.create

you have to use a chat engine like gpt-3.5-turbo

here a little snippet of code where self.COMPLETIONS_MODEL = "gpt-3.5-turbo"

  COMPLETIONS_API_PARAMS = {
      # We use temperature of 0.0 because it gives the most predictable, factual answer.
      "temperature": 0.0,
      "max_tokens": 256,
      "model": self.COMPLETIONS_MODEL,
  }    
  prompt = self.construct_prompt(
      query,
      document_embeddings,
      df
  )
  
  messages = [
      {"role":"system","content":"Rispondi a domande sull'utilizzo del servizio WebAPI di Passepartout"},
      {"role":"user","content":prompt}
    ]
  response = openai.ChatCompletion.create(
              messages=messages,
              **COMPLETIONS_API_PARAMS
          )  
calabrone
  • 96
  • 2
  • 9
0

Be sure your python version is 3.8 or 3.9. I used 3.6 and had the same issue, it was not until I upgraded that it worked correctly

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/34524252) – Alez Jun 13 '23 at 10:49
0

So I also faced the same issue and this is because of the latest version of the openai library is not on your system , could be because when you install from requirements.txt or maybe something else but here's how I solved it :

You have to just uninstall the current openai library, and install the latest openai library version.

pip uninstall openai

pip install openai
Bihag Kashikar
  • 1,009
  • 1
  • 2
  • 13