Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@soerface
Copy link
Contributor

@soerface soerface commented Dec 27, 2022

Discriminate between server and client errors. If it's a client error, parse the JSON to pretty print the failure reason. I assume that every client error response looks similar (contains error.message).

This is the response I got because a payment method was missing:

$ curl -X POST https://api.openai.com/v1/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "top_p": 1,
    "stop": "```",
    "temperature": 0,
    "suffix": "\n```",
    "max_tokens": 1000,
    "presence_penalty": 0,
    "frequency_penalty": 0,
    "model": "text-davinci-003",
    "prompt": "your prompt"                
}'

{
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details.",
        "type": "insufficient_quota",
        "param": null,
        "code": null
    }
}

Fixes #2

.unwrap();

let status_code = response.status();
if status_code.is_client_error() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to get the response body after using error_for_status. It looks to me like the Error type does not expose the response body. Since error_for_status is not doing much more than turning the response into ok / error by using if status.is_client_error() || status.is_server_error() {, I just use those methods here directly.

@m1guelpf m1guelpf merged commit e9551cb into m1guelpf:main Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More info for error "Failed to get a response. Have you set the OPENAI_API_KEY variable?"

2 participants