Python ChatGPT:模块“openai”没有属性“Completion”。您指的是:“完成”吗?

问题描述 投票:0回答:1

我尝试用 Python 为我的网站制作一个 BOT。但我有一个错误:

错误:

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'?

这是Python代码:我尝试了很多组合,也尝试了

text-davinci-003
,但没有任何效果

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)

python python-3.x bots openai-api chatgpt-api
1个回答
1
投票

您的代码中存在一些问题:

  • 使用错误的方法名称(即
    Completion
  • 使用已弃用的参数(即
    engine
  • 使用与 Completions API
  • 不兼容的模型

以下代码应该可以工作:

response = openai.completions.create( # Changed
    model="gpt-3.5-turbo-instruct", # Changed
    prompt=prompt,
    max_tokens=150
)
© www.soinside.com 2019 - 2024. All rights reserved.