99import org .springframework .beans .factory .annotation .Value ;
1010import org .springframework .context .annotation .Bean ;
1111import org .springframework .context .annotation .Configuration ;
12+ import org .springframework .http .client .SimpleClientHttpRequestFactory ;
13+ import org .springframework .web .client .RestClient ;
14+
15+ import java .time .Duration ;
1216
1317@ Configuration
1418public class OpenAiConfig {
@@ -24,10 +28,16 @@ public class OpenAiConfig {
2428 @ Value ("${spring.ai.openai.chat.options.max-tokens}" )
2529 private Integer maxTokens ;
2630
31+ @ Value ("${spring.ai.openai.timeout:60}" )
32+ private Integer timeoutSeconds ;
33+
2734 @ Bean
2835 public OpenAiChatModel openAiChatModel () {
36+ RestClient .Builder restClientBuilder = createRestClientBuilder ();
37+
2938 OpenAiApi openAiApi = OpenAiApi .builder ()
3039 .apiKey (apiKey )
40+ .restClientBuilder (restClientBuilder )
3141 .build ();
3242
3343 OpenAiChatOptions options = OpenAiChatOptions .builder ()
@@ -44,8 +54,11 @@ public OpenAiChatModel openAiChatModel() {
4454
4555 @ Bean
4656 public OpenAiEmbeddingModel openAiEmbeddingModel () {
57+ RestClient .Builder restClientBuilder = createRestClientBuilder ();
58+
4759 OpenAiApi openAiApi = OpenAiApi .builder ()
4860 .apiKey (apiKey )
61+ .restClientBuilder (restClientBuilder )
4962 .build ();
5063
5164 OpenAiEmbeddingOptions options = OpenAiEmbeddingOptions .builder ()
@@ -55,4 +68,13 @@ public OpenAiEmbeddingModel openAiEmbeddingModel() {
5568
5669 return new OpenAiEmbeddingModel (openAiApi , MetadataMode .EMBED , options );
5770 }
71+
72+ private RestClient .Builder createRestClientBuilder () {
73+ SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory ();
74+ requestFactory .setConnectTimeout (Duration .ofSeconds (timeoutSeconds ));
75+ requestFactory .setReadTimeout (Duration .ofSeconds (timeoutSeconds ));
76+
77+ return RestClient .builder ()
78+ .requestFactory (requestFactory );
79+ }
5880}
0 commit comments