1
+ /*
2
+ * Copyright 2025 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
package io .pivotal .cfenv .boot .genai ;
2
17
18
+ import java .util .List ;
19
+ import java .util .Map ;
20
+ import java .util .function .Predicate ;
21
+
3
22
import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
4
23
import com .fasterxml .jackson .annotation .JsonProperty ;
5
24
import org .slf4j .Logger ;
6
25
import org .slf4j .LoggerFactory ;
26
+
7
27
import org .springframework .ai .chat .model .ChatModel ;
8
28
import org .springframework .ai .document .MetadataMode ;
9
29
import org .springframework .ai .embedding .EmbeddingModel ;
14
34
import org .springframework .ai .openai .api .OpenAiApi ;
15
35
import org .springframework .web .client .RestClient ;
16
36
17
- import java . util . AbstractMap ;
18
- import java . util . List ;
19
- import java . util . Map ;
20
- import java . util . function . Predicate ;
21
-
22
- public class DefaultGenAILocator implements GenAILocator {
37
+ /**
38
+ * Locates available models and mcp servers from ai-servers config endpoint
39
+ *
40
+ * @author Gareth Evans
41
+ **/
42
+ public class DefaultGenaiLocator implements GenaiLocator {
23
43
24
- private static final Logger LOGGER = LoggerFactory .getLogger (DefaultGenAILocator .class );
44
+ private static final Logger LOGGER = LoggerFactory .getLogger (DefaultGenaiLocator .class );
25
45
26
46
private final String configUrl ;
27
47
private final String apiKey ;
28
48
private final String apiBase ;
49
+ private final RestClient .Builder builder ;
29
50
30
- public DefaultGenAILocator (String configUrl , String apiKey , String apiBase ) {
51
+ public DefaultGenaiLocator (RestClient .Builder builder , String configUrl , String apiKey , String apiBase ) {
52
+ this .builder = builder ;
31
53
this .configUrl = configUrl ;
32
54
this .apiKey = apiKey ;
33
55
this .apiBase = apiBase ;
@@ -67,7 +89,7 @@ public ChatModel getChatModelByName(String name) {
67
89
return models .stream ()
68
90
.filter (filterModelConnectivityOnCapability ("CHAT" ))
69
91
.filter (c -> c .name ().equals (name ))
70
- .map (DefaultGenAILocator ::createChatModel )
92
+ .map (DefaultGenaiLocator ::createChatModel )
71
93
.findFirst ()
72
94
.orElseThrow (
73
95
() -> new RuntimeException ("Unable to find chat model with name '" + name + "'" ));
@@ -80,7 +102,7 @@ public List<ChatModel> getChatModelsByLabels(Map<String, String> labels) {
80
102
return models .stream ()
81
103
.filter (filterModelConnectivityOnCapability ("CHAT" ))
82
104
.filter (filterModelConnectivityOnLabels (labels ))
83
- .map (DefaultGenAILocator ::createChatModel )
105
+ .map (DefaultGenaiLocator ::createChatModel )
84
106
.toList ();
85
107
}
86
108
@@ -90,7 +112,7 @@ public ChatModel getFirstAvailableChatModel() {
90
112
91
113
return models .stream ()
92
114
.filter (filterModelConnectivityOnCapability ("CHAT" ))
93
- .map (DefaultGenAILocator ::createChatModel )
115
+ .map (DefaultGenaiLocator ::createChatModel )
94
116
.findFirst ()
95
117
.orElseThrow (() -> new RuntimeException ("Unable to find first chat model" ));
96
118
}
@@ -102,7 +124,7 @@ public ChatModel getFirstAvailableChatModelByLabels(Map<String, String> labels)
102
124
return models .stream ()
103
125
.filter (filterModelConnectivityOnCapability ("CHAT" ))
104
126
.filter (filterModelConnectivityOnLabels (labels ))
105
- .map (DefaultGenAILocator ::createChatModel )
127
+ .map (DefaultGenaiLocator ::createChatModel )
106
128
.findFirst ()
107
129
.orElseThrow (() -> new RuntimeException ("Unable to find first chat model" ));
108
130
}
@@ -114,7 +136,7 @@ public List<ChatModel> getToolModelsByLabels(Map<String, String> labels) {
114
136
return models .stream ()
115
137
.filter (filterModelConnectivityOnCapability ("TOOLS" ))
116
138
.filter (filterModelConnectivityOnLabels (labels ))
117
- .map (DefaultGenAILocator ::createChatModel )
139
+ .map (DefaultGenaiLocator ::createChatModel )
118
140
.toList ();
119
141
}
120
142
@@ -124,7 +146,7 @@ public ChatModel getFirstAvailableToolModel() {
124
146
125
147
return models .stream ()
126
148
.filter (filterModelConnectivityOnCapability ("TOOLS" ))
127
- .map (DefaultGenAILocator ::createChatModel )
149
+ .map (DefaultGenaiLocator ::createChatModel )
128
150
.findFirst ()
129
151
.orElseThrow (() -> new RuntimeException ("Unable to find first tool model" ));
130
152
}
@@ -136,7 +158,7 @@ public ChatModel getFirstAvailableToolModelByLabels(Map<String, String> labels)
136
158
return models .stream ()
137
159
.filter (filterModelConnectivityOnCapability ("TOOLS" ))
138
160
.filter (filterModelConnectivityOnLabels (labels ))
139
- .map (DefaultGenAILocator ::createChatModel )
161
+ .map (DefaultGenaiLocator ::createChatModel )
140
162
.findFirst ()
141
163
.orElseThrow (() -> new RuntimeException ("Unable to find first tool model" ));
142
164
}
@@ -156,7 +178,7 @@ public EmbeddingModel getEmbeddingModelByName(String name) {
156
178
return models .stream ()
157
179
.filter (filterModelConnectivityOnCapability ("EMBEDDING" ))
158
180
.filter (c -> c .name ().equals (name ))
159
- .map (DefaultGenAILocator ::createEmbeddingModel )
181
+ .map (DefaultGenaiLocator ::createEmbeddingModel )
160
182
.findFirst ()
161
183
.orElseThrow (
162
184
() -> new RuntimeException ("Unable to find embedding model with name '" + name + "'" ));
@@ -169,7 +191,7 @@ public List<EmbeddingModel> getEmbeddingModelsByLabels(Map<String, String> label
169
191
return models .stream ()
170
192
.filter (filterModelConnectivityOnCapability ("EMBEDDING" ))
171
193
.filter (filterModelConnectivityOnLabels (labels ))
172
- .map (DefaultGenAILocator ::createEmbeddingModel )
194
+ .map (DefaultGenaiLocator ::createEmbeddingModel )
173
195
.toList ();
174
196
}
175
197
@@ -179,7 +201,7 @@ public EmbeddingModel getFirstAvailableEmbeddingModel() {
179
201
180
202
return models .stream ()
181
203
.filter (filterModelConnectivityOnCapability ("EMBEDDING" ))
182
- .map (DefaultGenAILocator ::createEmbeddingModel )
204
+ .map (DefaultGenaiLocator ::createEmbeddingModel )
183
205
.findFirst ()
184
206
.orElseThrow (() -> new RuntimeException ("Unable to find first embedding model" ));
185
207
}
@@ -191,7 +213,7 @@ public EmbeddingModel getFirstAvailableEmbeddingModelByLabels(Map<String, String
191
213
return models .stream ()
192
214
.filter (filterModelConnectivityOnCapability ("EMBEDDING" ))
193
215
.filter (filterModelConnectivityOnLabels (labels ))
194
- .map (DefaultGenAILocator ::createEmbeddingModel )
216
+ .map (DefaultGenaiLocator ::createEmbeddingModel )
195
217
.findFirst ()
196
218
.orElseThrow (() -> new RuntimeException ("Unable to find first embedding model" ));
197
219
}
@@ -231,7 +253,7 @@ private List<McpConnectivity> getAllMcpConnectivityDetails() {
231
253
}
232
254
233
255
private ConfigEndpoint getEndpointConfig () {
234
- RestClient client = RestClient . builder () .build ();
256
+ RestClient client = builder .build ();
235
257
return client
236
258
.get ()
237
259
.uri (configUrl )
0 commit comments