Skip to content

Commit

Permalink
修复包结构命名错误
Browse files Browse the repository at this point in the history
  • Loading branch information
XYWENJIE committed Feb 23, 2024
1 parent dba0f9e commit 7340c69
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 86 deletions.
5 changes: 5 additions & 0 deletions models/spring-ai-dashscope/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
阿里云的灵积模块
支持以下模块

1. 通义千问
2. 通义万相
6 changes: 6 additions & 0 deletions models/spring-ai-dashscope/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.ai.dashcope;
package org.springframework.ai.dashscope;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -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";

Expand All @@ -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<HttpHeaders> jsonContentHeaders = headers -> {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -135,6 +135,15 @@ public ChatCompletionRequest(List<ChatCompletionMessage> 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(
Expand Down Expand Up @@ -298,6 +307,10 @@ public ResponseEntity<EmbeddingResponse> embeddingRequest(EmbeddingRequest embed
.body(embeddingRequest).retrieve()
.toEntity(EmbeddingResponse.class);
}

public ResponseEntity<CharGLMResponse> 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\"}";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.ai.dashcope.metadata;
package org.springframework.ai.dashscope.metadata;

import java.time.Duration;

Expand Down
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ChatCompletion> response) {
public static RateLimit extractAiResponseHeaders(ResponseEntity<DashsCopeService.ChatCompletion> response) {
//Long requestLimit = getHeaderAsLong(response,)
return new QwenRateLimit(null,null,null,null,null,null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.ai.dashcope.qwen;
package org.springframework.ai.dashscope.qwen;

import java.io.IOException;
import java.time.Duration;
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -58,7 +54,7 @@ public QWenChatClient(DashCopeService dashCopeService,QWenChatOptions qWenChatOp
public ChatResponse call(Prompt prompt) {
return this.retryTemplate.execute(ctx -> {

ResponseEntity<ChatCompletion> completionEntity = this.dashCopeService.chatCompletionEntity(createRequest(prompt));
ResponseEntity<DashsCopeService.ChatCompletion> completionEntity = this.dashCopeService.chatCompletionEntity(createRequest(prompt));

var chatCompletion = completionEntity.getBody();
if(chatCompletion == null) {
Expand All @@ -80,7 +76,7 @@ public ChatResponse call(Prompt prompt) {
public Flux<ChatResponse> stream(Prompt prompt) {
return this.retryTemplate.execute(ctx -> {

Flux<ChatCompletion> completionChunks = this.dashCopeService.chatCompletionStream(createRequest(prompt));
Flux<DashsCopeService.ChatCompletion> completionChunks = this.dashCopeService.chatCompletionStream(createRequest(prompt));
ConcurrentHashMap<String, String> roleMap = new ConcurrentHashMap();

return completionChunks.map(chunk -> {
Expand All @@ -100,14 +96,14 @@ public Flux<ChatResponse> stream(Prompt prompt) {
});
}

private ChatCompletionRequest createRequest(Prompt prompt) {
private DashsCopeService.ChatCompletionRequest createRequest(Prompt prompt) {
List<Message> messages = prompt.getInstructions();
List<ChatCompletionMessage> chatCompletionMessages = messages.stream()
.map(m -> new ChatCompletionMessage(m.getMessageType().getValue(),
List<DashsCopeService.ChatCompletionMessage> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.springframework.ai.dashcope.qwen;
package org.springframework.ai.dashscope.qwen;

import static org.assertj.core.api.Assertions.contentOf;

Expand All @@ -7,24 +7,21 @@

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;
import org.springframework.ai.embedding.Embedding;
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 {
Expand All @@ -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 <T extends Object, E extends Throwable> void onError(org.springframework.retry.RetryContext context, org.springframework.retry.RetryCallback<T,E> callback, Throwable throwable) {
Expand All @@ -44,20 +41,20 @@ public <T extends Object, E extends Throwable> 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不能为空");
Expand All @@ -75,8 +72,8 @@ public List<Double> 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);
}
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 7340c69

Please sign in to comment.