OpenAI API 错误:“您尝试访问 openai.ChatCompletion,但 openai>=1.0.0 不再支持此功能

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

我目前正在开发 gpt 内容创建器系统,由于我使用的是 Mac,它不允许我迁移到更新的 OpenAI 库或降级它。我可以用其他功能替换 ChatCompletion 函数以在我的版本上使用吗?

代码如下:

import os
import openai

# Set the API key from an environment variable
openai.api_key = os.environ.get("api key ")

# Prompt GPT for content generation
prompt = "Write a blog post introducing an automated marketing platform for small businesses."
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "user", "content": prompt}
    ],
    max_tokens=500,
    temperature=0.7,
)

# Print the generated content
print(response.choices[0].message.content)

这是完整的错误:

Traceback (most recent call last):
  File "/Users/macbook/marketing-automation/gpt_content_generation.py", line 9, in <module>
    response = openai.ChatCompletion.create(
  File "/Users/macbook/marketing-automation/env/lib/python3.9/site-packages/openai/lib/_old_api.py", line 39, in __call__
    raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1: 

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

我尝试通过pip升级和降级。

python python-2.7 openai-api chatgpt-function-call
1个回答
0
投票

如果您检查错误消息中的链接或查看官方文档

    client = openai.OpenAI(
        api_key=...
    )

    response = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model="gpt-3.5-turbo",
    )
© www.soinside.com 2019 - 2024. All rights reserved.