|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 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.deepseek.autoconfigure; |
| 18 | + |
| 19 | +import io.micrometer.observation.ObservationRegistry; |
| 20 | +import org.springframework.ai.chat.observation.ChatModelObservationConvention; |
| 21 | +import org.springframework.ai.model.SimpleApiKey; |
| 22 | +import org.springframework.ai.deepseek.DeepSeekChatModel; |
| 23 | +import org.springframework.ai.deepseek.api.DeepSeekApi; |
| 24 | +import org.springframework.ai.model.SpringAIModelProperties; |
| 25 | +import org.springframework.ai.model.SpringAIModels; |
| 26 | +import org.springframework.ai.model.tool.DefaultToolExecutionEligibilityPredicate; |
| 27 | +import org.springframework.ai.model.tool.ToolCallingManager; |
| 28 | +import org.springframework.ai.model.tool.ToolExecutionEligibilityPredicate; |
| 29 | +import org.springframework.ai.model.tool.autoconfigure.ToolCallingAutoConfiguration; |
| 30 | +import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration; |
| 31 | +import org.springframework.beans.factory.ObjectProvider; |
| 32 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 33 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 35 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 36 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 37 | +import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; |
| 38 | +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; |
| 39 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 40 | +import org.springframework.context.annotation.Bean; |
| 41 | +import org.springframework.retry.support.RetryTemplate; |
| 42 | +import org.springframework.util.Assert; |
| 43 | +import org.springframework.util.StringUtils; |
| 44 | +import org.springframework.web.client.ResponseErrorHandler; |
| 45 | +import org.springframework.web.client.RestClient; |
| 46 | +import org.springframework.web.reactive.function.client.WebClient; |
| 47 | + |
| 48 | +/** |
| 49 | + * {@link AutoConfiguration Auto-configuration} for DeepSeek Chat Model. |
| 50 | + * |
| 51 | + * @author Geng Rong |
| 52 | + */ |
| 53 | +@AutoConfiguration(after = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class, |
| 54 | + SpringAiRetryAutoConfiguration.class, ToolCallingAutoConfiguration.class }) |
| 55 | +@ConditionalOnClass(DeepSeekApi.class) |
| 56 | +@EnableConfigurationProperties({ DeepSeekConnectionProperties.class, DeepSeekChatProperties.class }) |
| 57 | +@ConditionalOnProperty(name = SpringAIModelProperties.CHAT_MODEL, havingValue = SpringAIModels.DEEPSEEK, |
| 58 | + matchIfMissing = true) |
| 59 | +@ImportAutoConfiguration(classes = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class, |
| 60 | + WebClientAutoConfiguration.class, ToolCallingAutoConfiguration.class }) |
| 61 | +public class DeepSeekChatAutoConfiguration { |
| 62 | + |
| 63 | + @Bean |
| 64 | + @ConditionalOnMissingBean |
| 65 | + public DeepSeekChatModel deepSeekChatModel(DeepSeekConnectionProperties commonProperties, |
| 66 | + DeepSeekChatProperties chatProperties, ObjectProvider<RestClient.Builder> restClientBuilderProvider, |
| 67 | + ObjectProvider<WebClient.Builder> webClientBuilderProvider, ToolCallingManager toolCallingManager, |
| 68 | + RetryTemplate retryTemplate, ResponseErrorHandler responseErrorHandler, |
| 69 | + ObjectProvider<ObservationRegistry> observationRegistry, |
| 70 | + ObjectProvider<ChatModelObservationConvention> observationConvention, |
| 71 | + ObjectProvider<ToolExecutionEligibilityPredicate> deepseekToolExecutionEligibilityPredicate) { |
| 72 | + |
| 73 | + var deepSeekApi = deepSeekApi(chatProperties, commonProperties, |
| 74 | + restClientBuilderProvider.getIfAvailable(RestClient::builder), |
| 75 | + webClientBuilderProvider.getIfAvailable(WebClient::builder), responseErrorHandler); |
| 76 | + |
| 77 | + var chatModel = DeepSeekChatModel.builder() |
| 78 | + .deepSeekApi(deepSeekApi) |
| 79 | + .defaultOptions(chatProperties.getOptions()) |
| 80 | + .toolCallingManager(toolCallingManager) |
| 81 | + .toolExecutionEligibilityPredicate(deepseekToolExecutionEligibilityPredicate |
| 82 | + .getIfUnique(DefaultToolExecutionEligibilityPredicate::new)) |
| 83 | + .retryTemplate(retryTemplate) |
| 84 | + .observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP)) |
| 85 | + .build(); |
| 86 | + |
| 87 | + observationConvention.ifAvailable(chatModel::setObservationConvention); |
| 88 | + |
| 89 | + return chatModel; |
| 90 | + } |
| 91 | + |
| 92 | + private DeepSeekApi deepSeekApi(DeepSeekChatProperties chatProperties, |
| 93 | + DeepSeekConnectionProperties commonProperties, RestClient.Builder restClientBuilder, |
| 94 | + WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) { |
| 95 | + |
| 96 | + String resolvedBaseUrl = StringUtils.hasText(chatProperties.getBaseUrl()) ? chatProperties.getBaseUrl() |
| 97 | + : commonProperties.getBaseUrl(); |
| 98 | + Assert.hasText(resolvedBaseUrl, "DeepSeek base URL must be set"); |
| 99 | + |
| 100 | + String resolvedApiKey = StringUtils.hasText(chatProperties.getApiKey()) ? chatProperties.getApiKey() |
| 101 | + : commonProperties.getApiKey(); |
| 102 | + Assert.hasText(resolvedApiKey, "DeepSeek API key must be set"); |
| 103 | + |
| 104 | + return DeepSeekApi.builder() |
| 105 | + .baseUrl(resolvedBaseUrl) |
| 106 | + .apiKey(new SimpleApiKey(resolvedApiKey)) |
| 107 | + .completionsPath(chatProperties.getCompletionsPath()) |
| 108 | + .betaPrefixPath(chatProperties.getBetaPrefixPath()) |
| 109 | + .restClientBuilder(restClientBuilder) |
| 110 | + .webClientBuilder(webClientBuilder) |
| 111 | + .responseErrorHandler(responseErrorHandler) |
| 112 | + .build(); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments