1
1
package me .pacphi .config ;
2
2
3
+ import io .micrometer .observation .ObservationRegistry ;
4
+ import org .apache .commons .lang3 .StringUtils ;
3
5
import org .springframework .ai .autoconfigure .openai .OpenAiChatProperties ;
4
6
import org .springframework .ai .autoconfigure .openai .OpenAiConnectionProperties ;
5
7
import org .springframework .ai .chat .client .ChatClient ;
6
8
import org .springframework .ai .chat .client .advisor .SimpleLoggerAdvisor ;
7
- import org .springframework .ai .model .function .FunctionCallbackResolver ;
9
+ import org .springframework .ai .model .SimpleApiKey ;
10
+ import org .springframework .ai .model .tool .DefaultToolCallingManager ;
8
11
import org .springframework .ai .openai .OpenAiChatModel ;
9
12
import org .springframework .ai .openai .OpenAiChatOptions ;
10
13
import org .springframework .ai .openai .api .OpenAiApi ;
14
+ import org .springframework .ai .retry .RetryUtils ;
11
15
import org .springframework .context .annotation .Bean ;
12
16
import org .springframework .context .annotation .Configuration ;
13
- import org .springframework .context .annotation .Profile ;
14
17
import org .springframework .http .HttpHeaders ;
15
18
import org .springframework .retry .support .RetryTemplate ;
16
- import org .springframework .web .client .ResponseErrorHandler ;
17
19
import org .springframework .web .client .RestClient ;
18
20
import org .springframework .web .reactive .function .client .WebClient ;
19
21
@@ -29,21 +31,32 @@ public Map<String, ChatClient> chatClients(
29
31
OpenAiChatProperties chatProperties ,
30
32
WebClient .Builder webClientBuilder ,
31
33
RetryTemplate retryTemplate ,
32
- FunctionCallbackResolver functionCallbackResolver ,
33
- ResponseErrorHandler responseErrorHandler ,
34
34
MultiChatProperties multiChatProperties
35
35
) {
36
+ HttpHeaders headers = new HttpHeaders ();
37
+ headers .set (HttpHeaders .ACCEPT_ENCODING , "gzip, deflate" );
38
+
36
39
RestClient .Builder restClientBuilder = RestClient .builder ()
37
- .defaultHeaders (headers -> headers .set (HttpHeaders .ACCEPT_ENCODING , "gzip, deflate" ));
40
+ .defaultHeaders (h -> h .addAll (headers ));
41
+
42
+ String apiKey = connectionProperties .getApiKey ();
43
+ if (connectionProperties .getApiKey ().equalsIgnoreCase ("redundant" ) && StringUtils .isNotBlank (chatProperties .getApiKey ())) {
44
+ apiKey = chatProperties .getApiKey ();
45
+ }
38
46
39
47
OpenAiApi openAiApi = new OpenAiApi (
40
48
chatProperties .getBaseUrl () != null ? chatProperties .getBaseUrl () : connectionProperties .getBaseUrl (),
41
- chatProperties .getApiKey () != null ? chatProperties .getApiKey () : connectionProperties .getApiKey (),
49
+ new SimpleApiKey (apiKey ),
50
+ headers ,
51
+ "/v1/chat/completions" ,
52
+ "/v1/embeddings" ,
42
53
restClientBuilder ,
43
54
webClientBuilder ,
44
- responseErrorHandler
55
+ RetryUtils . DEFAULT_RESPONSE_ERROR_HANDLER
45
56
);
46
57
58
+ ObservationRegistry observationRegistry = ObservationRegistry .NOOP ;
59
+
47
60
return multiChatProperties .getOptions ().getModels ().stream ().collect (
48
61
Collectors .toMap (
49
62
model -> model ,
@@ -52,9 +65,10 @@ public Map<String, ChatClient> chatClients(
52
65
chatOptions .setModel (model );
53
66
OpenAiChatModel openAiChatModel = new OpenAiChatModel (
54
67
openAiApi ,
55
- chatOptions ,
56
- functionCallbackResolver ,
57
- retryTemplate
68
+ chatProperties .getOptions (),
69
+ DefaultToolCallingManager .builder ().observationRegistry (observationRegistry ).build (),
70
+ retryTemplate ,
71
+ observationRegistry
58
72
);
59
73
// Create ChatClient with similar configuration to original service
60
74
return ChatClient .builder (openAiChatModel )
0 commit comments