Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "chore: update some code" #3

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 2 additions & 1 deletion spring-ai-alibaba-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
</dependency>

<!-- test dependencies -->

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

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

/**
* @author nuocheng.lxm
* @author yuluo
* @date 2024/8/16 11:45
*/
@AutoConfiguration(after = { RestClientAutoConfiguration.class, WebClientAutoConfiguration.class,
Expand Down Expand Up @@ -228,4 +227,33 @@ 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 AI Alibaba configuration prefix.
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String CONFIG_PREFIX = "spring.ai.dashscope.chat";

/**
* Default DashScope Chat model.
* Default DashScope Qwen 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 Dashscope ai chat client.
* Enable qwen 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 AI Alibaba TongYi LLM connection properties.
* Spring Cloud Alibaba AI TongYi LLM connection properties.
*
* @author yuluo
* @author <a href="mailto:[email protected]">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 AI Alibaba configuration prefix.
* Spring Cloud Alibaba AI configuration prefix.
*/
public static final String CONFIG_PREFIX = "spring.ai.dashscope.image";

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

/**
* Enable DashScope ai images client.
* Enable TongYiQWEN 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;
}

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

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

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

/**
* @author nuocheng.lxm
* @author yuluo
*
* @date 2024/7/31 14:15
*/
public class DashScopeApi {
Expand Down Expand Up @@ -347,19 +345,28 @@ 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".equalsIgnoreCase(uploadLeaseResponse.code())) {
if (!"SUCCESS".equals(uploadLeaseResponse.code().toUpperCase())) {
throw new DashScopeException("ApplyLease Failed,code:%s,message:%s".formatted(uploadLeaseResponse.code(),
uploadLeaseResponse.message()));
}
// 上传文件
uploadFile(file, uploadLeaseResponse);
return addFile(uploadLeaseResponse.data.leaseId(), request);
// 开始解析
String fileId = addFile(uploadLeaseResponse.data.leaseId(), request);
return fileId;
}

public ResponseEntity<CommonResponse<QueryFileResponseData>> queryFileInfo(String categoryId,
Expand Down Expand Up @@ -444,7 +451,8 @@ public String getFilename() {
}
};
HttpEntity<InputStreamResource> requestEntity = new HttpEntity<>(resource, headers);
restTemplate.exchange(new URI(uploadParam.url), HttpMethod.PUT, requestEntity, Void.class);
ResponseEntity<Void> response = 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 @@ -664,8 +672,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 @@ -681,7 +689,7 @@ public void upsertPipeline(List<Document> documents, DashScopeStoreOptions store
.retrieve()
.toEntity(UpsertPipelineResponse.class);
if (upsertPipelineResponse.getBody() == null
|| !"SUCCESS".equalsIgnoreCase(upsertPipelineResponse.getBody().status)) {
|| !"SUCCESS".equals(upsertPipelineResponse.getBody().status.toUpperCase())) {
throw new DashScopeException(ErrorCodeEnum.CREATE_INDEX_ERROR);
}
String pipelineId = upsertPipelineResponse.getBody().id;
Expand All @@ -691,7 +699,7 @@ public void upsertPipeline(List<Document> documents, DashScopeStoreOptions store
.retrieve()
.toEntity(StartPipelineResponse.class);
if (startPipelineResponse.getBody() == null
|| !"SUCCESS".equalsIgnoreCase(startPipelineResponse.getBody().code)
|| !"SUCCESS".equals(startPipelineResponse.getBody().code.toUpperCase())
|| startPipelineResponse.getBody().ingestionId == null) {
throw new DashScopeException(ErrorCodeEnum.INDEX_ADD_DOCUMENT_ERROR);
}
Expand All @@ -707,7 +715,7 @@ public boolean deletePipelineDocument(String pipelineId, List<String> idList) {
.retrieve()
.toEntity(DelePipelineDocumentResponse.class);
if (deleDocumentResponse == null || deleDocumentResponse.getBody() == null
|| !"SUCCESS".equalsIgnoreCase(deleDocumentResponse.getBody().code)) {
|| !"SUCCESS".equals(deleDocumentResponse.getBody().code.toUpperCase())) {
return false;
}
return true;
Expand All @@ -728,11 +736,11 @@ public List<Document> retriever(String pipelineId, String query, DashScopeDocume
.retrieve()
.toEntity(DocumentRetrieveResponse.class);
if (deleDocumentResponse == null || deleDocumentResponse.getBody() == null
|| !"SUCCESS".equalsIgnoreCase(deleDocumentResponse.getBody().code)) {
|| !"SUCCESS".equals(deleDocumentResponse.getBody().code.toUpperCase())) {
throw new DashScopeException(ErrorCodeEnum.RETRIEVER_DOCUMENT_ERROR);
}
List<DocumentRetrieveResponse.DocumentRetrieveResponseNode> nodeList = deleDocumentResponse.getBody().nodes;
if (nodeList == null || nodeList.isEmpty()) {
if (nodeList == null || nodeList.size() == 0) {
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;

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

Expand Down Expand Up @@ -481,7 +481,7 @@ public Class<?> getResponseType() {
*/
@JsonInclude(Include.NON_NULL)
public record TranslationRequest(
// @formatter:off
// @formatter:off
@JsonProperty("file") byte[] file,
@JsonProperty("model") String model,
@JsonProperty("prompt") String prompt,
Expand Down Expand Up @@ -557,7 +557,7 @@ public TranslationRequest build() {
*/
@JsonInclude(Include.NON_NULL)
public record StructuredResponse(
// @formatter:off
// @formatter:off
@JsonProperty("language") String language,
@JsonProperty("duration") Float duration,
@JsonProperty("text") String text,
Expand All @@ -566,7 +566,7 @@ public record StructuredResponse(
// @formatter:on

/**
* Extracted word and it's corresponding timestamps.
* Extracted word and it corresponding timestamps.
*
* @param word The text content of the word.
* @param start The start time of the word in seconds.
Expand Down Expand Up @@ -600,7 +600,7 @@ public record Word(
*/
@JsonInclude(Include.NON_NULL)
public record Segment(
// @formatter:off
// @formatter:off
@JsonProperty("id") Integer id,
@JsonProperty("seek") Integer seek,
@JsonProperty("start") Float start,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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 @@ -27,6 +29,7 @@
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 @@ -35,6 +38,8 @@

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,14 +17,18 @@
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,6 +24,7 @@
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,6 +16,7 @@

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,6 +16,8 @@

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,6 +16,8 @@

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,6 +16,8 @@

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,15 +41,6 @@
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:[email protected]">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 Expand Up @@ -150,7 +141,7 @@ public Flux<ChatResponse> stream(Prompt prompt) {
@SuppressWarnings("null")
String requestId = chatCompletion2.requestId();

// @formatter:off
// @formatter:off
List<Generation> generations = chatCompletion2.output().choices().stream().map(choice -> {
if (choice.message().role() != null) {
roleMap.putIfAbsent(requestId, choice.message().role().name());
Expand Down
Loading
Loading