1
+ package me .pacphi .config ;
2
+
3
+ import org .springframework .ai .autoconfigure .openai .OpenAiChatProperties ;
4
+ import org .springframework .ai .autoconfigure .openai .OpenAiConnectionProperties ;
5
+ import org .springframework .ai .chat .client .ChatClient ;
6
+ import org .springframework .ai .chat .client .advisor .SimpleLoggerAdvisor ;
7
+ import org .springframework .ai .model .function .FunctionCallbackResolver ;
8
+ import org .springframework .ai .openai .OpenAiChatModel ;
9
+ import org .springframework .ai .openai .OpenAiChatOptions ;
10
+ import org .springframework .ai .openai .api .OpenAiApi ;
11
+ import org .springframework .context .annotation .Bean ;
12
+ import org .springframework .context .annotation .Configuration ;
13
+ import org .springframework .context .annotation .Profile ;
14
+ import org .springframework .http .HttpHeaders ;
15
+ import org .springframework .retry .support .RetryTemplate ;
16
+ import org .springframework .web .client .ResponseErrorHandler ;
17
+ import org .springframework .web .client .RestClient ;
18
+ import org .springframework .web .reactive .function .client .WebClient ;
19
+
20
+ import java .util .Map ;
21
+ import java .util .stream .Collectors ;
22
+
23
+ @ Configuration
24
+ public class MultiChat {
25
+
26
+ @ Bean
27
+ public Map <String , ChatClient > chatClients (
28
+ OpenAiConnectionProperties connectionProperties ,
29
+ OpenAiChatProperties chatProperties ,
30
+ WebClient .Builder webClientBuilder ,
31
+ RetryTemplate retryTemplate ,
32
+ FunctionCallbackResolver functionCallbackResolver ,
33
+ ResponseErrorHandler responseErrorHandler ,
34
+ MultiChatProperties multiChatProperties
35
+ ) {
36
+ RestClient .Builder restClientBuilder = RestClient .builder ()
37
+ .defaultHeaders (headers -> headers .set (HttpHeaders .ACCEPT_ENCODING , "gzip, deflate" ));
38
+
39
+ OpenAiApi openAiApi = new OpenAiApi (
40
+ chatProperties .getBaseUrl () != null ? chatProperties .getBaseUrl () : connectionProperties .getBaseUrl (),
41
+ chatProperties .getApiKey () != null ? chatProperties .getApiKey () : connectionProperties .getApiKey (),
42
+ restClientBuilder ,
43
+ webClientBuilder ,
44
+ responseErrorHandler
45
+ );
46
+
47
+ return multiChatProperties .getOptions ().getModels ().stream ().collect (
48
+ Collectors .toMap (
49
+ model -> model ,
50
+ model -> {
51
+ OpenAiChatOptions chatOptions = OpenAiChatOptions .fromOptions (chatProperties .getOptions ());
52
+ chatOptions .setModel (model );
53
+ OpenAiChatModel openAiChatModel = new OpenAiChatModel (
54
+ openAiApi ,
55
+ chatOptions ,
56
+ functionCallbackResolver ,
57
+ retryTemplate
58
+ );
59
+ // Create ChatClient with similar configuration to original service
60
+ return ChatClient .builder (openAiChatModel )
61
+ .defaultAdvisors (
62
+ new SimpleLoggerAdvisor ())
63
+ .build ();
64
+ }
65
+ )
66
+ );
67
+ }
68
+
69
+ }
0 commit comments