chatgpt之api的调用问题

1.调用api过程中,出现如下报错内容

先写一个测试样例

python 复制代码
import openai


openai.api_key = "OPEN_AI_KEY"
openai.api_base="OPEN_AI_BASE_URL" # 是否需要base根据自己所在地区和key情况进行

completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "帮我用python写topk算法"
        },
    ]
)

print(completion.choices[0].message)

运行后,出现如下报错。

python 复制代码
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

2. 解决问题

根据提示可知道,是API更新的缘故,查阅文档对代码进行修改即可。

python 复制代码
import openai


openai.api_key = "OPEN_AI_KEY"
openai.api_base="OPEN_AI_BASE_URL" # 是否需要base根据自己所在地区和key情况进行

# 区别主要是将openai.ChatCompletion.create换成openai.chat.completions.create
completion = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": "帮我用python写topk算法"
        },
    ],
)
print(completion.choices[0].message.content)

正常运行即可,结果如下:

3.思考

api_base是干嘛的?其实他就是调用api过程中的镜像网站,避免连接不上或者被block。

相关推荐
kisshuan123962 分钟前
基于Mask-RCNN与Res2Net的排水系统缺陷检测与分类
人工智能·数据挖掘
P.H. Infinity7 分钟前
【QLIB】一、系统架构
人工智能·金融
搬砖的kk11 分钟前
openJiuwen 快速入门:使用华为云大模型搭建 AI 智能体
数据库·人工智能·华为云
Gavin在路上18 分钟前
SpringAIAlibaba之从执行生命周期到实战落地(7)
人工智能
万俟淋曦26 分钟前
【论文速递】2025年第50周(Dec-07-13)(Robotics/Embodied AI/LLM)
人工智能·深度学习·机器人·大模型·论文·robotics·具身智能
没有不重的名么34 分钟前
When Hypergraph Meets Heterophily: New Benchmark Datasets and Baseline
人工智能·深度学习·opencv·计算机视觉·超图
zxsz_com_cn1 小时前
设备预测性维护优势全景解读:安全、降本、增效与可量化ROI
人工智能
爬点儿啥1 小时前
[Ai Agent] 13 用 Streamlit 为 Agents SDK 打造可视化“驾驶舱”
人工智能·ai·状态模式·agent·streamlit·智能体
机器学习算法与Python实战1 小时前
腾讯翻译大模型,手机可运行
人工智能
百***58841 小时前
MATLAB高效算法实战技术文章大纲1
人工智能·算法·matlab