Skip to content
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

Feature/autoconfig 2024 #110

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
<modules>
<module>spring-ai-alibaba-core</module>
<module>spring-ai-alibaba-starter</module>
</modules>
<module>spring-ai-alibaba-autoconfigure</module>
</modules>

<properties>
<revision>1.0.0-M3.2</revision>
Expand Down
56 changes: 56 additions & 0 deletions spring-ai-alibaba-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright 2023-2024 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>spring-ai-alibaba-autoconfigure</artifactId>
<packaging>jar</packaging>

<name>spring-ai-autoconfig</name>
<url>https://github.com/alibaba/spring-ai-alibaba</url>
<scm>
<url>https://github.com/alibaba/spring-ai-alibaba</url>
<connection>git://github.com/alibaba/spring-ai-alibaba.git</connection>
<developerConnection>[email protected]:alibaba/spring-ai-alibaba.git</developerConnection>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-core</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

package com.alibaba.cloud.ai.autoconfigure.dashscope;

import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeSpeechSynthesisApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAudioTranscriptionApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeImageApi;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.api.*;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeAudioTranscriptionModel;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.common.DashScopeApiConstants;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
Expand Down Expand Up @@ -121,8 +117,7 @@ public DashScopeApi dashscopeChatApi(DashScopeConnectionProperties commonPropert
DashScopeChatProperties chatProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
chatProperties, "chat");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, chatProperties, "chat");

return new DashScopeApi(resolved.baseUrl(), resolved.apiKey(), resolved.workspaceId(), restClientBuilder,
webClientBuilder, responseErrorHandler);
Expand All @@ -147,8 +142,8 @@ public DashScopeEmbeddingModel dashscopeEmbeddingModel(DashScopeConnectionProper
public DashScopeApi dashscopeEmbeddingApi(DashScopeConnectionProperties commonProperties,
DashScopeEmbeddingProperties embeddingProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {
DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
embeddingProperties, "embedding");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, embeddingProperties,
"embedding");

return new DashScopeApi(resolved.baseUrl(), resolved.apiKey(), resolved.workspaceId(), restClientBuilder,
webClientBuilder, responseErrorHandler);
Expand All @@ -161,8 +156,8 @@ public DashScopeApi dashscopeEmbeddingApi(DashScopeConnectionProperties commonPr
public DashScopeSpeechSynthesisApi dashScopeSpeechSynthesisApi(DashScopeConnectionProperties commonProperties,
DashScopeSpeechSynthesisProperties speechSynthesisProperties) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
speechSynthesisProperties, "speechsynthesis");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, speechSynthesisProperties,
"speechsynthesis");

return new DashScopeSpeechSynthesisApi(resolved.apiKey());
}
Expand All @@ -174,7 +169,7 @@ public DashScopeSpeechSynthesisApi dashScopeSpeechSynthesisApi(DashScopeConnecti
public DashScopeAudioTranscriptionApi dashScopeAudioTranscriptionApi(DashScopeConnectionProperties commonProperties,
DashScopeAudioTranscriptionProperties audioTranscriptionProperties) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
audioTranscriptionProperties, "audiotranscription");

return new DashScopeAudioTranscriptionApi(resolved.apiKey());
Expand All @@ -188,8 +183,7 @@ public DashScopeAgentApi dashscopeAgentApi(DashScopeConnectionProperties commonP
DashScopeChatProperties chatProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
chatProperties, "chat");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, chatProperties, "chat");

return new DashScopeAgentApi(resolved.baseUrl(), resolved.apiKey(), resolved.workspaceId(), restClientBuilder,
webClientBuilder, responseErrorHandler);
Expand All @@ -211,8 +205,7 @@ public DashScopeImageModel dashScopeImageModel(DashScopeConnectionProperties com
WebClient.Builder webClientBuilder, RetryTemplate retryTemplate,
ResponseErrorHandler responseErrorHandler) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
imageProperties, "image");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, imageProperties, "image");

var dashScopeImageApi = new DashScopeImageApi(resolved.baseUrl(), resolved.apiKey(), resolved.workspaceId(),
restClientBuilder, webClientBuilder, responseErrorHandler);
Expand All @@ -228,8 +221,8 @@ public DashScopeRerankModel dashscopeRerankModel(DashScopeConnectionProperties c
DashScopeRerankProperties rerankProperties, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, RetryTemplate retryTemplate,
ResponseErrorHandler responseErrorHandler) {
DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
rerankProperties, "rerank");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, rerankProperties,
"rerank");

var dashscopeApi = new DashScopeApi(resolved.baseUrl(), resolved.apiKey(), resolved.workspaceId(),
restClientBuilder, webClientBuilder, responseErrorHandler);
Expand All @@ -244,8 +237,8 @@ public DashScopeRerankModel dashscopeRerankModel(DashScopeConnectionProperties c
public DashScopeSpeechSynthesisModel dashScopeSpeechSynthesisModel(DashScopeConnectionProperties commonProperties,
DashScopeSpeechSynthesisProperties speechSynthesisProperties, RetryTemplate retryTemplate) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
speechSynthesisProperties, "speechsynthesis");
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties, speechSynthesisProperties,
"speechsynthesis");

var dashScopeSpeechSynthesisApi = dashScopeSpeechSynthesisApi(commonProperties, speechSynthesisProperties);

Expand All @@ -261,7 +254,7 @@ public DashScopeAudioTranscriptionModel dashScopeAudioTranscriptionModel(
DashScopeConnectionProperties commonProperties,
DashScopeAudioTranscriptionProperties audioTranscriptionProperties, RetryTemplate retryTemplate) {

DashScopeAutoConfiguration.ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
ResolvedConnectionProperties resolved = resolveConnectionProperties(commonProperties,
audioTranscriptionProperties, "audiotranscription");

var dashScopeSpeechSynthesisApi = dashScopeAudioTranscriptionApi(commonProperties,
Expand All @@ -284,7 +277,7 @@ private record ResolvedConnectionProperties(String baseUrl, String apiKey, Strin
MultiValueMap<String, String> headers) {
}

private static @NotNull DashScopeAutoConfiguration.ResolvedConnectionProperties resolveConnectionProperties(
private static @NotNull ResolvedConnectionProperties resolveConnectionProperties(
DashScopeParentProperties commonProperties, DashScopeParentProperties modelProperties, String modelType) {

String baseUrl = StringUtils.hasText(modelProperties.getBaseUrl()) ? modelProperties.getBaseUrl()
Expand Down Expand Up @@ -313,7 +306,7 @@ private record ResolvedConnectionProperties(String baseUrl, String apiKey, Strin
"DashScope API key must be set. Use the connection property: spring.ai.dashscope.api-key or spring.ai.dashscope."
+ modelType + ".api-key property.");

return new DashScopeAutoConfiguration.ResolvedConnectionProperties(baseUrl, apiKey, workspaceId,
return new ResolvedConnectionProperties(baseUrl, apiKey, workspaceId,
CollectionUtils.toMultiValueMap(connectionHeaders));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.alibaba.cloud.ai.autoconfigure.dashscope;

import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;

import org.springframework.ai.document.MetadataMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@

public class PromptTemplateAutoConfiguration {

private final static String TEMPLATE_PREFIX = "spring.ai.nacos.prompt.template";

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.ai.nacos.prompt.template", name = "enabled", havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(prefix = TEMPLATE_PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public ConfigurablePromptTemplateFactory configurablePromptTemplateFactory() {
return new ConfigurablePromptTemplateFactory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.alibaba.cloud.ai.agent;

import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration;
import com.alibaba.cloud.ai.autoconfig.dashscope.DashScopeAutoConfiguration;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.cloud.ai.autoconfig.dashscope;

import com.alibaba.cloud.ai.dashscope.audio.DashScopeAudioTranscriptionOptions;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import static com.alibaba.cloud.ai.dashscope.common.DashScopeApiConstants.DEFAULT_BASE_URL;

/**
* @author xYLiu
* @author yuluo
* @author kevinlin09
*/

@ConfigurationProperties(DashScopeAudioTranscriptionProperties.CONFIG_PREFIX)
public class DashScopeAudioTranscriptionProperties extends DashScopeParentProperties {

/**
* Spring AI Alibaba configuration prefix.
*/
public static final String CONFIG_PREFIX = "spring.ai.dashscope.audio.transcription";

@NestedConfigurationProperty
private DashScopeAudioTranscriptionOptions options = DashScopeAudioTranscriptionOptions.builder().build();

public DashScopeAudioTranscriptionProperties() {
super.setBaseUrl(DEFAULT_BASE_URL);
}

public DashScopeAudioTranscriptionOptions getOptions() {
return this.options;
}

public void setOptions(DashScopeAudioTranscriptionOptions options) {
this.options = options;
}

}
Loading