当前位置: 首页 > news >正文

OpenAI API: How to catch all 5xx errors in Python?

题意:OpenAI API:如何在 Python 中捕获所有 5xx 错误?

问题背景:

I want to catch all 5xx errors (e.g., 500) that OpenAI API sends so that I can retry before giving up and reporting an exception.

我想捕获 OpenAI API 发送的所有 5xx 错误(例如,500),以便在放弃并报告异常之前进行重试。

Right now I'm basically doing the following:

现在我基本上是在做以下操作:

try:response = openai.ChatCompletion.create(req)
except InvalidRequestError as e:reportError
except ServiceUnavailableError as e:retry
except Exception as e:response = f"Exception: {e}"raise Exception(response)

Some 5xx errors are getting caught as unknown errors (last case) which I want to catch so that I can retry them as I do in the case of the ServiceUnavailableError. But I don't know how to go about catching all the 5xx errors for retry. The docs just talk about how to catch the specifically named errors.

一些 5xx 错误被当作未知错误(最后一种情况)捕获,我想捕获这些错误,以便像处理 `ServiceUnavailableError` 一样重试。但我不知道如何捕获所有 5xx 错误以进行重试。文档只提到如何捕获特定命名的错误。

问题解决:

All 5xx errors belong to the ServiceUnavailableError. Take a look at the official OpenAI documentation:

所有 5xx 错误都属于 `ServiceUnavailableError`。请查看官方的 OpenAI 文档:

TYPEOVERVIEW
APIErrorCause: Issue on our side.
Solution: Retry your request after a brief wait and contact us if the issue persists.
TimeoutCause: Request timed out.
Solution: Retry your request after a brief wait and contact us if the issue persists.
RateLimitErrorCause: You have hit your assigned rate limit.
Solution: Pace your requests. Read more in our Rate limit guide.
APIConnectionErrorCause: Issue connecting to our services.
Solution: Check your network settings, proxy configuration, SSL certificates, or firewall rules.
InvalidRequestErrorCause: Your request was malformed or missing some required parameters, such as a token or an input.
Solution: The error message should advise you on the specific error made. Check the documentation for the specific API method you are calling and make sure you are sending valid and complete parameters. You may also need to check the encoding, format, or size of your request data.
AuthenticationErrorCause: Your API key or token was invalid, expired, or revoked.
Solution: Check your API key or token and make sure it is correct and active. You may need to generate a new one from your account dashboard.
ServiceUnavailableErrorCause: Issue on our servers.
Solution: Retry your request after a brief wait and contact us if the issue persists. Check the status page.

Handle the ServiceUnavailableError as follows:

如下处理 `ServiceUnavailableError`:

try:# Make your OpenAI API request hereresponse = openai.Completion.create(prompt="Hello world",model="text-davinci-003")except openai.error.ServiceUnavailableError as e:# Handle 5xx errors hereprint(f"OpenAI API request error: {e}")pass

http://www.lryc.cn/news/445454.html

相关文章:

  • C++初阶学习——探索STL奥秘——标准库中的priority_queue与模拟实现
  • PyTorch经典模型
  • C++ STL容器(三) —— 迭代器底层剖析
  • 力扣416周赛
  • vue 页面常用图表框架
  • spring 注解 - @PostConstruct - 用于初始化工作
  • 多机器学习模型学习
  • 【网页设计】前言
  • STM32巡回研讨会总结(2024)
  • 54 螺旋矩阵
  • 基于STM32与OpenCV的物料搬运机械臂设计流程
  • [万字长文]stable diffusion代码阅读笔记
  • watchEffect工作原理
  • 斐波那契数列
  • TCP并发服务器的实现
  • 前端大屏自适应方案
  • 16.3 k8s容器cpu内存告警指标与资源request和limit
  • 【计算机网络 - 基础问题】每日 3 题(二十)
  • 铰链损失函数
  • 项目实战bug修复
  • Git常用指令整理【新手入门级】【by慕羽】
  • 记某学校小程序漏洞挖掘
  • 腾讯百度阿里华为常见算法面试题TOP100(3):链表、栈、特殊技巧
  • Apache CVE-2021-41773 漏洞复现
  • vue-入门速通
  • 【AI大模型】通义大模型API接口实现
  • CVPR最牛图像评价算法!
  • Spring源码-从源码层面讲解传播特性
  • Rust调用tree-sitter解析C语言
  • 奇瑞汽车—经纬恒润 供应链技术共创交流日 成功举办