【问题标题】:Openai api Completion.create not working on my python codeOpenai api Completion.create 无法处理我的 python 代码
【发布时间】:2022-10-18 10:36:01
【问题描述】:

在 openai.py 中,Completion.create 突出显示为警报并且也不起作用..错误就在下面..代码有什么问题

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)

【问题讨论】:

    标签: python openai


    【解决方案1】:

    尝试这个, 引擎=“达芬奇”

    【讨论】:

      【解决方案2】:

      我尝试了 openai 版本 0.18.1 并且能够运行示例 GPT-3 代码。

      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)}')
      

      【讨论】:

        【解决方案3】:

        升级openai 模块或尝试重新安装它。 text-davinci-002 是正确的引擎名称,因此无需更改。

        【讨论】:

          【解决方案4】:

          对于我的伙伴们来说,通过上述所有建议并想知道为什么它不起作用:

          确保您的文件未命名为openai.py。因为那样它会调用自己,因为 python.

          在这个废话上浪费了 2 个小时,哈哈。

          相关链接How to fix AttributeError: partially initialized module?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-10
            • 2020-02-19
            • 1970-01-01
            • 1970-01-01
            • 2022-11-08
            • 1970-01-01
            相关资源
            最近更新 更多