diff --git a/models/spring-ai-dashscope/README.md b/models/spring-ai-dashscope/README.md new file mode 100644 index 0000000..843dec2 --- /dev/null +++ b/models/spring-ai-dashscope/README.md @@ -0,0 +1,5 @@ +阿里云的灵积模块 +支持以下模块 + +1. 通义千问 +2. 通义万相 \ No newline at end of file diff --git a/models/spring-ai-dashscope/pom.xml b/models/spring-ai-dashscope/pom.xml index bf26967..b853782 100644 --- a/models/spring-ai-dashscope/pom.xml +++ b/models/spring-ai-dashscope/pom.xml @@ -43,5 +43,11 @@ org.springframework.boot spring-boot-starter-logging + + + com.alibaba + dashscope-sdk-java + 2.10.1 + \ No newline at end of file diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/StableDiffusionImageClient.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/StableDiffusionImageClient.java deleted file mode 100644 index d9ddc2b..0000000 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/StableDiffusionImageClient.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.springframework.ai.dashcope; - -public class StableDiffusionImageClient { - -} diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/DashCopeService.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/DashsCopeService.java similarity index 94% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/DashCopeService.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/DashsCopeService.java index 8657478..80ef2c1 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/DashCopeService.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/DashsCopeService.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope; +package org.springframework.ai.dashscope; import java.io.IOException; import java.util.List; @@ -27,9 +27,9 @@ import reactor.core.publisher.Flux; -public class DashCopeService { +public class DashsCopeService { - private final Logger logger = LoggerFactory.getLogger(DashCopeService.class); + private final Logger logger = LoggerFactory.getLogger(DashsCopeService.class); private final String baseUrl = "https://dashscope.aliyuncs.com"; @@ -39,7 +39,7 @@ public class DashCopeService { private final ObjectMapper objectMapper; - public DashCopeService(String accessToken) { + public DashsCopeService(String accessToken) { this.objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); Consumer jsonContentHeaders = headers -> { @@ -94,7 +94,7 @@ public DashCopeApiClientErrorException(String message,Throwable cause) { public class LogHttpRequestInterceptor implements ClientHttpRequestInterceptor{ - private final Logger logger = LoggerFactory.getLogger(DashCopeService.class); + private final Logger logger = LoggerFactory.getLogger(DashsCopeService.class); @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) @@ -135,6 +135,15 @@ public ChatCompletionRequest(List messages,String model,F this(model,new Input(null, messages,null),new Parameters("message", null, null, null, null, null, temperature, null, null,null)); } } + + @JsonInclude(Include.NON_NULL) + public record ChatGLMRequest(){ + + } + + public record CharGLMResponse(){ + + } @JsonInclude(Include.NON_NULL) public record Input( @@ -298,6 +307,10 @@ public ResponseEntity embeddingRequest(EmbeddingRequest embed .body(embeddingRequest).retrieve() .toEntity(EmbeddingResponse.class); } + + public ResponseEntity chatGLMEntity(ChatGLMRequest chatGLMRequest){ + return this.restClient.post().uri("").body(chatGLMRequest).retrieve().toEntity(CharGLMResponse.class); + } public static void main(String[] args) throws JsonProcessingException { String body = "{\"output\":{\"finish_reason\":\"stop\",\"text\":\"我是阿里云开发的一款超大规模语言模型,我叫通义千问。\"},\"usage\":{\"total_tokens\":20,\"output_tokens\":17,\"input_tokens\":3},\"request_id\":\"f978f627-fd0f-91fd-be5d-b3ec1ac394b1\"}"; diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/StableDiffusionImageClient.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/StableDiffusionImageClient.java new file mode 100644 index 0000000..f6fa3e4 --- /dev/null +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/StableDiffusionImageClient.java @@ -0,0 +1,13 @@ +package org.springframework.ai.dashscope; + +import org.springframework.ai.image.ImageClient; +import org.springframework.ai.image.ImagePrompt; +import org.springframework.ai.image.ImageResponse; + +public class StableDiffusionImageClient implements ImageClient { + + @Override + public ImageResponse call(ImagePrompt request) { + return null; + } +} diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/api/AbstractDashCopeService.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/api/AbstractDashCopeService.java similarity index 94% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/api/AbstractDashCopeService.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/api/AbstractDashCopeService.java index 2049e5a..c957100 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/api/AbstractDashCopeService.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/api/AbstractDashCopeService.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope.api; +package org.springframework.ai.dashscope.api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/QwenRateLimit.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/QwenRateLimit.java similarity index 96% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/QwenRateLimit.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/QwenRateLimit.java index 66f78a4..788cbb9 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/QwenRateLimit.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/QwenRateLimit.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope.metadata; +package org.springframework.ai.dashscope.metadata; import java.time.Duration; diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/ChatModel.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/ChatModel.java similarity index 89% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/ChatModel.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/ChatModel.java index a5bd8da..4460604 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/ChatModel.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/ChatModel.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope.metadata.support; +package org.springframework.ai.dashscope.metadata.support; public enum ChatModel { QWen_1_8B_CHAT("qwen-1.8b-chat"), diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/DashCopeResponseHeaderExtractor.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/DashCopeResponseHeaderExtractor.java similarity index 59% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/DashCopeResponseHeaderExtractor.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/DashCopeResponseHeaderExtractor.java index f9271fb..ca60d4f 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/DashCopeResponseHeaderExtractor.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/DashCopeResponseHeaderExtractor.java @@ -1,13 +1,13 @@ -package org.springframework.ai.dashcope.metadata.support; +package org.springframework.ai.dashscope.metadata.support; import org.springframework.ai.chat.metadata.RateLimit; -import org.springframework.ai.dashcope.DashCopeService.ChatCompletion; -import org.springframework.ai.dashcope.metadata.QwenRateLimit; +import org.springframework.ai.dashscope.DashsCopeService; +import org.springframework.ai.dashscope.metadata.QwenRateLimit; import org.springframework.http.ResponseEntity; public class DashCopeResponseHeaderExtractor { - public static RateLimit extractAiResponseHeaders(ResponseEntity response) { + public static RateLimit extractAiResponseHeaders(ResponseEntity response) { //Long requestLimit = getHeaderAsLong(response,) return new QwenRateLimit(null,null,null,null,null,null); } diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/EmbeddingModel.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/EmbeddingModel.java similarity index 77% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/EmbeddingModel.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/EmbeddingModel.java index 6af347a..cb29173 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/metadata/support/EmbeddingModel.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/metadata/support/EmbeddingModel.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope.metadata.support; +package org.springframework.ai.dashscope.metadata.support; public enum EmbeddingModel { TEXT_EMBEDDING_V1("text-embedding-v1"),TEXT_EMBEDDING_V2("text-embedding-v2"); diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenChatClient.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenChatClient.java similarity index 72% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenChatClient.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenChatClient.java index eb192a3..dcb749d 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenChatClient.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenChatClient.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope.qwen; +package org.springframework.ai.dashscope.qwen; import java.io.IOException; import java.time.Duration; @@ -16,12 +16,8 @@ import org.springframework.ai.chat.metadata.ChatGenerationMetadata; import org.springframework.ai.chat.metadata.RateLimit; import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.ai.dashcope.DashCopeService; -import org.springframework.ai.dashcope.DashCopeService.ChatCompletion; -import org.springframework.ai.dashcope.DashCopeService.ChatCompletionMessage; -import org.springframework.ai.dashcope.DashCopeService.ChatCompletionRequest; -import org.springframework.ai.dashcope.metadata.support.ChatModel; -import org.springframework.ai.model.ModelOptionsUtils; +import org.springframework.ai.dashscope.DashsCopeService; +import org.springframework.ai.dashscope.metadata.support.ChatModel; import org.springframework.http.ResponseEntity; import org.springframework.retry.support.RetryTemplate; @@ -35,15 +31,15 @@ public class QWenChatClient implements ChatClient,StreamingChatClient { private final Logger logger = LoggerFactory.getLogger(QWenChatClient.class); - private final DashCopeService dashCopeService; + private final DashsCopeService dashCopeService; private QWenChatOptions defaultChatOptions; - public QWenChatClient(DashCopeService dashCopeService) { + public QWenChatClient(DashsCopeService dashCopeService) { this(dashCopeService, QWenChatOptions.builder().withModel(ChatModel.QWen_72B_CHAT).withTemperature(0.7f).build()); } - public QWenChatClient(DashCopeService dashCopeService,QWenChatOptions qWenChatOptions) { + public QWenChatClient(DashsCopeService dashCopeService, QWenChatOptions qWenChatOptions) { this.dashCopeService = dashCopeService; this.defaultChatOptions = qWenChatOptions; } @@ -58,7 +54,7 @@ public QWenChatClient(DashCopeService dashCopeService,QWenChatOptions qWenChatOp public ChatResponse call(Prompt prompt) { return this.retryTemplate.execute(ctx -> { - ResponseEntity completionEntity = this.dashCopeService.chatCompletionEntity(createRequest(prompt)); + ResponseEntity completionEntity = this.dashCopeService.chatCompletionEntity(createRequest(prompt)); var chatCompletion = completionEntity.getBody(); if(chatCompletion == null) { @@ -80,7 +76,7 @@ public ChatResponse call(Prompt prompt) { public Flux stream(Prompt prompt) { return this.retryTemplate.execute(ctx -> { - Flux completionChunks = this.dashCopeService.chatCompletionStream(createRequest(prompt)); + Flux completionChunks = this.dashCopeService.chatCompletionStream(createRequest(prompt)); ConcurrentHashMap roleMap = new ConcurrentHashMap(); return completionChunks.map(chunk -> { @@ -100,14 +96,14 @@ public Flux stream(Prompt prompt) { }); } - private ChatCompletionRequest createRequest(Prompt prompt) { + private DashsCopeService.ChatCompletionRequest createRequest(Prompt prompt) { List messages = prompt.getInstructions(); - List chatCompletionMessages = messages.stream() - .map(m -> new ChatCompletionMessage(m.getMessageType().getValue(), + List chatCompletionMessages = messages.stream() + .map(m -> new DashsCopeService.ChatCompletionMessage(m.getMessageType().getValue(), m.getContent())) .toList(); - ChatCompletionRequest request = new ChatCompletionRequest(chatCompletionMessages, this.defaultChatOptions.getModel(), this.defaultChatOptions.getTemperature()); + DashsCopeService.ChatCompletionRequest request = new DashsCopeService.ChatCompletionRequest(chatCompletionMessages, this.defaultChatOptions.getModel(), this.defaultChatOptions.getTemperature()); return request; } diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenChatOptions.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenChatOptions.java similarity index 93% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenChatOptions.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenChatOptions.java index dcc28de..a3beb3a 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenChatOptions.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenChatOptions.java @@ -1,7 +1,7 @@ -package org.springframework.ai.dashcope.qwen; +package org.springframework.ai.dashscope.qwen; import org.springframework.ai.chat.ChatOptions; -import org.springframework.ai.dashcope.metadata.support.ChatModel; +import org.springframework.ai.dashscope.metadata.support.ChatModel; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenEmbeddingClient.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenEmbeddingClient.java similarity index 71% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenEmbeddingClient.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenEmbeddingClient.java index f36b425..e8ac6ca 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenEmbeddingClient.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenEmbeddingClient.java @@ -1,4 +1,4 @@ -package org.springframework.ai.dashcope.qwen; +package org.springframework.ai.dashscope.qwen; import static org.assertj.core.api.Assertions.contentOf; @@ -7,10 +7,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.ai.dashcope.DashCopeService; -import org.springframework.ai.dashcope.DashCopeService.DashCopeApiException; -import org.springframework.ai.dashcope.DashCopeService.Usage; -import org.springframework.ai.dashcope.metadata.support.EmbeddingModel; +import org.springframework.ai.dashscope.DashsCopeService; +import org.springframework.ai.dashscope.metadata.support.EmbeddingModel; import org.springframework.ai.document.Document; import org.springframework.ai.document.MetadataMode; import org.springframework.ai.embedding.AbstractEmbeddingClient; @@ -18,13 +16,12 @@ import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; import org.springframework.ai.embedding.EmbeddingResponseMetadata; -import org.springframework.http.ResponseEntity; import org.springframework.retry.RetryListener; import org.springframework.retry.support.RetryTemplate; import org.springframework.util.Assert; /** - * 通义千问的的文本内嵌客户端实现 + * 通用文本向量,是通义实验室基于LLM底座的多语言文本统一向量模型,面向全球多个主流语种,提供高水准的向量服务,帮助开发者将文本数据快速转换为高质量的向量数据。 * @author 黄文杰 */ public class QWenEmbeddingClient extends AbstractEmbeddingClient { @@ -34,7 +31,7 @@ public class QWenEmbeddingClient extends AbstractEmbeddingClient { public static final EmbeddingModel DEFAULT_OPENAI_EMBEDDING_MODEL = EmbeddingModel.TEXT_EMBEDDING_V1; private final RetryTemplate retryTemplate = RetryTemplate.builder().maxAttempts(10) - .retryOn(DashCopeApiException.class) + .retryOn(DashsCopeService.DashCopeApiException.class) .exponentialBackoff(Duration.ofMillis(2000), 5, Duration.ofMillis(3* 60000)) .withListener(new RetryListener() { public void onError(org.springframework.retry.RetryContext context, org.springframework.retry.RetryCallback callback, Throwable throwable) { @@ -44,20 +41,20 @@ public void onError(org.springframework. private final QWenEmbeddingOptions embeddingOptions; - private final DashCopeService dashCopeService; + private final DashsCopeService dashCopeService; private final MetadataMode metadataMode; - public QWenEmbeddingClient(DashCopeService dashCopeService) { + public QWenEmbeddingClient(DashsCopeService dashCopeService) { this(dashCopeService,MetadataMode.EMBED); } - public QWenEmbeddingClient(DashCopeService dashCopeService,MetadataMode metadataMode) { + public QWenEmbeddingClient(DashsCopeService dashCopeService, MetadataMode metadataMode) { this(dashCopeService,metadataMode,QWenEmbeddingOptions.builder().withModel(DEFAULT_OPENAI_EMBEDDING_MODEL).build()); //this(null,null,null); } - public QWenEmbeddingClient(DashCopeService dashCopeService,MetadataMode metadataMode,QWenEmbeddingOptions embeddingOptions) { + public QWenEmbeddingClient(DashsCopeService dashCopeService, MetadataMode metadataMode, QWenEmbeddingOptions embeddingOptions) { Assert.notNull(dashCopeService, "dashCopeService不能为空"); Assert.notNull(metadataMode, "metadataMode不能为空"); Assert.notNull(embeddingOptions, "embeddingOptions不能为空"); @@ -75,8 +72,8 @@ public List embed(Document document) { @Override public EmbeddingResponse call(EmbeddingRequest request) { return this.retryTemplate.execute(ctx -> { - org.springframework.ai.dashcope.DashCopeService.EmbeddingRequest embeddingRequest = new org.springframework.ai.dashcope.DashCopeService.EmbeddingRequest(this.embeddingOptions.getModel().value, request.getInstructions(), "document"); - org.springframework.ai.dashcope.DashCopeService.EmbeddingResponse embeddingResponse = this.dashCopeService.embeddingRequest(embeddingRequest).getBody(); + DashsCopeService.EmbeddingRequest embeddingRequest = new DashsCopeService.EmbeddingRequest(this.embeddingOptions.getModel().value, request.getInstructions(), "document"); + DashsCopeService.EmbeddingResponse embeddingResponse = this.dashCopeService.embeddingRequest(embeddingRequest).getBody(); if(embeddingResponse == null) { logger.warn("No {}",embeddingRequest); } @@ -86,7 +83,7 @@ public EmbeddingResponse call(EmbeddingRequest request) { }); } - private EmbeddingResponseMetadata generateResponseMetadata(String model,Usage usage) { + private EmbeddingResponseMetadata generateResponseMetadata(String model, DashsCopeService.Usage usage) { EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata(); metadata.put("model", model); metadata.put("total-tokens", usage.totalTokens()); diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenEmbeddingOptions.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenEmbeddingOptions.java similarity index 87% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenEmbeddingOptions.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenEmbeddingOptions.java index 1c64113..1d8b03d 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenEmbeddingOptions.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenEmbeddingOptions.java @@ -1,6 +1,6 @@ -package org.springframework.ai.dashcope.qwen; +package org.springframework.ai.dashscope.qwen; -import org.springframework.ai.dashcope.metadata.support.EmbeddingModel; +import org.springframework.ai.dashscope.metadata.support.EmbeddingModel; import org.springframework.ai.embedding.EmbeddingOptions; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenImageClient.java b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenImageClient.java similarity index 58% rename from models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenImageClient.java rename to models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenImageClient.java index 8d8baa3..e7b6439 100644 --- a/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashcope/qwen/QWenImageClient.java +++ b/models/spring-ai-dashscope/src/main/java/org/springframework/ai/dashscope/qwen/QWenImageClient.java @@ -1,19 +1,11 @@ -package org.springframework.ai.dashcope.qwen; - -import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +package org.springframework.ai.dashscope.qwen; import java.time.Duration; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.ai.dashcope.DashCopeService; -import org.springframework.ai.dashcope.DashCopeService.DashCopeApiException; -import org.springframework.ai.dashcope.DashCopeService.QWenImageRequest; -import org.springframework.ai.dashcope.DashCopeService.Input; -import org.springframework.ai.dashcope.DashCopeService.QWenImageResponse; -import org.springframework.ai.dashcope.DashCopeService.StatusStatus; -import org.springframework.ai.dashcope.DashCopeService.TaskMetrices; +import org.springframework.ai.dashscope.DashsCopeService; import org.springframework.ai.image.Image; import org.springframework.ai.image.ImageClient; import org.springframework.ai.image.ImageGeneration; @@ -32,32 +24,32 @@ public class QWenImageClient implements ImageClient { private final Logger logger = LoggerFactory.getLogger(QWenImageClient.class); - private final DashCopeService dashCopeService; + private final DashsCopeService dashCopeService; private final RetryTemplate retryTemplate = RetryTemplate.builder() .maxAttempts(10) - .retryOn(DashCopeApiException.class) + .retryOn(DashsCopeService.DashCopeApiException.class) .exponentialBackoff(Duration.ofMillis(2000), 5, Duration.ofMillis(3 * 60000)) .build(); - public QWenImageClient(DashCopeService dashCopeService) { + public QWenImageClient(DashsCopeService dashCopeService) { Assert.notNull(dashCopeService, "dashCopeService 不能为空"); this.dashCopeService = dashCopeService; } @Override public ImageResponse call(ImagePrompt imagePrompt) { - QWenImageResponse taskImageResponse = this.retryTemplate.execute(ctx -> { + DashsCopeService.QWenImageResponse taskImageResponse = this.retryTemplate.execute(ctx -> { ImageOptions imageOptions = imagePrompt.getOptions(); String instructions = imagePrompt.getInstructions().get(0).getText(); - QWenImageRequest imageRequest = new QWenImageRequest(new Input(instructions,null,null), null); - ResponseEntity responseEntity = this.dashCopeService.createQwenImageTask(imageRequest); + DashsCopeService.QWenImageRequest imageRequest = new DashsCopeService.QWenImageRequest(new DashsCopeService.Input(instructions,null,null), null); + ResponseEntity responseEntity = this.dashCopeService.createQwenImageTask(imageRequest); return responseEntity.getBody(); }); try { - if(taskImageResponse.output().taskStatus() == StatusStatus.PENDING) { + if(taskImageResponse.output().taskStatus() == DashsCopeService.StatusStatus.PENDING) { logger.info("任务提交成功,需要排队,休眠4秒"); Thread.sleep(4000); logger.info("休眠完成,查询任务结果!"); @@ -67,20 +59,20 @@ public ImageResponse call(ImagePrompt imagePrompt) { } return this.retryTemplate.execute(ctx -> { logger.info("请求查询结果"); - ResponseEntity responseEntity = this.dashCopeService.findImageTaskResult(taskImageResponse.output().taskId()); - QWenImageResponse resultImageResponse = responseEntity.getBody(); + ResponseEntity responseEntity = this.dashCopeService.findImageTaskResult(taskImageResponse.output().taskId()); + DashsCopeService.QWenImageResponse resultImageResponse = responseEntity.getBody(); logger.info("查询结果完成"); - if(resultImageResponse.output().taskStatus() != StatusStatus.SUCCEEDED) { - TaskMetrices taskMetrices = resultImageResponse.output().taskMetrices(); + if(resultImageResponse.output().taskStatus() != DashsCopeService.StatusStatus.SUCCEEDED) { + DashsCopeService.TaskMetrices taskMetrices = resultImageResponse.output().taskMetrices(); logger.info("任务还在进行生成中,一共有{}任务,完成{}个任务,请稍后",taskMetrices.total(),taskMetrices.succeeded()); - throw new DashCopeApiException("任务还在进行生成请稍后"); + throw new DashsCopeService.DashCopeApiException("任务还在进行生成请稍后"); } logger.info("查询结果完成,并封装数据"); return convertResponse(resultImageResponse); }); } - private ImageResponse convertResponse(QWenImageResponse qwenImageResponse) { + private ImageResponse convertResponse(DashsCopeService.QWenImageResponse qwenImageResponse) { if(qwenImageResponse == null) { logger.warn("No image response returned for request: {}",qwenImageResponse); return new ImageResponse(List.of()); diff --git a/models/spring-ai-dashscope/src/test/java/org/springframework/ai/dashcope/DashCopeTestConfiguration.java b/models/spring-ai-dashscope/src/test/java/org/springframework/ai/dashscope/DashsCopeTestConfiguration.java similarity index 56% rename from models/spring-ai-dashscope/src/test/java/org/springframework/ai/dashcope/DashCopeTestConfiguration.java rename to models/spring-ai-dashscope/src/test/java/org/springframework/ai/dashscope/DashsCopeTestConfiguration.java index 9aa7206..81ed6f3 100644 --- a/models/spring-ai-dashscope/src/test/java/org/springframework/ai/dashcope/DashCopeTestConfiguration.java +++ b/models/spring-ai-dashscope/src/test/java/org/springframework/ai/dashscope/DashsCopeTestConfiguration.java @@ -1,15 +1,15 @@ -package org.springframework.ai.dashcope; +package org.springframework.ai.dashscope; +import org.springframework.ai.dashscope.qwen.QWenChatClient; +import org.springframework.ai.dashscope.qwen.QWenEmbeddingClient; +import org.springframework.ai.dashscope.qwen.QWenImageClient; import org.springframework.context.annotation.Bean; import org.springframework.util.StringUtils; -import org.springframework.ai.dashcope.qwen.QWenChatClient; -import org.springframework.ai.dashcope.qwen.QWenEmbeddingClient; -import org.springframework.ai.dashcope.qwen.QWenImageClient; import org.springframework.ai.embedding.EmbeddingClient; import org.springframework.boot.SpringBootConfiguration; @SpringBootConfiguration -public class DashCopeTestConfiguration { +public class DashsCopeTestConfiguration { private String getApiKey() { System.out.println(System.getenv()); @@ -21,22 +21,22 @@ private String getApiKey() { } @Bean - public DashCopeService dashCopeService() { - return new DashCopeService(getApiKey()); + public DashsCopeService dashCopeService() { + return new DashsCopeService(getApiKey()); } @Bean - public QWenChatClient qWenChatClient(DashCopeService dashCopeService) { + public QWenChatClient qWenChatClient(DashsCopeService dashCopeService) { return new QWenChatClient(dashCopeService); } @Bean - public QWenImageClient qWenImageClient(DashCopeService dashCopeService) { + public QWenImageClient qWenImageClient(DashsCopeService dashCopeService) { return new QWenImageClient(dashCopeService); } @Bean - public EmbeddingClient qwenEmbeddingClient(DashCopeService dashCopeService) { + public EmbeddingClient qwenEmbeddingClient(DashsCopeService dashCopeService) { return new QWenEmbeddingClient(dashCopeService); }