Skip to content

Commit

Permalink
feat: support read apikey from system env
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
  • Loading branch information
yuluo-yx committed Sep 24, 2024
1 parent fa83e29 commit 1964119
Showing 1 changed file with 70 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.util.Map;
import java.util.Objects;

import com.alibaba.cloud.ai.constant.AIConstants;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeImageApi;
import com.alibaba.cloud.ai.dashscope.common.DashScopeApiConstants;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
Expand Down Expand Up @@ -68,14 +70,24 @@
* @author yuluo
* @since 2024/8/16 11:45
*/
@AutoConfiguration(after = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class,
SpringAiRetryAutoConfiguration.class })
@ConditionalOnClass(DashScopeApi.class)
@EnableConfigurationProperties({ DashScopeConnectionProperties.class, DashScopeChatProperties.class,
DashScopeImageProperties.class, DashScopeAudioTranscriptionProperties.class,
DashScopeAudioSpeechProperties.class, DashScopeEmbeddingProperties.class })
@ImportAutoConfiguration(classes = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class,
WebClientAutoConfiguration.class })
@AutoConfiguration(after = {
RestClientAutoConfiguration.class,
WebClientAutoConfiguration.class,
SpringAiRetryAutoConfiguration.class
})
@EnableConfigurationProperties({
DashScopeConnectionProperties.class,
DashScopeChatProperties.class,
DashScopeImageProperties.class,
DashScopeAudioTranscriptionProperties.class,
DashScopeAudioSpeechProperties.class,
DashScopeEmbeddingProperties.class })
@ImportAutoConfiguration(classes = {
SpringAiRetryAutoConfiguration.class,
RestClientAutoConfiguration.class,
WebClientAutoConfiguration.class
})
public class DashScopeAutoConfiguration {

@Bean
Expand All @@ -93,8 +105,12 @@ public Transcription transcription() {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = DashScopeChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(
prefix = DashScopeChatProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public DashScopeChatModel dashscopeChatModel(DashScopeConnectionProperties commonProperties,
DashScopeChatProperties chatProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, List<FunctionCallback> toolFunctionCallbacks,
Expand All @@ -114,8 +130,12 @@ public DashScopeChatModel dashscopeChatModel(DashScopeConnectionProperties commo

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = DashScopeEmbeddingProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(
prefix = DashScopeEmbeddingProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public DashScopeApi dashscopeChatApi(DashScopeConnectionProperties commonProperties,
DashScopeChatProperties chatProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {
Expand All @@ -129,8 +149,12 @@ public DashScopeApi dashscopeChatApi(DashScopeConnectionProperties commonPropert

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = DashScopeEmbeddingProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(
prefix = DashScopeEmbeddingProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public DashScopeEmbeddingModel dashscopeEmbeddingModel(DashScopeConnectionProperties commonProperties,
DashScopeEmbeddingProperties embeddingProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, RetryTemplate retryTemplate,
Expand All @@ -143,10 +167,13 @@ public DashScopeEmbeddingModel dashscopeEmbeddingModel(DashScopeConnectionProper
embeddingProperties.getOptions(), retryTemplate);
}

public DashScopeApi dashscopeEmbeddingApi(DashScopeConnectionProperties commonProperties,
DashScopeEmbeddingProperties embeddingProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {

public DashScopeApi dashscopeEmbeddingApi(
DashScopeConnectionProperties commonProperties,
DashScopeEmbeddingProperties embeddingProperties,
RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder,
ResponseErrorHandler responseErrorHandler
) {
DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
embeddingProperties, "embedding");

Expand All @@ -156,8 +183,12 @@ public DashScopeApi dashscopeEmbeddingApi(DashScopeConnectionProperties commonPr

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = DashScopeEmbeddingProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(
prefix = DashScopeEmbeddingProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public DashScopeAgentApi dashscopeAgentApi(DashScopeConnectionProperties commonProperties,
DashScopeChatProperties chatProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {
Expand All @@ -178,8 +209,12 @@ public RestClientCustomizer restClientCustomizer(DashScopeConnectionProperties c

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = DashScopeImageProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(
prefix = DashScopeImageProperties.CONFIG_PREFIX,
name = "enabled",
havingValue = "true",
matchIfMissing = true
)
public DashScopeImageModel dashScopeImageModel(DashScopeConnectionProperties commonProperties,
DashScopeImageProperties imageProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, RetryTemplate retryTemplate,
Expand All @@ -197,14 +232,18 @@ public DashScopeImageModel dashScopeImageModel(DashScopeConnectionProperties com
@Bean
@ConditionalOnMissingBean
public FunctionCallbackContext springAiFunctionManager(ApplicationContext context) {

FunctionCallbackContext manager = new FunctionCallbackContext();
manager.setApplicationContext(context);
return manager;
}

private record ResolvedConnectionProperties(String baseUrl, String apiKey, String workspaceId,
MultiValueMap<String, String> headers) {
}
private record ResolvedConnectionProperties(
String baseUrl,
String apiKey,
String workspaceId,
MultiValueMap<String, String> headers
) { }

private static @NotNull DashScopeAutoConfiguration.ResolvedConnectionProperties resolveConnectionProperties(
DashScopeParentProperties commonProperties, DashScopeParentProperties modelProperties, String modelType) {
Expand All @@ -221,6 +260,13 @@ private record ResolvedConnectionProperties(String baseUrl, String apiKey, Strin
connectionHeaders.put("DashScope-Workspace", List.of(workspaceId));
}

// get apikey from system env.
if (Objects.isNull(apiKey)) {
if (Objects.nonNull(System.getenv(DashScopeApiConstants.DASHSCOPE_API_KEY))) {
apiKey = System.getenv(DashScopeApiConstants.DASHSCOPE_API_KEY);
}
}

Assert.hasText(baseUrl,
"DashScope base URL must be set. Use the connection property: spring.ai.dashscope.base-url or spring.ai.dashscope."
+ modelType + ".base-url property.");
Expand Down

0 comments on commit 1964119

Please sign in to comment.