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

feat: update dashscope agent #428

Merged
merged 1 commit into from
Feb 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi.DashScopeAgentRequest;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi.DashScopeAgentResponse;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi.DashScopeAgentRequest.DashScopeAgentRequestInput.DashScopeAgentRequestMessage;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi.DashScopeAgentRequest.DashScopeAgentRequestParameters.DashScopeAgentRequestRagOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.metadata.ChatGenerationMetadata;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.http.ResponseEntity;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
Expand Down Expand Up @@ -63,6 +67,7 @@ public DashScopeAgent(DashScopeAgentApi dashScopeAgentApi) {
.withMemoryId(null)
.withIncrementalOutput(false)
.withHasThoughts(false)
.withImages(null)
.withBizParams(null)
.build();
}
Expand Down Expand Up @@ -98,24 +103,30 @@ public Flux<ChatResponse> stream(Prompt prompt) {
}

private DashScopeAgentRequest toRequest(Prompt prompt, Boolean stream) {
if (prompt == null || prompt.getOptions() == null) {
if (prompt == null) {
throw new IllegalArgumentException("option is null");
}

String appId = null;
if (prompt.getOptions() instanceof DashScopeAgentOptions options) {
appId = options.getAppId();
}
DashScopeAgentOptions runtimeOptions = mergeOptions(prompt.getOptions());
String appId = runtimeOptions.getAppId();

if (appId == null) {
throw new IllegalArgumentException("appId must be set");
}

DashScopeAgentRagOptions ragOptions = runtimeOptions.getRagOptions();
return new DashScopeAgentRequest(appId,
new DashScopeAgentRequest.DashScopeAgentRequestInput(prompt.getContents(), this.options.getSessionId(),
this.options.getMemoryId(), this.options.getBizParams()),
new DashScopeAgentRequest.DashScopeAgentRequestParameters(this.options.getHasThoughts(),
stream && this.options.getIncrementalOutput()));
new DashScopeAgentRequest.DashScopeAgentRequestInput(null, prompt.getInstructions()
.stream()
.map(msg -> new DashScopeAgentRequestMessage(msg.getMessageType().getValue(), msg.getText()))
.toList(), runtimeOptions.getSessionId(), runtimeOptions.getMemoryId(), runtimeOptions.getImages(),
runtimeOptions.getBizParams()),
new DashScopeAgentRequest.DashScopeAgentRequestParameters(runtimeOptions.getHasThoughts(),
stream && runtimeOptions.getIncrementalOutput(),
ragOptions == null ? null
: new DashScopeAgentRequestRagOptions(ragOptions.getPipelineIds(),
ragOptions.getFileIds(), ragOptions.getMetadataFilter(), ragOptions.getTags(),
ragOptions.getStructuredFilter(), ragOptions.getSessionFileIds())));
}

private ChatResponse toChatResponse(DashScopeAgentResponse response) {
Expand All @@ -139,4 +150,10 @@ private ChatResponse toChatResponse(DashScopeAgentResponse response) {
return new ChatResponse(List.of(generation));
}

private DashScopeAgentOptions mergeOptions(ChatOptions chatOptions) {
DashScopeAgentOptions agentOptions = ModelOptionsUtils.copyToTarget(chatOptions, ChatOptions.class,
DashScopeAgentOptions.class);
return ModelOptionsUtils.merge(agentOptions, this.options, DashScopeAgentOptions.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;

import org.springframework.ai.chat.prompt.ChatOptions;
Expand All @@ -29,18 +30,30 @@

public class DashScopeAgentOptions implements ChatOptions {

@JsonProperty("app_id")
private String appId;

@JsonProperty("session_id")
private String sessionId;

@JsonProperty("memory_id")
private String memoryId;

@JsonProperty("incremental_output")
private Boolean incrementalOutput;

@JsonProperty("has_thoughts")
private Boolean hasThoughts;

@JsonProperty("images")
private List<String> images;

@JsonProperty("biz_params")
private JsonNode bizParams;

@JsonProperty("rag_options")
private DashScopeAgentRagOptions ragOptions;

@Override
public String getModel() {
return null;
Expand Down Expand Up @@ -121,6 +134,14 @@ public void setHasThoughts(Boolean hasThoughts) {
this.hasThoughts = hasThoughts;
}

public List<String> getImages() {
return images;
}

public void setImages(List<String> images) {
this.images = images;
}

public JsonNode getBizParams() {
return bizParams;
}
Expand All @@ -129,6 +150,14 @@ public void setBizParams(JsonNode bizParams) {
this.bizParams = bizParams;
}

public DashScopeAgentRagOptions getRagOptions() {
return ragOptions;
}

public void setRagOptions(DashScopeAgentRagOptions ragOptions) {
this.ragOptions = ragOptions;
}

@Override
public ChatOptions copy() {
return DashScopeAgentOptions.fromOptions(this);
Expand Down Expand Up @@ -186,11 +215,21 @@ public Builder withHasThoughts(Boolean hasThoughts) {
return this;
}

public Builder withImages(List<String> images) {
this.options.images = images;
return this;
}

public Builder withBizParams(JsonNode bizParams) {
this.options.bizParams = bizParams;
return this;
}

public Builder withRagOptions(DashScopeAgentRagOptions ragOptions) {
this.options.ragOptions = ragOptions;
return this;
}

public DashScopeAgentOptions build() {
return this.options;
}
Expand All @@ -205,6 +244,7 @@ public String toString() {
sb.append(", memoryId='").append(memoryId).append('\'');
sb.append(", incrementalOutput=").append(incrementalOutput);
sb.append(", hasThoughts=").append(hasThoughts);
sb.append(", images=").append(images);
sb.append(", bizParams=").append(bizParams);
sb.append('}');
return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.dashscope.agent;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;

import java.util.List;

/**
* @author kevinlin09
*/
public class DashScopeAgentRagOptions {

/** knowledge base ids */
@JsonProperty("pipeline_ids")
private List<String> pipelineIds;

/** file ids of knowledge base */
@JsonProperty("file_ids")
private List<String> fileIds;

/** tags of knowledge base */
@JsonProperty("tags")
private List<String> tags;

/** metadata filter of knowledge base query */
@JsonProperty("metadata_filter")
private JsonNode metadataFilter;

/** structured filter of knowledge base query */
@JsonProperty("structured_filter")
private JsonNode structuredFilter;

/** file ID is a temporary file associated with the current session */
@JsonProperty("session_file_ids")
private List<String> sessionFileIds;

public List<String> getPipelineIds() {
return pipelineIds;
}

public void setPipelineIds(List<String> pipelineIds) {
this.pipelineIds = pipelineIds;
}

public List<String> getFileIds() {
return fileIds;
}

public void setFileIds(List<String> fileIds) {
this.fileIds = fileIds;
}

public List<String> getTags() {
return tags;
}

public void setTags(List<String> tags) {
this.tags = tags;
}

public JsonNode getMetadataFilter() {
return metadataFilter;
}

public void setMetadataFilter(JsonNode metadataFilter) {
this.metadataFilter = metadataFilter;
}

public JsonNode getStructuredFilter() {
return structuredFilter;
}

public void setStructuredFilter(JsonNode structuredFilter) {
this.structuredFilter = structuredFilter;
}

public List<String> getSessionFileIds() {
return sessionFileIds;
}

public void setSessionFileIds(List<String> sessionFileIds) {
this.sessionFileIds = sessionFileIds;
}

public static DashScopeAgentRagOptions.Builder builder() {

return new DashScopeAgentRagOptions.Builder();
}

public static class Builder {

protected DashScopeAgentRagOptions options;

public Builder() {
this.options = new DashScopeAgentRagOptions();
}

public Builder(DashScopeAgentRagOptions options) {
this.options = options;
}

public Builder withPipelineIds(List<String> pipelineIds) {
this.options.pipelineIds = pipelineIds;
return this;
}

public Builder withFileIds(List<String> fileIds) {
this.options.fileIds = fileIds;
return this;
}

public Builder withTags(List<String> tags) {
this.options.tags = tags;
return this;
}

public Builder withMetadataFilter(JsonNode metadataFilter) {
this.options.metadataFilter = metadataFilter;
return this;
}

public Builder withStructuredFilter(JsonNode structuredFilter) {
this.options.structuredFilter = structuredFilter;
return this;
}

public Builder withSessionFileIds(List<String> sessionFileIds) {
this.options.sessionFileIds = sessionFileIds;
return this;
}

public DashScopeAgentRagOptions build() {
return this.options;
}

}

}
Loading
Loading