Skip to content

Commit

Permalink
Merge pull request #5 from yuluo-yx/0920-yuluo/optimize-1
Browse files Browse the repository at this point in the history
chore: update some code
  • Loading branch information
chickenlj authored Sep 20, 2024
2 parents fdf24d7 + 4974585 commit 8647cb6
Show file tree
Hide file tree
Showing 48 changed files with 359 additions and 401 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,4 @@
</releases>
</repository>
</repositories>
</project>
</project>
3 changes: 1 addition & 2 deletions spring-ai-alibaba-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
</dependency>

<!-- test dependencies -->

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
Expand All @@ -73,4 +72,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

/**
* @author nuocheng.lxm
* @author yuluo
* @date 2024/8/16 11:45
*/
@AutoConfiguration(after = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class,
Expand Down Expand Up @@ -227,33 +228,4 @@ private record ResolvedConnectionProperties(String baseUrl, String apiKey, Strin
CollectionUtils.toMultiValueMap(connectionHeaders));
}

/**
* Setting the API key.
* @param connectionProperties {@link DashScopeConnectionProperties}
*/
private void settingApiKey(DashScopeConnectionProperties connectionProperties) {
String apiKey;
try {
// It is recommended to set the key by defining the api-key in an environment
// variable.
var envKey = System.getenv(DASHSCOPE_API_KEY);
if (Objects.nonNull(envKey)) {
Constants.apiKey = envKey;
return;
}
if (Objects.nonNull(connectionProperties.getApiKey())) {
apiKey = connectionProperties.getApiKey();
}
else {
apiKey = ApiKey.getApiKey(null);
}

Constants.apiKey = apiKey;
}
catch (NoApiKeyException e) {
throw new RuntimeException(e.getMessage());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
public class DashScopeChatProperties extends DashScopeParentProperties {

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

/**
* Default DashScope Qwen Chat model.
* Default DashScope Chat model.
*/
public static final String DEFAULT_DEPLOYMENT_NAME = Generation.Models.QWEN_TURBO;

Expand All @@ -48,7 +48,7 @@ public class DashScopeChatProperties extends DashScopeParentProperties {
private static final Float DEFAULT_TEMPERATURE = 0.8f;

/**
* Enable qwen ai chat client.
* Enable Dashscope ai chat client.
*/
private boolean enabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static com.alibaba.cloud.ai.dashscope.common.DashScopeApiConstants.DEFAULT_READ_TIMEOUT;

/**
* Spring Cloud Alibaba AI TongYi LLM connection properties.
* Spring AI Alibaba TongYi LLM connection properties.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
public class DashScopeImageProperties extends DashScopeParentProperties {

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

/**
* Default TongYi Chat model.
* Default DashScope Chat model.
*/
public static final String DEFAULT_IMAGES_MODEL_NAME = ImageSynthesis.Models.WANX_V1;

/**
* Enable TongYiQWEN ai images client.
* Enable DashScope ai images client.
*/
private boolean enabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

String getWorkspaceId() {
public String getWorkspaceId() {
return workspaceId;
}

void setWorkspaceId(String workspaceId) {
public void setWorkspaceId(String workspaceId) {
this.workspaceId = workspaceId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

/**
* @author nuocheng.lxm
* @author yuluo
* @date 2024/7/31 14:15
*/
public class DashScopeApi {
Expand Down Expand Up @@ -345,28 +346,19 @@ public record DocumentChunk(@JsonProperty("chunk_id") int chunkId, @JsonProperty
}
}

/**
* 上传文件 返回文件ID
* @param file
* @param request
* @return
*/
public String upload(File file, UploadRequest request) {
// 申请上传
ResponseEntity<UploadLeaseResponse> responseEntity = uploadLease(request);
var uploadLeaseResponse = responseEntity.getBody();
if (uploadLeaseResponse == null) {
throw new DashScopeException(ErrorCodeEnum.READER_APPLY_LEASE_ERROR);
}
if (!"SUCCESS".equals(uploadLeaseResponse.code().toUpperCase())) {
if (!"SUCCESS".equalsIgnoreCase(uploadLeaseResponse.code())) {
throw new DashScopeException("ApplyLease Failed,code:%s,message:%s".formatted(uploadLeaseResponse.code(),
uploadLeaseResponse.message()));
}
// 上传文件
uploadFile(file, uploadLeaseResponse);
// 开始解析
String fileId = addFile(uploadLeaseResponse.data.leaseId(), request);
return fileId;
return addFile(uploadLeaseResponse.data.leaseId(), request);
}

public ResponseEntity<CommonResponse<QueryFileResponseData>> queryFileInfo(String categoryId,
Expand Down Expand Up @@ -451,8 +443,7 @@ public String getFilename() {
}
};
HttpEntity<InputStreamResource> requestEntity = new HttpEntity<>(resource, headers);
ResponseEntity<Void> response = restTemplate.exchange(new URI(uploadParam.url), HttpMethod.PUT,
requestEntity, Void.class);
restTemplate.exchange(new URI(uploadParam.url), HttpMethod.PUT, requestEntity, Void.class);
}
catch (Exception ex) {
throw new DashScopeException("Upload File Failed", ex);
Expand Down Expand Up @@ -672,8 +663,8 @@ public void upsertPipeline(List<Document> documents, DashScopeStoreOptions store
retrieverOptions.getRerankModelName())),
retrieverOptions.getRerankMinScore(), retrieverOptions.getRerankTopN()));
List<String> documentIdList = documents.stream()
.filter(e -> e.getId() != null)
.map(Document::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
UpsertPipelineRequest upsertPipelineRequest = new UpsertPipelineRequest(storeOptions.getIndexName(),
"MANAGED_SHARED", null, "unstructured", "recommend",
Expand All @@ -689,7 +680,7 @@ public void upsertPipeline(List<Document> documents, DashScopeStoreOptions store
.retrieve()
.toEntity(UpsertPipelineResponse.class);
if (upsertPipelineResponse.getBody() == null
|| !"SUCCESS".equals(upsertPipelineResponse.getBody().status.toUpperCase())) {
|| !"SUCCESS".equalsIgnoreCase(upsertPipelineResponse.getBody().status)) {
throw new DashScopeException(ErrorCodeEnum.CREATE_INDEX_ERROR);
}
String pipelineId = upsertPipelineResponse.getBody().id;
Expand All @@ -698,8 +689,7 @@ public void upsertPipeline(List<Document> documents, DashScopeStoreOptions store
.body(upsertPipelineRequest)
.retrieve()
.toEntity(StartPipelineResponse.class);
if (startPipelineResponse.getBody() == null
|| !"SUCCESS".equals(startPipelineResponse.getBody().code.toUpperCase())
if (startPipelineResponse.getBody() == null || !"SUCCESS".equalsIgnoreCase(startPipelineResponse.getBody().code)
|| startPipelineResponse.getBody().ingestionId == null) {
throw new DashScopeException(ErrorCodeEnum.INDEX_ADD_DOCUMENT_ERROR);
}
Expand All @@ -715,7 +705,7 @@ public boolean deletePipelineDocument(String pipelineId, List<String> idList) {
.retrieve()
.toEntity(DelePipelineDocumentResponse.class);
if (deleDocumentResponse == null || deleDocumentResponse.getBody() == null
|| !"SUCCESS".equals(deleDocumentResponse.getBody().code.toUpperCase())) {
|| !"SUCCESS".equalsIgnoreCase(deleDocumentResponse.getBody().code)) {
return false;
}
return true;
Expand All @@ -736,11 +726,11 @@ public List<Document> retriever(String pipelineId, String query, DashScopeDocume
.retrieve()
.toEntity(DocumentRetrieveResponse.class);
if (deleDocumentResponse == null || deleDocumentResponse.getBody() == null
|| !"SUCCESS".equals(deleDocumentResponse.getBody().code.toUpperCase())) {
|| !"SUCCESS".equalsIgnoreCase(deleDocumentResponse.getBody().code)) {
throw new DashScopeException(ErrorCodeEnum.RETRIEVER_DOCUMENT_ERROR);
}
List<DocumentRetrieveResponse.DocumentRetrieveResponseNode> nodeList = deleDocumentResponse.getBody().nodes;
if (nodeList == null || nodeList.size() == 0) {
if (nodeList == null || nodeList.isEmpty()) {
return new ArrayList<>();
}
List<Document> documents = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public record SpeechRequest(
*/
public enum Voice {

// @formatter:off
// @formatter:off
@JsonProperty("alloy") ALLOY("alloy"),
@JsonProperty("echo") ECHO("echo"),
@JsonProperty("fable") FABLE("fable"),
Expand All @@ -191,7 +191,7 @@ public enum Voice {

public final String value;

private Voice(String value) {
Voice(String value) {
this.value = value;
}

Expand Down Expand Up @@ -566,7 +566,7 @@ public record StructuredResponse(
// @formatter:on

/**
* Extracted word and it corresponding timestamps.
* Extracted word and it's corresponding timestamps.
*
* @param word The text content of the word.
* @param start The start time of the word in seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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

import java.nio.ByteBuffer;

import com.alibaba.cloud.ai.dashscope.api.DashScopeAudioApi;
import com.alibaba.cloud.ai.dashscope.audio.speech.Speech;
import com.alibaba.cloud.ai.dashscope.audio.speech.SpeechModel;
Expand All @@ -29,7 +27,6 @@
import com.alibaba.cloud.ai.dashscope.metadata.audio.DashScopeAudioSpeechResponseMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.metadata.RateLimit;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.support.RetryTemplate;
Expand All @@ -38,8 +35,6 @@

import reactor.core.publisher.Flux;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
* OpenAI audio speech client implementation for backed by {@link DashScopeAudioApi}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
package com.alibaba.cloud.ai.dashscope.audio;

import com.alibaba.cloud.ai.dashscope.api.DashScopeAudioApi;
import com.alibaba.cloud.ai.dashscope.metadata.audio.DashScopeAudioTranscriptionResponseMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ai.audio.transcription.AudioTranscription;
import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.chat.metadata.RateLimit;
import org.springframework.ai.model.Model;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.Assert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.ai.audio.transcription.AudioTranscriptionOptions;
import org.springframework.ai.model.ModelOptions;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.alibaba.cloud.ai.dashscope.audio.speech;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.alibaba.cloud.ai.dashscope.audio.speech;

import java.nio.ByteBuffer;

import org.springframework.ai.model.Model;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.alibaba.cloud.ai.dashscope.audio.speech;

import java.nio.ByteBuffer;

import reactor.core.publisher.Flux;

import org.springframework.ai.model.StreamingModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.alibaba.cloud.ai.dashscope.audio.speech;

import java.nio.ByteBuffer;

import reactor.core.publisher.Flux;

import org.springframework.ai.model.StreamingModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeType;

/**
* {@link ChatModel} implementation for {@literal Alibaba DashScope} backed by
* {@link Generation}.
*
* @author yuluo
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
* @see ChatModel
* @see com.alibaba.dashscope.aigc.generation
*/
public class DashScopeChatModel extends AbstractToolCallSupport implements ChatModel {

private static final Logger logger = LoggerFactory.getLogger(DashScopeChatModel.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public class DashScopeChatOptions implements FunctionCallingOptions, ChatOptions
private @JsonProperty("top_k") Integer topK;

/**
*
*
* <ul>
* <li>stop参数用于实现内容生成过程的精确控制,在生成内容即将包含指定的字符串或token_ids时自动停止,生成内容不包含指定的内容。
* <p>例如,如果指定stop为"你好",表示将要生成"你好"时停止;如果指定stop为[37763, 367],表示将要生成"Observation"时停止。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
* DashScope Embedding Model implementation.
*
* @author nuocheng.lxm
* @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @date 2024/7/31 10:57
*/
public class DashScopeEmbeddingModel extends AbstractEmbeddingModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

/**
* @author nuocheng.lxm
* @author why_ohh
* @author yuluo
* @author <a href="mailto:550588941@qq.com">why_ohh</a>
* @date 2024/8/1 11:14
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
Expand Down
Loading

0 comments on commit 8647cb6

Please sign in to comment.