1.調用api過程中,出現如下報錯內容
先寫一個測試樣例
import openaiopenai.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 openaiopenai.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。