Closed thesurlydev closed 4 months ago
Quick temporary workaround:
ClientHttpRequestFactorySettings requestFactorySettings = new ClientHttpRequestFactorySettings(
connectTimeout , readTimeout , SslBundle.of(null));
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactories.get(requestFactorySettings);
Builder clientBuilder = RestClient.builder().requestFactory(requestFactory);
OpenAiApi openAiApi = new OpenAiApi("https://api.openai.com", openAiToken, clientBuilder);
openAiChatClient = new OpenAiChatClient(openAiApi);
you can configure timeout settings easier with RestClientCustomizer
.
@Bean
public RestClientCustomizer restClientCustomizer() {
return restClientBuilder -> restClientBuilder
.requestFactory(ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings.DEFAULTS
.withConnectTimeout(Duration.ofSeconds(1))
.withReadTimeout(Duration.ofSeconds(2))));
}
This works not only for OpenAI but also for clients that use RestClient
under the hood.
@making Acting on client at configuration level would affect the context-provided RestClientBuilder. So customization would apply wherever you injected a RestClientBuilder in your application and for every spring boot starter using it. That's why I proposed a programmatic approach as a workaround.
With bigger models like GPT-4 response to non-streamed requests often takes more than 1 minute to come. So I think that read timeout deserves a dedicated configuration property, avoiding the need for workarounds.
Anyway any comments or criticism is appreciated.
@thingersoft Kind of agree. In the app side, timeouts can also be overridden via the builder. Timeouts are a concern not only for OpenAI but also across clients, so if the dedicated property is created at Spring AI level, I would expect that the design be common to all clients.
@making Indeeed my first thought was to introduce the new property at an higher level.
Unfortunately the current codebase lacks infrastructure to define properties commons to all ModelClients. A few architectural changes was needed but being my first contribution to the project I didn't want to be too intrusive, so I falled back to an OpenAI dedicated property.
Anyway if a reviewer tells me it's ok I could open a new pull request with a more generic approach.
We are looking into a more generic approach, perhaps based on the generic model, perhaps based on aspects.
0.9 is when I thought would be a good point to tackle this problem. In the meantime, we can expose a few retry options for what we have now as an initial step. spring.ai.openai.chat.retry.XYZ
property, where the @ConfigurationProperty for retry would be reusable across multiple implementation eventually.
Also, in the meantime we can provide an additional constructor that takes a RetryTemplate
and in the autoconfiguraiton we would pass in a default implementation if one wasn't provided in the application context already.
@markpollack
Hello Mark,
I may be missing something here since I didn't use Spring Retry before but I can't see how retry options and RetryTemplate
in particular relates to client read timeout configuration.
@markpollack Hello Mark, I may be missing something here since I didn't use Spring Retry before but I can't see how retry options and
RetryTemplate
in particular relates to client read timeout configuration.
@markpollack I would be happy to contribute if you can tell me something about my quoted remark.
Getting back to these issues now. Just saying that retry is in the same family of concerns around connectivty.
Closing in favor of issue #512
I'd like to think that there is a general solution for this not related to AI. For example, an app is supposed to be able to talk to say 10 microservices, each with different timeouts.
you can configure timeout settings easier with
RestClientCustomizer
.@Bean public RestClientCustomizer restClientCustomizer() { return restClientBuilder -> restClientBuilder .requestFactory(ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings.DEFAULTS .withConnectTimeout(Duration.ofSeconds(1)) .withReadTimeout(Duration.ofSeconds(2)))); }
This works not only for OpenAI but also for clients that use
RestClient
under the hood.
update Springboot3.4 remove okhttp3
Expected Behavior
Spring properties are exposed to control timeouts used by the clients.
Current Behavior
There's no documentation or properties to control things like connect, read, and write timeouts.
Context
The default timeout configuration results in some requests timing out. Here's an example: