2

In openai.py the Completion.create is highlighting as alert and also not working.. the error is right down below.. whats the problem with the code

response = openai.Completion.create(
    engine="text-davinci-002",
    prompt="Generate blog topic on: Ethical hacking",
    temperature=0.7,
    max_tokens=256,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
)

$ python openai.py
Traceback (most recent call last):
  File "E:\python\openAI\openai.py", line 2, in <module>
    import openai
  File "E:\python\openAI\openai.py", line 9, in <module>
    response = openai.Completion.create(
AttributeError: partially initialized module 'openai' has no attribute 'Completion' (most likely due to a circular import)
Märuf
  • 53
  • 1
  • 1
  • 7

4 Answers4

23

for my fellow doofuses going thru all the above suggestions and wondering why its not working:

make sure your file is NOT named openai.py. because then it will call itself, because python.

wasted 2 hours on this nonsense lol.

relevant link How to fix AttributeError: partially initialized module?

swyx
  • 2,378
  • 5
  • 24
  • 39
  • 1
    Oh, not only that but if you have another copy of the script that has the name of an imported module in your current dir (or path?) but invoke it under a different name, you'll still run into the same problem. – clearlight Feb 18 '23 at 05:38
  • 1
    you are God :) cant believe this was the cause – Amarsh May 23 '23 at 00:12
1

I tried the openai version 0.18.1 and was able to run a sample GPT-3 code.

pip install openai==0.18.1


import openai
import config

openai.api_key = config.OPENAI_API_KEY if 'OPENAI_API_KEY' in dir(config) else ''
print(f'openai.api_key : {openai.api_key}')


def openAIQuery(query):
    response = openai.Completion.create(
      engine="davinci-instruct-beta-v3",
      prompt=query,
      temperature=0.8,
      max_tokens=200,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0)

    if 'choices' in response:
        if len(response['choices']) > 0:
            answer = response['choices'][0]['text']
        else:
            answer = 'Opps sorry, you beat the AI this time'
    else:
        answer = 'Opps sorry, you beat the AI this time'

    return answer


if __name__ == '__main__':
    if not openai.api_key:
        print(f'api_key is not set')
        exit(0)
        
    query = 'Generate a keras 3 layer neural network python code for classification'
    try:
        response = openAIQuery(query)
        print(f'Response : {response}')
    except Exception as e:
        print(f'Exception : {str(e)}')
Biranchi
  • 16,120
  • 23
  • 124
  • 161
0

I had the same issue. My problem was that I created a virtual environment with the name openai. That name conflicts with the library. Make sure that openai is not used for a folder or virtual environment.

FabianVal
  • 374
  • 1
  • 7
-1

The problem is coming from your file name rename to opai.py instead of openai.py from example then python opai.py if not it will call itself.

Abass Dev
  • 71
  • 6
  • 2
    This is the same solution as in [this other answer](https://stackoverflow.com/a/74105084/2227743). – Eric Aya Jan 27 '23 at 15:01