fix: fix tongyi ai embedding ci (#3741)

Signed-off-by: yuluo-yx <yuluo08290126@gmail.com>
pull/3739/head^2
YuLuo 9 months ago committed by GitHub
parent 2b393fb664
commit fe7a28e6ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -23,11 +23,11 @@ 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: 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. > Note: It is recommended to set the api-key as an environment variable to avoid api-key leakage.
>
```shell > ```shell
export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45 > export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45
``` > ```
```yml ```yml
spring: spring:

@ -23,11 +23,11 @@ Spring Cloud Alibaba AI 模块基于 [Spring AI 0.8.1](https://docs.spring.io/sp
2. 在 application.yml 配置文件中加入以下配置: 2. 在 application.yml 配置文件中加入以下配置:
Note: 推荐使用环境变量的方式设置 api-key避免 api-key 泄露。 > Note: 推荐使用环境变量的方式设置 api-key避免 api-key 泄露。
>
```shell > ```shell
export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45 > export SPRING_CLOUD_AI_TONGYI_API_KEY=sk-a3d73b1709bf4a178c28ed7c8b3b5a45
``` > ```
```yaml ```yaml
spring: spring:

@ -23,7 +23,6 @@ import com.alibaba.cloud.ai.example.tongyi.models.ActorsFilms;
import com.alibaba.cloud.ai.example.tongyi.models.Completion; import com.alibaba.cloud.ai.example.tongyi.models.Completion;
import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.image.ImageResponse; import org.springframework.ai.image.ImageResponse;
/** /**
@ -46,49 +45,57 @@ public abstract class AbstractTongYiServiceImpl implements TongYiService {
@Override @Override
public Map<String, String> streamCompletion(String message) { public Map<String, String> streamCompletion(String message) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public ActorsFilms genOutputParse(String actor) { public ActorsFilms genOutputParse(String actor) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public AssistantMessage genPromptTemplates(String adjective, String topic) { public AssistantMessage genPromptTemplates(String adjective, String topic) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public AssistantMessage genRole(String message, String name, String voice) { public AssistantMessage genRole(String message, String name, String voice) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public Completion stuffCompletion(String message, boolean stuffit) { public Completion stuffCompletion(String message, boolean stuffit) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public ImageResponse genImg(String imgPrompt) { public ImageResponse genImg(String imgPrompt) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public String genAudio(String text) { public String genAudio(String text) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
@Override @Override
public List<Double> textEmbedding(String text) { public List<Double> textEmbedding(String text) {
throw new RuntimeException(INFO_PREFIX + Thread.currentThread().getStackTrace()[2].getMethodName() + INFO_SUFFIX); throw new RuntimeException(INFO_PREFIX + Thread.currentThread()
.getStackTrace()[2].getMethodName() + INFO_SUFFIX);
} }
} }

@ -0,0 +1,22 @@
# Spring Cloud Alibaba AI Text Embedding
`TongYiController` 接受一个 HTTP GET 请求 `http://localhost:8080/ai/audio`
`controller` 将会调用 `TongYiService` 中的 `genAudio` 方法,完成服务请求得到响应。
有一个可选的 `text` 参数其默认值为“Spring Cloud Alibaba AI 框架!”。 请求响应来自 Alibaba TongYi Text Embedding 服务。
## 构建和运行
1. 修改配置文件 `application.yml` 中的 apikey 为有效的 apikey
2. 通过 IDE 或者 `./mvnw spring-boot:run` 运行应用程序。
## 访问接口
使用 curl 工具或者使用浏览器对接口发起请求:
```shell
$ curl http://localhost:8080/ai/textEmbedding
# Response:
为一组向量集合
```

@ -17,7 +17,6 @@
package com.alibaba.cloud.ai.example.tongyi.service.impl.textembedding; package com.alibaba.cloud.ai.example.tongyi.service.impl.textembedding;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
import com.alibaba.cloud.ai.example.tongyi.service.AbstractTongYiServiceImpl; import com.alibaba.cloud.ai.example.tongyi.service.AbstractTongYiServiceImpl;
@ -32,8 +31,6 @@ import org.springframework.stereotype.Service;
@Service @Service
public class TongYiTextEmbeddingServiceImpl extends AbstractTongYiServiceImpl { public class TongYiTextEmbeddingServiceImpl extends AbstractTongYiServiceImpl {
private final Logger logger = Logger.getLogger(TongYiTextEmbeddingServiceImpl.class.getName());
private final EmbeddingClient embeddingClient; private final EmbeddingClient embeddingClient;
public TongYiTextEmbeddingServiceImpl(EmbeddingClient embeddingClient) { public TongYiTextEmbeddingServiceImpl(EmbeddingClient embeddingClient) {
@ -46,4 +43,5 @@ public class TongYiTextEmbeddingServiceImpl extends AbstractTongYiServiceImpl {
return embeddingClient.embed(text); return embeddingClient.embed(text);
} }
} }

@ -63,7 +63,7 @@ import org.springframework.context.annotation.Bean;
TongYiChatProperties.class, TongYiChatProperties.class,
TongYiImagesProperties.class, TongYiImagesProperties.class,
TongYiAudioSpeechProperties.class, TongYiAudioSpeechProperties.class,
TongYiConnectionProperties.class TongYiConnectionProperties.class,
TongYiConnectionProperties.class, TongYiConnectionProperties.class,
TongYiTextEmbeddingProperties.class TongYiTextEmbeddingProperties.class
}) })

@ -24,8 +24,9 @@ import org.springframework.ai.embedding.EmbeddingOptions;
/** /**
* @author why_ohh * @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a> * @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.0.0-RC1 * @since 2023.0.0.0
*/ */
public final class TongYiEmbeddingOptions implements EmbeddingOptions { public final class TongYiEmbeddingOptions implements EmbeddingOptions {

@ -39,14 +39,12 @@ import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
// * {@link TongYiTextEmbeddingClient} implementation for {@literal Alibaba DashScope} * {@link TongYiTextEmbeddingClient} implementation for {@literal Alibaba DashScope}.
* backed by {@link Generation}.
*
* @author why_ohh * @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a> * @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.0.0-RC1 * @since 2023.0.0.0
* @see TextEmbeddingClient * {@see TextEmbeddingClient}
* @see com.alibaba.dashscope.aigc.generation
*/ */
public class TongYiTextEmbeddingClient extends AbstractEmbeddingClient { public class TongYiTextEmbeddingClient extends AbstractEmbeddingClient {
@ -59,7 +57,7 @@ public class TongYiTextEmbeddingClient extends AbstractEmbeddingClient {
private final TextEmbedding textEmbedding; private final TextEmbedding textEmbedding;
/** /**
* {@link MetadataMode} * {@link MetadataMode}.
*/ */
private final MetadataMode metadataMode; private final MetadataMode metadataMode;
@ -83,8 +81,7 @@ public class TongYiTextEmbeddingClient extends AbstractEmbeddingClient {
TextEmbedding textEmbedding, TextEmbedding textEmbedding,
MetadataMode metadataMode, MetadataMode metadataMode,
TongYiEmbeddingOptions options TongYiEmbeddingOptions options
){ ) {
Assert.notNull(textEmbedding, "textEmbedding must not be null"); Assert.notNull(textEmbedding, "textEmbedding must not be null");
Assert.notNull(metadataMode, "Metadata mode must not be null"); Assert.notNull(metadataMode, "Metadata mode must not be null");
Assert.notNull(options, "TongYiEmbeddingOptions must not be null"); Assert.notNull(options, "TongYiEmbeddingOptions must not be null");
@ -94,7 +91,7 @@ public class TongYiTextEmbeddingClient extends AbstractEmbeddingClient {
this.defaultOptions = options; this.defaultOptions = options;
} }
public TongYiEmbeddingOptions getDefaultOptions(){ public TongYiEmbeddingOptions getDefaultOptions() {
return this.defaultOptions; return this.defaultOptions;
} }

@ -20,13 +20,17 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* @author why_ohh * @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a> * @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @since 2023.0.0.0-RC1 * @since 2023.0.0.0
*/ */
@ConfigurationProperties(TongYiTextEmbeddingProperties.CONFIG_PREFIX) @ConfigurationProperties(TongYiTextEmbeddingProperties.CONFIG_PREFIX)
public class TongYiTextEmbeddingProperties { public class TongYiTextEmbeddingProperties {
/**
* Prefix of TongYi Text Embedding properties.
*/
public static final String CONFIG_PREFIX = "spring.cloud.ai.tongyi.embedding"; public static final String CONFIG_PREFIX = "spring.cloud.ai.tongyi.embedding";
private boolean enabled = true; private boolean enabled = true;

@ -22,6 +22,7 @@ import org.springframework.ai.embedding.EmbeddingResponseMetadata;
/** /**
* @author why_ohh * @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a> * @author <a href="mailto:550588941@qq.com">why_ohh</a>
*/ */
@ -48,4 +49,5 @@ public class TongYiTextEmbeddingResponseMetadata extends EmbeddingResponseMetada
this.totalTokens = totalTokens; this.totalTokens = totalTokens;
} }
} }

Loading…
Cancel
Save