| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2023-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 | + */  | 
 | 16 | + | 
 | 17 | +package org.springframework.ai.model.mistralai.autoconfigure;  | 
 | 18 | + | 
 | 19 | +import org.springframework.ai.mistralai.api.MistralAiApi;  | 
 | 20 | +import org.springframework.ai.mistralai.api.MistralAiModerationApi;  | 
 | 21 | +import org.springframework.ai.mistralai.moderation.MistralAiModerationModel;  | 
 | 22 | +import org.springframework.ai.model.SpringAIModelProperties;  | 
 | 23 | +import org.springframework.ai.model.SpringAIModels;  | 
 | 24 | +import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration;  | 
 | 25 | +import org.springframework.beans.factory.ObjectProvider;  | 
 | 26 | +import org.springframework.boot.autoconfigure.AutoConfiguration;  | 
 | 27 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration;  | 
 | 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;  | 
 | 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;  | 
 | 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;  | 
 | 31 | +import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;  | 
 | 32 | +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;  | 
 | 33 | +import org.springframework.boot.context.properties.EnableConfigurationProperties;  | 
 | 34 | +import org.springframework.context.annotation.Bean;  | 
 | 35 | +import org.springframework.retry.support.RetryTemplate;  | 
 | 36 | +import org.springframework.util.Assert;  | 
 | 37 | +import org.springframework.util.StringUtils;  | 
 | 38 | +import org.springframework.web.client.ResponseErrorHandler;  | 
 | 39 | +import org.springframework.web.client.RestClient;  | 
 | 40 | + | 
 | 41 | +/**  | 
 | 42 | + * Moderation {@link AutoConfiguration Auto-configuration} for Mistral AI.  | 
 | 43 | + *  | 
 | 44 | + * @author Ricken Bazolo  | 
 | 45 | + */  | 
 | 46 | +@AutoConfiguration(after = { RestClientAutoConfiguration.class, SpringAiRetryAutoConfiguration.class,  | 
 | 47 | +		WebClientAutoConfiguration.class })  | 
 | 48 | +@EnableConfigurationProperties({ MistralAiCommonProperties.class, MistralAiModerationProperties.class })  | 
 | 49 | +@ConditionalOnProperty(name = SpringAIModelProperties.MODERATION_MODEL, havingValue = SpringAIModels.MISTRAL,  | 
 | 50 | +		matchIfMissing = true)  | 
 | 51 | +@ConditionalOnClass(MistralAiApi.class)  | 
 | 52 | +@ImportAutoConfiguration(classes = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class,  | 
 | 53 | +		WebClientAutoConfiguration.class })  | 
 | 54 | +public class MistralAiModerationAutoConfiguration {  | 
 | 55 | + | 
 | 56 | +	@Bean  | 
 | 57 | +	@ConditionalOnMissingBean  | 
 | 58 | +	public MistralAiModerationModel mistralAiModerationModel(MistralAiCommonProperties commonProperties,  | 
 | 59 | +			MistralAiModerationProperties moderationProperties, RetryTemplate retryTemplate,  | 
 | 60 | +			ObjectProvider<RestClient.Builder> restClientBuilderProvider, ResponseErrorHandler responseErrorHandler) {  | 
 | 61 | + | 
 | 62 | +		var apiKey = moderationProperties.getApiKey();  | 
 | 63 | +		var baseUrl = moderationProperties.getBaseUrl();  | 
 | 64 | + | 
 | 65 | +		var resolvedApiKey = StringUtils.hasText(apiKey) ? apiKey : commonProperties.getApiKey();  | 
 | 66 | +		var resoledBaseUrl = StringUtils.hasText(baseUrl) ? baseUrl : commonProperties.getBaseUrl();  | 
 | 67 | + | 
 | 68 | +		Assert.hasText(resolvedApiKey, "Mistral API key must be set");  | 
 | 69 | +		Assert.hasText(resoledBaseUrl, "Mistral base URL must be set");  | 
 | 70 | + | 
 | 71 | +		var mistralAiModerationAi = new MistralAiModerationApi(resoledBaseUrl, resolvedApiKey,  | 
 | 72 | +				restClientBuilderProvider.getIfAvailable(RestClient::builder), responseErrorHandler);  | 
 | 73 | + | 
 | 74 | +		return new MistralAiModerationModel(mistralAiModerationAi, retryTemplate, moderationProperties.getOptions());  | 
 | 75 | +	}  | 
 | 76 | + | 
 | 77 | +}  | 
0 commit comments