Skip to content

Commit

Permalink
Merge pull request #1117 from jmartisk/issue-1095
Browse files Browse the repository at this point in the history
Integrate Mistral AI moderation models
  • Loading branch information
geoand authored Nov 28, 2024
2 parents 173990e + 1386b01 commit 6209bee
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 4 deletions.
119 changes: 119 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-langchain4j-mistral-ai.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ endif::add-copy-button-to-env-var[]
|boolean
|`true`

a|icon:lock[title=Fixed at build time] [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-enabled]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-enabled[`quarkus.langchain4j.mistralai.moderation-model.enabled`]##

[.description]
--
Whether the model should be enabled


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_ENABLED+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_ENABLED+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`true`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-base-url]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-base-url[`quarkus.langchain4j.mistralai.base-url`]##

[.description]
Expand Down Expand Up @@ -286,6 +303,57 @@ endif::add-copy-button-to-env-var[]
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-model-name]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-model-name[`quarkus.langchain4j.mistralai.moderation-model.model-name`]##

[.description]
--
Model name to use


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_MODEL_NAME+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_MODEL_NAME+++`
endif::add-copy-button-to-env-var[]
--
|string
|`mistral-moderation-latest`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-requests[`quarkus.langchain4j.mistralai.moderation-model.log-requests`]##

[.description]
--
Whether moderation model requests should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_REQUESTS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_REQUESTS+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-responses]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-responses[`quarkus.langchain4j.mistralai.moderation-model.log-responses`]##

[.description]
--
Whether moderation model responses should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_RESPONSES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_RESPONSES+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-log-requests[`quarkus.langchain4j.mistralai.log-requests`]##

[.description]
Expand Down Expand Up @@ -585,6 +653,57 @@ endif::add-copy-button-to-env-var[]
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-model-name]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-model-name[`quarkus.langchain4j.mistralai."model-name".moderation-model.model-name`]##

[.description]
--
Model name to use


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_MODEL_NAME+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_MODEL_NAME+++`
endif::add-copy-button-to-env-var[]
--
|string
|`mistral-moderation-latest`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-requests[`quarkus.langchain4j.mistralai."model-name".moderation-model.log-requests`]##

[.description]
--
Whether moderation model requests should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_REQUESTS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_REQUESTS+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-responses]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-responses[`quarkus.langchain4j.mistralai."model-name".moderation-model.log-responses`]##

[.description]
--
Whether moderation model responses should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_RESPONSES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_RESPONSES+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-log-requests[`quarkus.langchain4j.mistralai."model-name".log-requests`]##

[.description]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ endif::add-copy-button-to-env-var[]
|boolean
|`true`

a|icon:lock[title=Fixed at build time] [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-enabled]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-enabled[`quarkus.langchain4j.mistralai.moderation-model.enabled`]##

[.description]
--
Whether the model should be enabled


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_ENABLED+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_ENABLED+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`true`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-base-url]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-base-url[`quarkus.langchain4j.mistralai.base-url`]##

[.description]
Expand Down Expand Up @@ -286,6 +303,57 @@ endif::add-copy-button-to-env-var[]
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-model-name]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-model-name[`quarkus.langchain4j.mistralai.moderation-model.model-name`]##

[.description]
--
Model name to use


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_MODEL_NAME+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_MODEL_NAME+++`
endif::add-copy-button-to-env-var[]
--
|string
|`mistral-moderation-latest`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-requests[`quarkus.langchain4j.mistralai.moderation-model.log-requests`]##

[.description]
--
Whether moderation model requests should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_REQUESTS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_REQUESTS+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-responses]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-moderation-model-log-responses[`quarkus.langchain4j.mistralai.moderation-model.log-responses`]##

[.description]
--
Whether moderation model responses should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_RESPONSES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI_MODERATION_MODEL_LOG_RESPONSES+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-log-requests[`quarkus.langchain4j.mistralai.log-requests`]##

[.description]
Expand Down Expand Up @@ -585,6 +653,57 @@ endif::add-copy-button-to-env-var[]
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-model-name]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-model-name[`quarkus.langchain4j.mistralai."model-name".moderation-model.model-name`]##

[.description]
--
Model name to use


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_MODEL_NAME+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_MODEL_NAME+++`
endif::add-copy-button-to-env-var[]
--
|string
|`mistral-moderation-latest`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-requests[`quarkus.langchain4j.mistralai."model-name".moderation-model.log-requests`]##

[.description]
--
Whether moderation model requests should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_REQUESTS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_REQUESTS+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-responses]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-moderation-model-log-responses[`quarkus.langchain4j.mistralai."model-name".moderation-model.log-responses`]##

[.description]
--
Whether moderation model responses should be logged


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_RESPONSES+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_LANGCHAIN4J_MISTRALAI__MODEL_NAME__MODERATION_MODEL_LOG_RESPONSES+++`
endif::add-copy-button-to-env-var[]
--
|boolean
|`false`

a| [[quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-log-requests]] [.property-path]##link:#quarkus-langchain4j-mistral-ai_quarkus-langchain4j-mistralai-model-name-log-requests[`quarkus.langchain4j.mistralai."model-name".log-requests`]##

[.description]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public interface LangChain4jMistralAiBuildConfig {
*/
EmbeddingModelBuildConfig embeddingModel();

/**
* Moderation model related settings
*/
ModerationModelBuildConfig moderationModel();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.quarkiverse.langchain4j.deployment.LangChain4jDotNames.CHAT_MODEL;
import static io.quarkiverse.langchain4j.deployment.LangChain4jDotNames.EMBEDDING_MODEL;
import static io.quarkiverse.langchain4j.deployment.LangChain4jDotNames.MODERATION_MODEL;
import static io.quarkiverse.langchain4j.deployment.LangChain4jDotNames.STREAMING_CHAT_MODEL;

import java.util.List;
Expand All @@ -15,8 +16,10 @@
import io.quarkiverse.langchain4j.ModelName;
import io.quarkiverse.langchain4j.deployment.items.ChatModelProviderCandidateBuildItem;
import io.quarkiverse.langchain4j.deployment.items.EmbeddingModelProviderCandidateBuildItem;
import io.quarkiverse.langchain4j.deployment.items.ModerationModelProviderCandidateBuildItem;
import io.quarkiverse.langchain4j.deployment.items.SelectedChatModelProviderBuildItem;
import io.quarkiverse.langchain4j.deployment.items.SelectedEmbeddingModelCandidateBuildItem;
import io.quarkiverse.langchain4j.deployment.items.SelectedModerationModelProviderBuildItem;
import io.quarkiverse.langchain4j.mistralai.runtime.MistralAiRecorder;
import io.quarkiverse.langchain4j.mistralai.runtime.config.LangChain4jMistralAiConfig;
import io.quarkiverse.langchain4j.runtime.NamedConfigUtil;
Expand Down Expand Up @@ -52,13 +55,17 @@ void nativeSupport(BuildProducer<ReflectiveClassBuildItem> reflectiveClassProduc
@BuildStep
public void providerCandidates(BuildProducer<ChatModelProviderCandidateBuildItem> chatProducer,
BuildProducer<EmbeddingModelProviderCandidateBuildItem> embeddingProducer,
BuildProducer<ModerationModelProviderCandidateBuildItem> moderationProducer,
LangChain4jMistralAiBuildConfig config) {
if (config.chatModel().enabled().isEmpty() || config.chatModel().enabled().get()) {
chatProducer.produce(new ChatModelProviderCandidateBuildItem(PROVIDER));
}
if (config.embeddingModel().enabled().isEmpty() || config.embeddingModel().enabled().get()) {
embeddingProducer.produce(new EmbeddingModelProviderCandidateBuildItem(PROVIDER));
}
if (config.moderationModel().enabled().isEmpty() || config.moderationModel().enabled().get()) {
moderationProducer.produce(new ModerationModelProviderCandidateBuildItem(PROVIDER));
}
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
Expand All @@ -67,6 +74,7 @@ public void providerCandidates(BuildProducer<ChatModelProviderCandidateBuildItem
void generateBeans(MistralAiRecorder recorder,
List<SelectedChatModelProviderBuildItem> selectedChatItem,
List<SelectedEmbeddingModelCandidateBuildItem> selectedEmbedding,
List<SelectedModerationModelProviderBuildItem> selectedModeration,
LangChain4jMistralAiConfig config,
BuildProducer<SyntheticBeanBuildItem> beanProducer) {

Expand Down Expand Up @@ -107,6 +115,20 @@ void generateBeans(MistralAiRecorder recorder,
beanProducer.produce(builder.done());
}
}

for (var selected : selectedModeration) {
if (PROVIDER.equals(selected.getProvider())) {
String configName = selected.getConfigName();
var builder = SyntheticBeanBuildItem
.configure(MODERATION_MODEL)
.setRuntimeInit()
.defaultBean()
.scope(ApplicationScoped.class)
.supplier(recorder.moderationModel(config, configName));
addQualifierIfNecessary(builder, configName);
beanProducer.produce(builder.done());
}
}
}

private void addQualifierIfNecessary(SyntheticBeanBuildItem.ExtendedBeanConfigurator builder, String configName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkiverse.langchain4j.mistralai.deployment;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocDefault;

public interface ModerationModelBuildConfig {

/**
* Whether the model should be enabled
*/
@ConfigDocDefault("true")
Optional<Boolean> enabled();

}
Loading

0 comments on commit 6209bee

Please sign in to comment.