1.调用api过程中,出现如下报错内容
先写一个测试样例
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)
运行后,出现如下报错。
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更新的缘故,查阅文档对代码进行修改即可。
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。