Skip to content

Commit

Permalink
feat: Set the api-key by means of an environment variable. (#3714)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
  • Loading branch information
yuluo-yx authored May 10, 2024
1 parent c009d94 commit d718b9a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ The Spring Cloud Alibaba AI module is based on [Spring AI 0.8.1](https://docs.sp

2. Add the following configuration to the application. Yml configuration file:

> Note: It is recommended to set the api-key as an environment variable to avoid api-key leakage.
>
> ```shell
> export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45
> ```

```yml
spring:
cloud:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Spring Cloud Alibaba AI 模块基于 [Spring AI 0.8.1](https://docs.spring.io/sp

2. 在 application.yml 配置文件中加入以下配置:

> Note: 推荐使用环境变量的方式设置 api-key,避免 api-key 泄露。
>
> ```shell
> export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45
> ```

```yaml
spring:
cloud:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.cloud.ai.tongyi.audio.TongYiAudioSpeechProperties;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatClient;
import com.alibaba.cloud.ai.tongyi.chat.TongYiChatProperties;
import com.alibaba.cloud.ai.tongyi.constants.TongYiConstants;
import com.alibaba.cloud.ai.tongyi.exception.TongYiException;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesClient;
import com.alibaba.cloud.ai.tongyi.image.TongYiImagesProperties;
Expand Down Expand Up @@ -114,6 +115,7 @@ public TongYiChatClient tongYiChatClient(Generation generation,
) {

settingApiKey(connectionProperties);

return new TongYiChatClient(generation, chatOptions.getOptions());
}

Expand Down Expand Up @@ -157,11 +159,17 @@ public TongYiAudioSpeechClient tongYiAudioSpeechClient(
* Setting the API key.
* @param connectionProperties {@link TongYiConnectionProperties}
*/
public void settingApiKey(TongYiConnectionProperties connectionProperties) {
private void settingApiKey(TongYiConnectionProperties connectionProperties) {

String apiKey;

try {
// It is recommended to set the key by defining the api-key in an environment variable.
var envKey = System.getenv(TongYiConstants.SCA_AI_TONGYI_API_KEY);
if (Objects.nonNull(envKey)) {
Constants.apiKey = envKey;
return;
}
if (Objects.nonNull(connectionProperties.getApiKey())) {
apiKey = connectionProperties.getApiKey();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024-2025 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.tongyi.constants;

/**
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
*/

public final class TongYiConstants {

private TongYiConstants() {
}

/**
* Spring Cloud Alibaba AI constants prefix.
*/
public static final String SCA_AI = "SPRING_CLOUD_ALIBABA_";

/**
* TongYi AI apikey env name.
*/
public static final String SCA_AI_TONGYI_API_KEY = SCA_AI + "TONGYI_API_KEY";

}

0 comments on commit d718b9a

Please sign in to comment.