🧩 编写 Agent 组件
# 创建组件
在 Spring Boot 扫描范围内创建 ChatAgentCmp.java:
import com.yomahub.liteflow.agent.context.LiteFlowAgentContext;
import com.yomahub.liteflow.agent.harness.component.HarnessAgentComponent;
import com.yomahub.liteflow.agent.model.ModelSpec;
import com.yomahub.liteflow.agent.openai.DeepSeek;
import org.springframework.stereotype.Component;
@Component("chatAgent")
public class ChatAgentCmp extends HarnessAgentComponent {
@Override
protected ModelSpec<?> model() {
return DeepSeek.of("deepseek-flash").stream(true);
}
@Override
protected String systemPrompt() {
return "你是一个简洁的助手,用中文回答。";
}
@Override
protected String userPrompt(LiteFlowAgentContext context) {
Object input = getSlot().getChainReqData(getSlot().getChainId());
return input == null ? "" : input.toString();
}
@Override
protected boolean enableShellTool() {
return false;
}
}
model() 选择模型,systemPrompt() 定义职责,userPrompt(context) 取得本次输入。采样参数沿用模型默认值。enableShellTool() 关闭命令工具,需要执行脚本时按 Shell 工具 开启。
# 将组件加入流程
创建 src/main/resources/agent/flow.el.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
<flow>
<chain name="chatChain">
THEN(chatAgent);
</chain>
</flow>
EL 中的 chatAgent 与组件的 Spring Bean 名一致。
# 组件方法的调用时机
model() 选择模型,systemPrompt() 定义固定职责,userPrompt(LiteFlowAgentContext context) 为每次调用提供输入。模型、工具和中间件等声明在运行时构建时装配,不要在其中缓存某个请求的数据。
框架会在自定义系统提示词前加入默认行为说明:使用用户提问的语言回答,在工具调用前给出简短的动作说明。需要按本次请求补充系统提示词时,使用 transformSystemPrompt(prompt, context)。
process() 由框架统一管理,不需要覆写。组件获得的 LiteFlowAgentContext 对应本次调用,可读取会话 ID、节点 ID、请求 ID,以及完成调用后的 Token 用量;不要把它保存在组件实例字段中供下一次请求使用。
下一步:获取 Agent 结果,或直接接入流式输出。
帮助我们改善此文档 (opens new window)
上次更新: 2026/09/20, 11:49:27



