3636@ Recorder
3737public class WatsonxRecorder {
3838
39- private static final String DUMMY_URL = "https://dummy.ai/api" ;
40- private static final String DUMMY_API_KEY = "dummy" ;
4139 private static final Map <String , WatsonxTokenGenerator > tokenGeneratorCache = new HashMap <>();
4240 private static final ConfigValidationException .Problem [] EMPTY_PROBLEMS = new ConfigValidationException .Problem [0 ];
4341
@@ -47,7 +45,7 @@ public Supplier<ChatLanguageModel> chatModel(LangChain4jWatsonxConfig runtimeCon
4745
4846 if (watsonRuntimeConfig .enableIntegration ()) {
4947
50- var builder = chatBuilder (watsonRuntimeConfig , configName );
48+ var builder = chatBuilder (runtimeConfig , configName );
5149 return new Supplier <>() {
5250 @ Override
5351 public ChatLanguageModel get () {
@@ -74,7 +72,7 @@ public Supplier<StreamingChatLanguageModel> streamingChatModel(LangChain4jWatson
7472
7573 if (watsonRuntimeConfig .enableIntegration ()) {
7674
77- var builder = chatBuilder (watsonRuntimeConfig , configName );
75+ var builder = chatBuilder (runtimeConfig , configName );
7876 return new Supplier <>() {
7977 @ Override
8078 public StreamingChatLanguageModel get () {
@@ -101,7 +99,7 @@ public Supplier<ChatLanguageModel> generationModel(LangChain4jWatsonxConfig runt
10199
102100 if (watsonRuntimeConfig .enableIntegration ()) {
103101
104- var builder = generationBuilder (watsonRuntimeConfig , configName );
102+ var builder = generationBuilder (runtimeConfig , configName );
105103 return new Supplier <>() {
106104 @ Override
107105 public ChatLanguageModel get () {
@@ -128,7 +126,7 @@ public Supplier<StreamingChatLanguageModel> generationStreamingModel(LangChain4j
128126
129127 if (watsonRuntimeConfig .enableIntegration ()) {
130128
131- var builder = generationBuilder (watsonRuntimeConfig , configName );
129+ var builder = generationBuilder (runtimeConfig , configName );
132130 return new Supplier <>() {
133131 @ Override
134132 public StreamingChatLanguageModel get () {
@@ -152,19 +150,20 @@ public Supplier<EmbeddingModel> embeddingModel(LangChain4jWatsonxConfig runtimeC
152150 LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
153151
154152 if (watsonConfig .enableIntegration ()) {
155- var configProblems = checkConfigurations (watsonConfig , configName );
153+ var configProblems = checkConfigurations (runtimeConfig , configName );
156154
157155 if (!configProblems .isEmpty ()) {
158156 throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
159157 }
160158
161159 String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
162160 WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
163- createTokenGenerator (watsonConfig .iam (), watsonConfig .apiKey ()));
161+ createTokenGenerator (watsonConfig .iam (),
162+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
164163
165164 URL url ;
166165 try {
167- url = new URL (watsonConfig .baseUrl ());
166+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
168167 } catch (Exception e ) {
169168 throw new RuntimeException (e );
170169 }
@@ -177,8 +176,8 @@ public Supplier<EmbeddingModel> embeddingModel(LangChain4jWatsonxConfig runtimeC
177176 .logRequests (firstOrDefault (false , embeddingModelConfig .logRequests (), watsonConfig .logRequests ()))
178177 .logResponses (firstOrDefault (false , embeddingModelConfig .logResponses (), watsonConfig .logResponses ()))
179178 .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 () ))
182181 .modelId (embeddingModelConfig .modelId ())
183182 .truncateInputTokens (embeddingModelConfig .truncateInputTokens ().orElse (null ));
184183
@@ -204,19 +203,20 @@ public EmbeddingModel get() {
204203 public Supplier <ScoringModel > scoringModel (LangChain4jWatsonxConfig runtimeConfig , String configName ) {
205204 LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
206205
207- var configProblems = checkConfigurations (watsonConfig , configName );
206+ var configProblems = checkConfigurations (runtimeConfig , configName );
208207
209208 if (!configProblems .isEmpty ()) {
210209 throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
211210 }
212211
213212 String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
214213 WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
215- createTokenGenerator (watsonConfig .iam (), watsonConfig .apiKey ()));
214+ createTokenGenerator (watsonConfig .iam (),
215+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
216216
217217 URL url ;
218218 try {
219- url = new URL (watsonConfig .baseUrl ());
219+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
220220 } catch (Exception e ) {
221221 throw new RuntimeException (e );
222222 }
@@ -229,8 +229,8 @@ public Supplier<ScoringModel> scoringModel(LangChain4jWatsonxConfig runtimeConfi
229229 .logRequests (firstOrDefault (false , rerankModelConfig .logRequests (), watsonConfig .logRequests ()))
230230 .logResponses (firstOrDefault (false , rerankModelConfig .logResponses (), watsonConfig .logResponses ()))
231231 .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 () ))
234234 .modelId (rerankModelConfig .modelId ())
235235 .truncateInputTokens (rerankModelConfig .truncateInputTokens ().orElse (null ));
236236
@@ -253,38 +253,38 @@ public WatsonxTokenGenerator apply(String iamUrl) {
253253 };
254254 }
255255
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 );
259258
260- ChatModelConfig chatModelConfig = watsonRuntimeConfig .chatModel ();
261- var configProblems = checkConfigurations (watsonRuntimeConfig , configName );
259+ var configProblems = checkConfigurations (runtimeConfig , configName );
262260
263261 if (!configProblems .isEmpty ()) {
264262 throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
265263 }
266264
267- String iamUrl = watsonRuntimeConfig .iam ().baseUrl ().toExternalForm ();
265+ ChatModelConfig chatModelConfig = watsonConfig .chatModel ();
266+ String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
268267 WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
269- createTokenGenerator (watsonRuntimeConfig .iam (), watsonRuntimeConfig .apiKey ()));
268+ createTokenGenerator (watsonConfig .iam (),
269+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
270270
271271 URL url ;
272272 try {
273- url = new URL (watsonRuntimeConfig .baseUrl ());
273+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
274274 } catch (Exception e ) {
275275 throw new RuntimeException (e );
276276 }
277277
278278 return WatsonxChatModel .builder ()
279279 .tokenGenerator (tokenGenerator )
280280 .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 ())
288288 .frequencyPenalty (chatModelConfig .frequencyPenalty ())
289289 .logprobs (chatModelConfig .logprobs ())
290290 .topLogprobs (chatModelConfig .topLogprobs ().orElse (null ))
@@ -296,24 +296,24 @@ private WatsonxChatModel.Builder chatBuilder(
296296 .responseFormat (chatModelConfig .responseFormat ().orElse (null ));
297297 }
298298
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 );
302301
303- GenerationModelConfig generationModelConfig = watsonRuntimeConfig .generationModel ();
304- var configProblems = checkConfigurations (watsonRuntimeConfig , configName );
302+ var configProblems = checkConfigurations (runtimeConfig , configName );
305303
306304 if (!configProblems .isEmpty ()) {
307305 throw new ConfigValidationException (configProblems .toArray (EMPTY_PROBLEMS ));
308306 }
309307
310- String iamUrl = watsonRuntimeConfig .iam ().baseUrl ().toExternalForm ();
308+ GenerationModelConfig generationModelConfig = watsonConfig .generationModel ();
309+ String iamUrl = watsonConfig .iam ().baseUrl ().toExternalForm ();
311310 WatsonxTokenGenerator tokenGenerator = tokenGeneratorCache .computeIfAbsent (iamUrl ,
312- createTokenGenerator (watsonRuntimeConfig .iam (), watsonRuntimeConfig .apiKey ()));
311+ createTokenGenerator (watsonConfig .iam (),
312+ firstOrDefault (null , watsonConfig .apiKey (), runtimeConfig .defaultConfig ().apiKey ())));
313313
314314 URL url ;
315315 try {
316- url = new URL (watsonRuntimeConfig .baseUrl ());
316+ url = new URL (firstOrDefault ( null , watsonConfig .baseUrl (), runtimeConfig . defaultConfig (). baseUrl () ));
317317 } catch (Exception e ) {
318318 throw new RuntimeException (e );
319319 }
@@ -325,13 +325,13 @@ private WatsonxGenerationModel.Builder generationBuilder(
325325 return WatsonxGenerationModel .builder ()
326326 .tokenGenerator (tokenGenerator )
327327 .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 ())
335335 .decodingMethod (generationModelConfig .decodingMethod ())
336336 .decayFactor (decayFactor )
337337 .startIndex (startIndex )
@@ -359,20 +359,21 @@ private LangChain4jWatsonxConfig.WatsonConfig correspondingWatsonRuntimeConfig(L
359359 return watsonConfig ;
360360 }
361361
362- private List <ConfigValidationException .Problem > checkConfigurations (LangChain4jWatsonxConfig . WatsonConfig watsonConfig ,
362+ private List <ConfigValidationException .Problem > checkConfigurations (LangChain4jWatsonxConfig runtimeConfig ,
363363 String configName ) {
364364 List <ConfigValidationException .Problem > configProblems = new ArrayList <>();
365+ LangChain4jWatsonxConfig .WatsonConfig watsonConfig = correspondingWatsonRuntimeConfig (runtimeConfig , configName );
365366
366- if (DUMMY_URL . equals ( watsonConfig . baseUrl ())) {
367+ if (watsonConfig . baseUrl (). isEmpty () && runtimeConfig . defaultConfig (). baseUrl (). isEmpty ( )) {
367368 configProblems .add (createBaseURLConfigProblem (configName ));
368369 }
369- String apiKey = watsonConfig .apiKey ();
370- if (DUMMY_API_KEY .equals (apiKey )) {
370+ if (watsonConfig .apiKey ().isEmpty () && runtimeConfig .defaultConfig ().apiKey ().isEmpty ()) {
371371 configProblems .add (createApiKeyConfigProblem (configName ));
372372 }
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 ()) {
374375 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" ;
376377 configProblems .add (new ConfigValidationException .Problem (
377378 String .format (errorMessage , config , "project-id" , config , "space-id" )));
378379 }
0 commit comments