36
36
@ Recorder
37
37
public class WatsonxRecorder {
38
38
39
- private static final String DUMMY_URL = "https://dummy.ai/api" ;
40
- private static final String DUMMY_API_KEY = "dummy" ;
41
39
private static final Map <String , WatsonxTokenGenerator > tokenGeneratorCache = new HashMap <>();
42
40
private static final ConfigValidationException .Problem [] EMPTY_PROBLEMS = new ConfigValidationException .Problem [0 ];
43
41
@@ -47,7 +45,7 @@ public Supplier<ChatLanguageModel> chatModel(LangChain4jWatsonxConfig runtimeCon
47
45
48
46
if (watsonRuntimeConfig .enableIntegration ()) {
49
47
50
- var builder = chatBuilder (watsonRuntimeConfig , configName );
48
+ var builder = chatBuilder (runtimeConfig , configName );
51
49
return new Supplier <>() {
52
50
@ Override
53
51
public ChatLanguageModel get () {
@@ -74,7 +72,7 @@ public Supplier<StreamingChatLanguageModel> streamingChatModel(LangChain4jWatson
74
72
75
73
if (watsonRuntimeConfig .enableIntegration ()) {
76
74
77
- var builder = chatBuilder (watsonRuntimeConfig , configName );
75
+ var builder = chatBuilder (runtimeConfig , configName );
78
76
return new Supplier <>() {
79
77
@ Override
80
78
public StreamingChatLanguageModel get () {
@@ -101,7 +99,7 @@ public Supplier<ChatLanguageModel> generationModel(LangChain4jWatsonxConfig runt
101
99
102
100
if (watsonRuntimeConfig .enableIntegration ()) {
103
101
104
- var builder = generationBuilder (watsonRuntimeConfig , configName );
102
+ var builder = generationBuilder (runtimeConfig , configName );
105
103
return new Supplier <>() {
106
104
@ Override
107
105
public ChatLanguageModel get () {
@@ -128,7 +126,7 @@ public Supplier<StreamingChatLanguageModel> generationStreamingModel(LangChain4j
128
126
129
127
if (watsonRuntimeConfig .enableIntegration ()) {
130
128
131
- var builder = generationBuilder (watsonRuntimeConfig , configName );
129
+ var builder = generationBuilder (runtimeConfig , configName );
132
130
return new Supplier <>() {
133
131
@ Override
134
132
public StreamingChatLanguageModel get () {
@@ -152,19 +150,20 @@ public Supplier<EmbeddingModel> embeddingModel(LangChain4jWatsonxConfig runtimeC
152
150
LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
153
151
154
152
if (watsonConfig .enableIntegration ()) {
155
- var configProblems = checkConfigurations (watsonConfig , configName );
153
+ var configProblems = checkConfigurations (runtimeConfig , configName );
156
154
157
155
if (!configProblems .isEmpty ()) {
158
156
throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
159
157
}
160
158
161
159
String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
162
160
WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
163
- createTokenGenerator (watsonConfig .iam (), watsonConfig .apiKey ()));
161
+ createTokenGenerator (watsonConfig .iam (),
162
+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
164
163
165
164
URL url ;
166
165
try {
167
- url = new URL (watsonConfig .baseUrl ());
166
+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
168
167
} catch (Exception e ) {
169
168
throw new RuntimeException (e );
170
169
}
@@ -177,8 +176,8 @@ public Supplier<EmbeddingModel> embeddingModel(LangChain4jWatsonxConfig runtimeC
177
176
.logRequests (firstOrDefault (false , embeddingModelConfig .logRequests (), watsonConfig .logRequests ()))
178
177
.logResponses (firstOrDefault (false , embeddingModelConfig .logResponses (), watsonConfig .logResponses ()))
179
178
.version (watsonConfig .version ())
180
- .spaceId (watsonConfig .spaceId (). orElse ( null ))
181
- .projectId (watsonConfig .projectId (). orElse ( null ))
179
+ .spaceId (firstOrDefault ( null , watsonConfig .spaceId (), runtimeConfig . defaultConfig (). spaceId () ))
180
+ .projectId (firstOrDefault ( null , watsonConfig .projectId (), runtimeConfig . defaultConfig (). projectId () ))
182
181
.modelId (embeddingModelConfig .modelId ())
183
182
.truncateInputTokens (embeddingModelConfig .truncateInputTokens ().orElse (null ));
184
183
@@ -204,19 +203,20 @@ public EmbeddingModel get() {
204
203
public Supplier <ScoringModel > scoringModel (LangChain4jWatsonxConfig runtimeConfig , String configName ) {
205
204
LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
206
205
207
- var configProblems = checkConfigurations (watsonConfig , configName );
206
+ var configProblems = checkConfigurations (runtimeConfig , configName );
208
207
209
208
if (!configProblems .isEmpty ()) {
210
209
throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
211
210
}
212
211
213
212
String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
214
213
WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
215
- createTokenGenerator (watsonConfig .iam (), watsonConfig .apiKey ()));
214
+ createTokenGenerator (watsonConfig .iam (),
215
+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
216
216
217
217
URL url ;
218
218
try {
219
- url = new URL (watsonConfig .baseUrl ());
219
+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
220
220
} catch (Exception e ) {
221
221
throw new RuntimeException (e );
222
222
}
@@ -229,8 +229,8 @@ public Supplier<ScoringModel> scoringModel(LangChain4jWatsonxConfig runtimeConfi
229
229
.logRequests (firstOrDefault (false , rerankModelConfig .logRequests (), watsonConfig .logRequests ()))
230
230
.logResponses (firstOrDefault (false , rerankModelConfig .logResponses (), watsonConfig .logResponses ()))
231
231
.version (watsonConfig .version ())
232
- .spaceId (watsonConfig .spaceId (). orElse ( null ))
233
- .projectId (watsonConfig .projectId (). orElse ( null ))
232
+ .spaceId (firstOrDefault ( null , watsonConfig .spaceId (), runtimeConfig . defaultConfig (). spaceId () ))
233
+ .projectId (firstOrDefault ( null , watsonConfig .projectId (), runtimeConfig . defaultConfig (). projectId () ))
234
234
.modelId (rerankModelConfig .modelId ())
235
235
.truncateInputTokens (rerankModelConfig .truncateInputTokens ().orElse (null ));
236
236
@@ -253,38 +253,38 @@ public WatsonxTokenGenerator apply(String iamUrl) {
253
253
};
254
254
}
255
255
256
- private WatsonxChatModel .Builder chatBuilder (
257
- LangChain4jWatsonxConfig .WatsonConfig watsonRuntimeConfig ,
258
- String configName ) {
256
+ private WatsonxChatModel .Builder chatBuilder (LangChain4jWatsonxConfig runtimeConfig , String configName ) {
257
+ LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
259
258
260
- ChatModelConfig chatModelConfig = watsonRuntimeConfig .chatModel ();
261
- var configProblems = checkConfigurations (watsonRuntimeConfig , configName );
259
+ var configProblems = checkConfigurations (runtimeConfig , configName );
262
260
263
261
if (!configProblems .isEmpty ()) {
264
262
throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
265
263
}
266
264
267
- String iamUrl = watsonRuntimeConfig .iam ().baseUrl ().toExternalForm ();
265
+ ChatModelConfig chatModelConfig = watsonConfig .chatModel ();
266
+ String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
268
267
WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
269
- createTokenGenerator (watsonRuntimeConfig .iam (), watsonRuntimeConfig .apiKey ()));
268
+ createTokenGenerator (watsonConfig .iam (),
269
+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
270
270
271
271
URL url ;
272
272
try {
273
- url = new URL (watsonRuntimeConfig .baseUrl ());
273
+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
274
274
} catch (Exception e ) {
275
275
throw new RuntimeException (e );
276
276
}
277
277
278
278
return WatsonxChatModel .builder ()
279
279
.tokenGenerator (tokenGenerator )
280
280
.url (url )
281
- .timeout (watsonRuntimeConfig .timeout ().orElse (Duration .ofSeconds (10 )))
282
- .logRequests (firstOrDefault (false , chatModelConfig .logRequests (), watsonRuntimeConfig .logRequests ()))
283
- .logResponses (firstOrDefault (false , chatModelConfig .logResponses (), watsonRuntimeConfig .logResponses ()))
284
- .version (watsonRuntimeConfig .version ())
285
- .spaceId (watsonRuntimeConfig .spaceId (). orElse ( null ))
286
- .projectId (watsonRuntimeConfig .projectId (). orElse ( null ))
287
- .modelId (watsonRuntimeConfig . generationModel ().modelId ())
281
+ .timeout (watsonConfig .timeout ().orElse (Duration .ofSeconds (10 )))
282
+ .logRequests (firstOrDefault (false , chatModelConfig .logRequests (), watsonConfig .logRequests ()))
283
+ .logResponses (firstOrDefault (false , chatModelConfig .logResponses (), watsonConfig .logResponses ()))
284
+ .version (watsonConfig .version ())
285
+ .spaceId (firstOrDefault ( null , watsonConfig .spaceId (), runtimeConfig . defaultConfig (). spaceId () ))
286
+ .projectId (firstOrDefault ( null , watsonConfig .projectId (), runtimeConfig . defaultConfig (). projectId () ))
287
+ .modelId (watsonConfig . chatModel ().modelId ())
288
288
.frequencyPenalty (chatModelConfig .frequencyPenalty ())
289
289
.logprobs (chatModelConfig .logprobs ())
290
290
.topLogprobs (chatModelConfig .topLogprobs ().orElse (null ))
@@ -296,24 +296,24 @@ private WatsonxChatModel.Builder chatBuilder(
296
296
.responseFormat (chatModelConfig .responseFormat ().orElse (null ));
297
297
}
298
298
299
- private WatsonxGenerationModel .Builder generationBuilder (
300
- LangChain4jWatsonxConfig .WatsonConfig watsonRuntimeConfig ,
301
- String configName ) {
299
+ private WatsonxGenerationModel .Builder generationBuilder (LangChain4jWatsonxConfig runtimeConfig , String configName ) {
300
+ LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
302
301
303
- GenerationModelConfig generationModelConfig = watsonRuntimeConfig .generationModel ();
304
- var configProblems = checkConfigurations (watsonRuntimeConfig , configName );
302
+ var configProblems = checkConfigurations (runtimeConfig , configName );
305
303
306
304
if (!configProblems .isEmpty ()) {
307
305
throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
308
306
}
309
307
310
- String iamUrl = watsonRuntimeConfig .iam ().baseUrl ().toExternalForm ();
308
+ GenerationModelConfig generationModelConfig = watsonConfig .generationModel ();
309
+ String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
311
310
WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
312
- createTokenGenerator (watsonRuntimeConfig .iam (), watsonRuntimeConfig .apiKey ()));
311
+ createTokenGenerator (watsonConfig .iam (),
312
+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
313
313
314
314
URL url ;
315
315
try {
316
- url = new URL (watsonRuntimeConfig .baseUrl ());
316
+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
317
317
} catch (Exception e ) {
318
318
throw new RuntimeException (e );
319
319
}
@@ -325,13 +325,13 @@ private WatsonxGenerationModel.Builder generationBuilder(
325
325
return WatsonxGenerationModel .builder ()
326
326
.tokenGenerator (tokenGenerator )
327
327
.url (url )
328
- .timeout (watsonRuntimeConfig .timeout ().orElse (Duration .ofSeconds (10 )))
329
- .logRequests (firstOrDefault (false , generationModelConfig .logRequests (), watsonRuntimeConfig .logRequests ()))
330
- .logResponses (firstOrDefault (false , generationModelConfig .logResponses (), watsonRuntimeConfig .logResponses ()))
331
- .version (watsonRuntimeConfig .version ())
332
- .spaceId (watsonRuntimeConfig .spaceId (). orElse ( null ))
333
- .projectId (watsonRuntimeConfig .projectId (). orElse ( null ))
334
- .modelId (watsonRuntimeConfig .generationModel ().modelId ())
328
+ .timeout (watsonConfig .timeout ().orElse (Duration .ofSeconds (10 )))
329
+ .logRequests (firstOrDefault (false , generationModelConfig .logRequests (), watsonConfig .logRequests ()))
330
+ .logResponses (firstOrDefault (false , generationModelConfig .logResponses (), watsonConfig .logResponses ()))
331
+ .version (watsonConfig .version ())
332
+ .spaceId (firstOrDefault ( null , watsonConfig .spaceId (), runtimeConfig . defaultConfig (). spaceId () ))
333
+ .projectId (firstOrDefault ( null , watsonConfig .projectId (), runtimeConfig . defaultConfig (). projectId () ))
334
+ .modelId (watsonConfig .generationModel ().modelId ())
335
335
.decodingMethod (generationModelConfig .decodingMethod ())
336
336
.decayFactor (decayFactor )
337
337
.startIndex (startIndex )
@@ -359,20 +359,21 @@ private LangChain4jWatsonxConfig.WatsonConfig correspondingWatsonRuntimeConfig(L
359
359
return watsonConfig ;
360
360
}
361
361
362
- private List <ConfigValidationException .Problem > checkConfigurations (LangChain4jWatsonxConfig . WatsonConfig watsonConfig ,
362
+ private List <ConfigValidationException .Problem > checkConfigurations (LangChain4jWatsonxConfig runtimeConfig ,
363
363
String configName ) {
364
364
List <ConfigValidationException .Problem > configProblems = new ArrayList <>();
365
+ LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
365
366
366
- if (DUMMY_URL . equals ( watsonConfig . baseUrl ())) {
367
+ if (watsonConfig . baseUrl (). isEmpty () && runtimeConfig . defaultConfig (). baseUrl (). isEmpty ( )) {
367
368
configProblems .add (createBaseURLConfigProblem (configName ));
368
369
}
369
- String apiKey = watsonConfig .apiKey ();
370
- if (DUMMY_API_KEY .equals (apiKey )) {
370
+ if (watsonConfig .apiKey ().isEmpty () && runtimeConfig .defaultConfig ().apiKey ().isEmpty ()) {
371
371
configProblems .add (createApiKeyConfigProblem (configName ));
372
372
}
373
- if (watsonConfig .projectId ().isEmpty () && watsonConfig .spaceId ().isEmpty ()) {
373
+ if (watsonConfig .projectId ().isEmpty () && runtimeConfig .defaultConfig ().projectId ().isEmpty () &&
374
+ watsonConfig .spaceId ().isEmpty () && runtimeConfig .defaultConfig ().spaceId ().isEmpty ()) {
374
375
var config = NamedConfigUtil .isDefault (configName ) ? "." : ("." + configName + "." );
375
- var errorMessage = "One of the two properties quarkus.langchain4j.watsonx%s%s / quarkus.langchain4j.watsonx%s%s is required, but could not be found in any config source" ;
376
+ var errorMessage = "One of the properties quarkus.langchain4j.watsonx%s%s / quarkus.langchain4j.watsonx%s%s is required, but could not be found in any config source" ;
376
377
configProblems .add (new ConfigValidationException .Problem (
377
378
String .format (errorMessage , config , "project-id" , config , "space-id" )));
378
379
}
0 commit comments