Expected Behavior

It would be great to have an explicit error message when the connection with OpenAI cannot be established.

Current Behavior (Using Spring AI 0.8.1) Messages are quite hard to understand when there is a connection error with the OpenAI API. For instance, if there is a mistake in my API key, I get the following error:

2024-05-26T21:23:44.729+08:00  WARN 43707 --- [petclinic-prompt] [           main] o.s.a.a.r.SpringAiRetryAutoConfiguration : Retry error. Retry count:1

org.springframework.web.client.RestClientException: Error while extracting response for type [org.springframework.ai.openai.api.OpenAiApi$ChatCompletion] and content type [application/json;charset=utf-8]
    at org.springframework.web.client.DefaultRestClient.readWithMessageConverters(DefaultRestClient.java:240) ~[spring-web-6.1.8.jar:6.1.8]
    at org.springframework.web.client.DefaultRestClient$DefaultResponseSpec.readBody(DefaultRestClient.java:688) ~[spring-web-6.1.8.jar:6.1.8]

Using curl, we are getting an error message that is easier to understand, so I thought it might be possible to improve it in Spring AI:

Calling OpenAI
{
    "error": {
        "message": "Incorrect API key provided: sk-proj-*********************************************F2yw. You can find your API key at https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}

Thanks a lot for the great work!

Comment From: Chris-zerocopy

I would like to add to this to include the response status code in the response objects that are returned.

The OpenAI API standard communicates a lot via the status codes and it would be useful to have access to this information to be able to handle different situations gracefully - e.g. hitting the monthly spending cap.