gpt4 book ai didi

OpenAI API: How to catch all 5xx errors in Python?(OpenAI API:如何在Python中捕获所有5xx错误?)

转载 作者:bug小助手 更新时间:2023-10-24 20:50:36 24 4
gpt4 key购买 nike



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文档:








































TYPE OVERVIEW
APIError Cause: Issue on our side.
Solution: Retry your request after a brief wait and contact us if the issue persists.
Timeout Cause: Request timed out.
Solution: Retry your request after a brief wait and contact us if the issue persists.
RateLimitError Cause: You have hit your assigned rate limit.
Solution: Pace your requests. Read more in our Rate limit guide.
APIConnectionError Cause: Issue connecting to our services.
Solution: Check your network settings, proxy configuration, SSL certificates, or firewall rules.
InvalidRequestError Cause: 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.
AuthenticationError Cause: 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.
ServiceUnavailableError Cause: 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 here
response = openai.Completion.create(prompt="Hello world",
model="text-davinci-003")

except openai.error.ServiceUnavailableError as e:
# Handle 5xx errors here
print(f"OpenAI API request error: {e}")
pass


you should use openai.error for example.

例如,您应该使用Openai.error。


except openai.error.APIConnectionError as e:
print(f"Failed to connect to OpenAI API: {e}")
pass

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com