com.alibaba.dashscope.conversation.qwen.QWenConversationParam Maven / Gradle / Ivy
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.conversation.qwen;
import static com.alibaba.dashscope.utils.ApiKeywords.*;
import com.alibaba.dashscope.common.ErrorType;
import com.alibaba.dashscope.common.Protocol;
import com.alibaba.dashscope.conversation.ConversationMessage;
import com.alibaba.dashscope.conversation.ConversationMessageStatus;
import com.alibaba.dashscope.conversation.ConversationParam;
import com.alibaba.dashscope.conversation.ConversationResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.google.gson.JsonObject;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.apache.http.HttpStatus;
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
@SuperBuilder
public class QWenConversationParam extends ConversationParam {
public static final String QWEN_V1 = "qwen-v1";
/** The max_length parameter for the generation algo. */
private Integer maxLength;
/** The top_p parameter for the generation algo. */
private Double topP;
/** Enable search. */
private boolean enableSearch = false;
/** Multi-modal conversation. */
@Builder.Default private boolean multiModal = false;
/** Enable debug or not. */
@Builder.Default private boolean enableDebug = false;
/**
* The n-round history to use, if > 0, the conversation api will choose 2 * nHistory ChatMessage
* to pass to the server side.
*/
private Integer nHistory;
public JsonObject getParameters() {
JsonObject json = new JsonObject();
if (topP != null) {
json.addProperty(TOP_P, topP);
}
if (maxLength != null) {
json.addProperty(MAX_LENGTH, maxLength);
}
json.addProperty(M6_SEARCH_IN_FIRST_QUERY, 0);
json.addProperty(M6_NUM_WEB_SEARCH, enableSearch ? 5 : 0);
return json;
}
@Override
public String url() {
return "/services/aigc/text-generation/generation?request_id=" + this.getMsgId();
}
@Override
public String buildMessageBody(String protocol) {
ConversationMessage inputMessage =
ConversationMessage.buildInputMessageWithHistory(
this.getMsgId(),
this.getModel(),
this.getPrompt(),
this.getHistory(),
this.getNHistory(),
this.getParameters(),
this.isStream(),
this.isEnableDebug());
if (this.isMultiModal()) {
inputMessage =
ConversationMessage.buildMultiModalInputMessageWithHistory(
this.getMsgId(),
this.getModel(),
this.getPrompt(),
this.getHistory(),
this.getNHistory(),
this.getParameters(),
this.isStream(),
this.isEnableDebug());
}
if (Protocol.HTTP.getValue().equals(protocol)) {
JsonObject json = new JsonObject();
json.addProperty(MODEL, inputMessage.getPayload().getModel());
json.add(PARAMETERS, inputMessage.getPayload().getParameters());
json.add(INPUT, inputMessage.getPayload().getInput());
return json.toString();
} else if (Protocol.WEBSOCKET.getValue().equals(protocol)) {
return JsonUtils.toJson(inputMessage);
} else {
throw new ApiException(
ConversationMessageStatus.builder()
.code(ErrorType.PROTOCOL_UNSUPPORTED.getValue())
.message(protocol)
.statusCode(HttpStatus.SC_BAD_REQUEST)
.build());
}
}
@Override
public Class extends ConversationResult> resultType() {
return QWenConversationResult.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy