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。

相关推荐
小程故事多_805 小时前
Agent+Milvus,告别静态知识库,打造具备动态记忆的智能AI助手
人工智能·深度学习·ai编程·milvus
code_pgf5 小时前
Llama 3详解
人工智能·llama
ComputerInBook5 小时前
数字图像处理(4版)——第 3 章——(图像的)强度变换和空间滤波(Rafael C.Gonzalez&Richard E. Woods)
图像处理·人工智能·计算机视觉·强度变换和空间滤波
爱写代码的小朋友5 小时前
生成式人工智能(AIGC)在开放式教育问答系统中的知识表征与推理机制研究
人工智能·aigc
技术专家6 小时前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
m0_488913016 小时前
万字长文带你梳理Llama开源家族:从Llama-1到Llama-3,看这一篇就够了!
人工智能·学习·机器学习·大模型·产品经理·llama·uml
helpme流水6 小时前
LLaMA Factory 从入门到精通,一篇讲完
人工智能·ai·语言模型·llama
段一凡-华北理工大学6 小时前
【大模型+知识图谱+工业智能体技术架构】~系列文章01:快速了解与初学入门!!!
人工智能·python·架构·知识图谱·工业智能体
Swift社区6 小时前
AI Governance:从 Policy Engine 到完整治理体系
人工智能·openclaw
田井中律.6 小时前
知识图谱(BILSTM+CRF项目完整实现)【第六章】
人工智能·知识图谱