com.dingtalk.baymax.framework.sdk.mercury.chain.Chain Maven / Gradle / Ivy
package com.dingtalk.baymax.framework.sdk.mercury.chain;
import com.alibaba.fastjson.annotation.JSONField;
import com.dingtalk.baymax.framework.sdk.mercury.annotation.ChainField;
import com.dingtalk.baymax.framework.sdk.mercury.constant.Constants;
import com.dingtalk.baymax.framework.sdk.mercury.domain.BaseModel;
import com.dingtalk.baymax.framework.sdk.mercury.interactive.BaseInteractive;
import com.dingtalk.baymax.framework.sdk.mercury.memory.BaseMemory;
import com.google.common.collect.ImmutableMap;
import org.jdeferred2.Promise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import static com.dingtalk.baymax.framework.sdk.mercury.constant.Constants.CONV_USER_OUT_MSG_KEY;
import static com.dingtalk.baymax.framework.sdk.mercury.constant.Constants.SKILL_OUTPUT_PROMISE_KEY;
/**
* 所有链都应该实现的基类
*
* @author xiaoxuan.lp
*/
public abstract class Chain extends BaseModel {
private static final Logger log = LoggerFactory.getLogger(Chain.class);
@ChainField(label = "AI卡片 | AI Card", name = "interactive", optional = true)
protected BaseInteractive interactive;
/**
* 内存
*/
@ChainField(label = "记忆 | Memory", name = "memory")
private BaseMemory memory;
@ChainField(label = "是否自动加载记忆", name = "autoLoadMemory")
private Boolean autoLoadMemory;
/**
* 是否打印
*/
private boolean verbose;
@Override
public void init() {
super.init();
if (null != memory) {
memory.init();
}
}
@Override
public void setup(Map context) {
super.setup(context);
if (null != memory) {
memory.setup(context);
}
}
@JSONField(serialize = false)
public abstract List getInputKeys();
@JSONField(serialize = false)
public abstract List getOutputKeys();
/**
* 直接请求,不带记忆功能
*
* @param inputs
* @return
*/
protected abstract Map call(Map inputs);
/**
* 异步方式直接请求,不带记忆功能
*
* @param inputs
* @return
*/
protected abstract CompletableFuture