OpenAI API: How do I handle errors in Python?

题意:在使用OpenAI API进行Python开发时,怎样处理错误?

问题背景:

I tried using the below code, but the OpenAI API doesn't have the AuthenticationError method in the library. How can I effectively handle such error.

我尝试使用下面的代码,但是OpenAI API的库中并没有AuthenticationError这个方法。我该如何有效地处理这种错误

python 复制代码
import openai

# Set up your OpenAI credentials
openai.api_key = 'YOUR_API_KEY'

try:
    # Perform OpenAI API request
    response = openai.some_function()  # Replace with the appropriate OpenAI API function

    # Process the response
    # ...
except openai.AuthenticationError:
    # Handle the AuthenticationError
    print("Authentication error: Invalid API key or insufficient permissions.")
    # Perform any necessary actions, such as displaying an error message or exiting the program

问题解决:

Error handling with the OpenAI Python SDK v1.0.0 or newer

在使用OpenAI Python SDK v1.0.0或更新版本时处理错误

• If you don't want to handle error types individually:

如果您不想单独处理每种错误类型:

python 复制代码
import os
from openai import OpenAI, OpenAIError
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')

try:
  # Make your OpenAI API request here
  response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Say this is a test"
  )
  print(response)
except OpenAIError as e:
  # Handle all OpenAI API errors
  print(f"Error: {e}")

• If you want to handle error types individually:

如果您想单独处理每种错误类型:

Note: Because there are a lot of classes for error handling, it might not be so elegant to import them individually. Instead, use import openai and all classes for error handling will be imported automatically. But the code is a bit different now.

注意:由于存在许多用于错误处理的类,因此单独导入它们可能不太优雅。相反,使用import openai会自动导入所有用于错误处理的类。但是,现在的代码会有所不同。

python 复制代码
import os
import openai # Import openai
from openai import OpenAI # But don't import OpenAIError
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')

try:
  # Make your OpenAI API request here
  response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Say this is a test"
  )
  print(response)
except openai.BadRequestError as e: # Don't forget to add openai
  # Handle error 400
  print(f"Error 400: {e}")
except openai.AuthenticationError as e: # Don't forget to add openai
  # Handle error 401
  print(f"Error 401: {e}")
except openai.PermissionDeniedError as e: # Don't forget to add openai
  # Handle error 403
  print(f"Error 403: {e}")
except openai.NotFoundError as e: # Don't forget to add openai
  # Handle error 404
  print(f"Error 404: {e}")
except openai.UnprocessableEntityError as e: # Don't forget to add openai
  # Handle error 422
  print(f"Error 422: {e}")
except openai.RateLimitError as e: # Don't forget to add openai
  # Handle error 429
  print(f"Error 429: {e}")
except openai.InternalServerError as e: # Don't forget to add openai
  # Handle error >=500
  print(f"Error >=500: {e}")
except openai.APIConnectionError as e: # Don't forget to add openai
  # Handle API connection error
  print(f"API connection error: {e}")

See the official OpenAI GitHub Python repository.

查询官方文档

Error handling with the OpenAI Python SDK v0.28.0

使用OpenAI Python SDK v0.28.0进行错误处理

Your code isn't correct. 你的代码不准确

Change this... 将下面的语句

python 复制代码
except openai.AuthenticationError

...to this. 修改成

python 复制代码
except openai.error.AuthenticationError
相关推荐
小喵要摸鱼1 小时前
Python 神经网络项目常用语法
python
一念之坤3 小时前
零基础学Python之数据结构 -- 01篇
数据结构·python
wxl7812273 小时前
如何使用本地大模型做数据分析
python·数据挖掘·数据分析·代码解释器
NoneCoder3 小时前
Python入门(12)--数据处理
开发语言·python
老艾的AI世界4 小时前
AI翻唱神器,一键用你喜欢的歌手翻唱他人的曲目(附下载链接)
人工智能·深度学习·神经网络·机器学习·ai·ai翻唱·ai唱歌·ai歌曲
LKID体4 小时前
Python操作neo4j库py2neo使用(一)
python·oracle·neo4j
小尤笔记4 小时前
利用Python编写简单登录系统
开发语言·python·数据分析·python基础
FreedomLeo14 小时前
Python数据分析NumPy和pandas(四十、Python 中的建模库statsmodels 和 scikit-learn)
python·机器学习·数据分析·scikit-learn·statsmodels·numpy和pandas
007php0075 小时前
GoZero 上传文件File到阿里云 OSS 报错及优化方案
服务器·开发语言·数据库·python·阿里云·架构·golang
Tech Synapse5 小时前
Python网络爬虫实践案例:爬取猫眼电影Top100
开发语言·爬虫·python