-
Notifications
You must be signed in to change notification settings - Fork 479
fix(dotAI): Dot AI LangChain4J - ProviderConfig fixes #35426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6b94e4f
feat(dotAI): consolidate config into single providerConfig JSON with …
ihoffmann-dot 99944b6
fix(dotAI): restore original YAML description, remove redundant field…
ihoffmann-dot 12dbb1f
fix(dotAI): set providerConfig as visible field (hidden: false)
ihoffmann-dot 1302acd
fix(dotAI): disable allowExtraParameters to remove Custom Properties …
ihoffmann-dot 5805599
temp: revert allowExtraParameters to true for cleanup
ihoffmann-dot 1f1f494
feat(ai): multi-model fallback via comma-separated model field
ihoffmann-dot 5cb58a2
refactor(ai): remove PUT config endpoint and ProviderConfigMerger
ihoffmann-dot d010f7f
refactor(ai): extract executeWithFallback helper in LangChain4jAIClient
ihoffmann-dot 5c73e87
fix(postman): update AI collection for providerConfig consolidation
ihoffmann-dot b6321ca
fix(ai): move listenerIndexer into providerConfig in AiTest setup
ihoffmann-dot 9985b5b
Merge branch 'main' into dot-ai-langchain-fixes
ihoffmann-dot 7dcb94f
feat(ai): add separate apiKey SECRET field to hide credentials in App…
ihoffmann-dot 533e62b
fix(ai): correct apiKey field type to STRING with hidden:true in dotA…
ihoffmann-dot 6ed266f
refactor(ai): address PR review comments on LangChain4jAIClient and P…
ihoffmann-dot 42ee39e
feat(ai): auto-route maxTokens to max_completion_tokens for OpenAI re…
ihoffmann-dot cb95a36
docs(ai): update dotAI.yml description to reference OpenAI instead of…
ihoffmann-dot b1c3404
revert(ai): keep model() as @Nullable String in ProviderConfig
ihoffmann-dot 615a4f8
fix(ai): flush SSE chunks, cancelled flag on IOException, maxRetries …
ihoffmann-dot ac9244d
fix(ai): null check in parseSection, deepCopy in injectApiKeyIntoSect…
ihoffmann-dot 24baed2
Merge branch 'main' into dot-ai-langchain-fixes
ihoffmann-dot de7a43a
fix(ai): immutable allModels(), fallback tests, self-import, javadoc,…
ihoffmann-dot eb277be
fix(ai): correct dotAI.yml hint — apiKey must not be in providerConfig
ihoffmann-dot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.dotcms.ai.client.langchain4j; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.dotcms.ai.AiKeys; | ||
| import com.dotcms.ai.app.AIModelType; | ||
| import com.dotcms.ai.app.AppConfig; | ||
|
|
@@ -32,7 +33,6 @@ | |
| import dev.langchain4j.model.embedding.EmbeddingModel; | ||
| import dev.langchain4j.model.image.ImageModel; | ||
| import dev.langchain4j.model.output.FinishReason; | ||
| import dev.langchain4j.model.output.Response; | ||
| import dev.langchain4j.model.output.TokenUsage; | ||
| import io.vavr.Lazy; | ||
|
|
||
|
|
@@ -42,6 +42,7 @@ | |
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
@@ -132,7 +133,7 @@ public <T extends Serializable> void sendRequest(final AIRequest<T> request, fin | |
| throw new DotAIAppConfigDisabledException("App dotAI config is not enabled — set providerConfig"); | ||
| } | ||
|
|
||
| final String providerConfigJson = appConfig.getProviderConfig(); | ||
| final String providerConfigJson = appConfig.getResolvedProviderConfig(); | ||
| final AIModelType type = jsonRequest.getType(); | ||
| final JSONObject payload = jsonRequest.getPayload(); | ||
|
|
||
|
|
@@ -153,46 +154,67 @@ public <T extends Serializable> void sendRequest(final AIRequest<T> request, fin | |
| } | ||
|
|
||
| private String executeChatRequest(final String cacheKeyPrefix, final String providerConfigJson, final JSONObject payload) { | ||
| final ChatModel model; | ||
| try { | ||
| model = chatModelCache.get( | ||
| cacheKeyPrefix + ":chat", | ||
| () -> LangChain4jModelFactory.buildChatModel(parseSection(providerConfigJson, "chat"))); | ||
| } catch (ExecutionException | UncheckedExecutionException e) { | ||
| final Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| throw new IllegalArgumentException("Failed to initialize chat model: " + cause.getMessage(), cause); | ||
| } | ||
|
|
||
| final ProviderConfig baseConfig = parseSection(providerConfigJson, "chat"); | ||
| final List<ChatMessage> messages = toMessages(payload.optJSONArray(AiKeys.MESSAGES)); | ||
| if (messages.isEmpty()) { | ||
| throw new IllegalArgumentException("Chat request must contain at least one message"); | ||
| } | ||
|
|
||
| final ChatResponse response = model.chat( | ||
| ChatRequest.builder().messages(messages).build()); | ||
| return toChatResponseJson(response); | ||
| return executeWithFallback(cacheKeyPrefix, "chat", baseConfig, chatModelCache, | ||
| LangChain4jModelFactory::buildChatModel, | ||
| model -> toChatResponseJson(model.chat(ChatRequest.builder().messages(messages).build()))); | ||
| } | ||
|
|
||
| private void executeStreamingChatRequest(final String cacheKeyPrefix, | ||
| final String providerConfigJson, | ||
| final JSONObject payload, | ||
| final OutputStream output) { | ||
| final StreamingChatModel model; | ||
| try { | ||
| model = streamingChatModelCache.get( | ||
| cacheKeyPrefix + ":chat:streaming", | ||
| () -> LangChain4jModelFactory.buildStreamingChatModel(parseSection(providerConfigJson, "chat"))); | ||
| } catch (ExecutionException | UncheckedExecutionException e) { | ||
| final Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| throw new IllegalArgumentException("Failed to initialize streaming chat model: " + cause.getMessage(), cause); | ||
| final ProviderConfig baseConfig = parseSection(providerConfigJson, "chat"); | ||
| final List<String> models = baseConfig.allModels(); | ||
| if (models.isEmpty()) { | ||
| throw new IllegalArgumentException("No model configured in providerConfig.chat — set 'model'"); | ||
| } | ||
|
|
||
| final List<ChatMessage> messages = toMessages(payload.optJSONArray(AiKeys.MESSAGES)); | ||
| if (messages.isEmpty()) { | ||
| throw new IllegalArgumentException("Chat request must contain at least one message"); | ||
| } | ||
|
|
||
| final StreamingChatModel model = initStreamingModel(cacheKeyPrefix, baseConfig, models); | ||
| streamWithModel(model, messages, output); | ||
| } | ||
|
|
||
| // Fallback is only possible before streaming starts — once bytes are written to output | ||
| // we cannot retry. Each init failure is logged immediately; the last exception is | ||
| // rethrown only after all configured fallback models have been attempted. | ||
| private StreamingChatModel initStreamingModel( | ||
| final String cacheKeyPrefix, | ||
| final ProviderConfig baseConfig, | ||
| final List<String> models) { | ||
| RuntimeException lastException = null; | ||
| for (final String modelName : models) { | ||
| try { | ||
| final ProviderConfig modelConfig = ImmutableProviderConfig.copyOf(baseConfig).withModel(modelName); | ||
| return streamingChatModelCache.get( | ||
| cacheKeyPrefix + ":chat:streaming:" + modelName, | ||
| () -> LangChain4jModelFactory.buildStreamingChatModel(modelConfig)); | ||
| } catch (ExecutionException | UncheckedExecutionException e) { | ||
| final Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| lastException = new IllegalArgumentException( | ||
| "Failed to initialize streaming model '" + modelName + "': " + cause.getMessage(), cause); | ||
| Logger.warn(LangChain4jAIClient.class, | ||
| "Streaming model '" + modelName + "' init failed: " + cause.getMessage() | ||
| + (models.size() > 1 ? " — trying next model" : "")); | ||
| } | ||
| } | ||
| throw lastException != null ? lastException | ||
| : new IllegalArgumentException("All configured streaming chat models exhausted"); | ||
| } | ||
|
|
||
| private void streamWithModel(final StreamingChatModel model, | ||
| final List<ChatMessage> messages, | ||
| final OutputStream output) { | ||
| final ChatRequest chatRequest = ChatRequest.builder().messages(messages).build(); | ||
| final long start = System.currentTimeMillis(); | ||
|
|
||
| final CountDownLatch latch = new CountDownLatch(1); | ||
| final AtomicReference<Throwable> error = new AtomicReference<>(); | ||
|
|
@@ -206,7 +228,9 @@ public void onPartialResponse(final String token) { | |
| } | ||
| try { | ||
| output.write(toSseChunk(token).getBytes(StandardCharsets.UTF_8)); | ||
| output.flush(); | ||
| } catch (IOException e) { | ||
| cancelled.set(true); | ||
| error.set(e); | ||
| latch.countDown(); | ||
| } | ||
|
|
@@ -238,6 +262,8 @@ public void onError(final Throwable e) { | |
| "Streaming timed out after " + STREAMING_TIMEOUT_SECONDS + " seconds", | ||
| new java.util.concurrent.TimeoutException()); | ||
| } | ||
| Logger.info(LangChain4jAIClient.class, | ||
| "Streaming chat completed in " + (System.currentTimeMillis() - start) + "ms"); | ||
| } catch (InterruptedException e) { | ||
| cancelled.set(true); | ||
| Thread.currentThread().interrupt(); | ||
|
|
@@ -273,35 +299,65 @@ private void writeToOutput(final String responseJson, final OutputStream output) | |
| } | ||
|
|
||
| private String executeEmbeddingRequest(final String cacheKeyPrefix, final String providerConfigJson, final JSONObject payload) { | ||
| final EmbeddingModel model; | ||
| try { | ||
| model = embeddingModelCache.get( | ||
| cacheKeyPrefix + ":embeddings", | ||
| () -> LangChain4jModelFactory.buildEmbeddingModel(parseSection(providerConfigJson, "embeddings"))); | ||
| } catch (ExecutionException | UncheckedExecutionException e) { | ||
| final Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| throw new IllegalArgumentException("Failed to initialize embedding model: " + cause.getMessage(), cause); | ||
| } | ||
|
|
||
| final ProviderConfig baseConfig = parseSection(providerConfigJson, "embeddings"); | ||
| final String input = payload.getString(AiKeys.INPUT); | ||
| final Response<Embedding> response = model.embed(TextSegment.from(input)); | ||
| return toEmbeddingResponseJson(response.content()); | ||
| return executeWithFallback(cacheKeyPrefix, "embeddings", baseConfig, embeddingModelCache, | ||
| LangChain4jModelFactory::buildEmbeddingModel, | ||
| model -> toEmbeddingResponseJson(model.embed(TextSegment.from(input)).content())); | ||
| } | ||
|
|
||
| private String executeImageRequest(final String cacheKeyPrefix, final String providerConfigJson, final JSONObject payload) { | ||
| final ImageModel model; | ||
| try { | ||
| model = imageModelCache.get( | ||
| cacheKeyPrefix + ":image", | ||
| () -> LangChain4jModelFactory.buildImageModel(parseSection(providerConfigJson, "image"))); | ||
| } catch (ExecutionException | UncheckedExecutionException e) { | ||
| final Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| throw new IllegalArgumentException("Failed to initialize image model: " + cause.getMessage(), cause); | ||
| } | ||
|
|
||
| final ProviderConfig baseConfig = parseSection(providerConfigJson, "image"); | ||
| final String prompt = payload.getString(AiKeys.PROMPT); | ||
| final Response<Image> response = model.generate(prompt); | ||
| return toImageResponseJson(response.content()); | ||
| return executeWithFallback(cacheKeyPrefix, "image", baseConfig, imageModelCache, | ||
| LangChain4jModelFactory::buildImageModel, | ||
| model -> toImageResponseJson(model.generate(prompt).content())); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| <M> String executeWithFallback( | ||
| final String cacheKeyPrefix, | ||
| final String section, | ||
| final ProviderConfig baseConfig, | ||
| final Cache<String, M> modelCache, | ||
| final Function<ProviderConfig, M> modelBuilder, | ||
| final Function<M, String> executor) { | ||
| final List<String> models = baseConfig.allModels(); | ||
| if (models.isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
| "No model configured in providerConfig." + section + " — set 'model'"); | ||
| } | ||
| // Each failure is logged immediately. The last exception is rethrown only after | ||
| // all configured fallback models have been attempted. | ||
| RuntimeException lastException = null; | ||
|
dario-daza marked this conversation as resolved.
|
||
| for (final String modelName : models) { | ||
| try { | ||
| final ProviderConfig modelConfig = ImmutableProviderConfig.copyOf(baseConfig).withModel(modelName); | ||
| final M model = modelCache.get( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for instance this method is easier to read |
||
| cacheKeyPrefix + ":" + section + ":" + modelName, | ||
| () -> modelBuilder.apply(modelConfig)); | ||
| final long start = System.currentTimeMillis(); | ||
| final String result = executor.apply(model); | ||
| Logger.info(LangChain4jAIClient.class, | ||
| section + " model '" + modelName + "' responded in " | ||
| + (System.currentTimeMillis() - start) + "ms"); | ||
| return result; | ||
| } catch (ExecutionException | UncheckedExecutionException e) { | ||
| final Throwable cause = e.getCause() != null ? e.getCause() : e; | ||
| lastException = new IllegalArgumentException( | ||
| "Failed to initialize " + section + " model '" + modelName + "': " + cause.getMessage(), cause); | ||
| Logger.warn(LangChain4jAIClient.class, | ||
| section + " model '" + modelName + "' init failed: " + cause.getMessage() | ||
| + (models.size() > 1 ? " — trying next model" : "")); | ||
| } catch (RuntimeException e) { | ||
| lastException = e; | ||
| Logger.warn(LangChain4jAIClient.class, | ||
| section + " model '" + modelName + "' failed: " + e.getMessage() | ||
| + (models.size() > 1 ? " — trying next model" : "")); | ||
| } | ||
| } | ||
| throw lastException != null ? lastException | ||
| : new IllegalArgumentException("All configured " + section + " models exhausted"); | ||
| } | ||
|
|
||
| static List<ChatMessage> toMessages(final JSONArray messagesArray) { | ||
|
|
@@ -395,6 +451,9 @@ static String toImageResponseJson(final Image image) { | |
| } | ||
|
|
||
| private static ProviderConfig parseSection(final String providerConfigJson, final String section) { | ||
| if (providerConfigJson == null) { | ||
| throw new IllegalArgumentException("providerConfig is null — app config is not enabled"); | ||
| } | ||
| try { | ||
| final JsonNode root = MAPPER.readTree(providerConfigJson); | ||
| final JsonNode sectionNode = root.get(section); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.