OpenAI Python Package Error: ‘ChatCompletion‘ object is not subscriptable

题意:OpenAI Python Package Error: 'ChatCompletion' object 不支持下标访问

问题背景:

After updating my OpenAI package to version 1.1.1, I got this error when trying to read the ChatGPT API response:

在将我的 OpenAI 包更新到版本 1.1.1 后,当我尝试读取 ChatGPT API 响应时遇到了这个错误:

'ChatCompletion' object is not subscriptable

Here is my code:        这是我的代码:

messages = [
        {"role": "system", "content": '''You answer question about some service'''
        },
        {"role": "user", "content": 'The user question is ...'},
    ]
response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0
    )
response_message = response["choices"][0]["message"]["content"]

How can I resolve this error?        我怎样处理这个错误?

问题解决:

In the latest OpenAI package the response.choices object type is changed and in this way you must read the response:

在最新的 OpenAI 包中,response.choices 的对象类型已经发生了变化,因此你需要以这种方式来读取响应:

print(response.choices[0].message.content)

The complete working code:        完整的可工作代码(Generic Example)

from openai import OpenAI

client = OpenAI(api_key='YourKey')
GPT_MODEL = "gpt-4-1106-preview" #"gpt-3.5-turbo-1106"
messages = [
        {"role": "system", "content": 'You answer question about Web  services.'
        },
        {"role": "user", "content": 'the user message'},
    ]
response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0
    )
response_message = response.choices[0].message.content
print(response_message )

See this example in the project README.

查看项目README中的这个示例。

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值