🧩 编写 Agent 组件
# 最简单的 Agent 组件
Agent 组件继承 ReActAgentComponent,至少实现三个方法:
@Component("deepseekAgent")
public class DeepSeekAgentCmp extends ReActAgentComponent {
// 声明使用的模型
@Override
protected ModelSpec<?> model() {
return DeepSeek.of("deepseek-v4-flash");
}
// 系统提示词(只在首次构建时调用)
@Override
protected String systemPrompt() {
return "你是一名简洁的中文助理,回答严格控制在两句话以内。";
}
// 用户消息(每次调用都会执行)
@Override
protected String userPrompt() {
Object req = getSlot().getChainReqData(getSlot().getChainId());
return req == null ? "" : req.toString();
}
}
三个必须实现的方法:
| 方法 | 说明 |
|---|---|
model() | 返回 ModelSpec<?>,声明使用哪个平台、哪个模型 |
systemPrompt() | 创建 Agent 时使用的系统提示词(框架会在它前面拼接一段统一系统提示词,见下方说明) |
userPrompt() | 每次调用时发送给 Agent 的用户消息 |
你的 systemPrompt() 不是最终系统提示词
框架会始终在你返回的内容前面拼接一段内置的统一系统提示词,最终下发给底层 ReActAgent 的是 内置提示词 + "\n\n" + 你的 systemPrompt()。这段内置提示词约定了模型的默认行为:
- 默认用用户提问所用的语言回答;如需固定输出语言,请在自己的
systemPrompt()里显式覆盖 - 每次调用工具前会先输出一两句推理摘要——这正是 Re-Act 日志和流式
agent.reasoning事件里出现简短推理片段的来源,属于预期行为,而非你的systemPrompt()没生效 - 当
systemPrompt()返回空字符串或空白时,框架会单独使用这段内置提示词
# 常用可选覆写
@Override
protected boolean enableShellTool() { return false; } // 关闭 Shell 工具
@Override
protected boolean enableWorkspaceFileTools() { return false; } // 关闭文件工具
# 在 EL 中编排
Agent 节点和普通 NodeComponent 一样使用:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
<flow>
<chain name="deepseekChain">
THEN(prepare, deepseekAgent, recordReply);
</chain>
</flow>
调用方式和普通 LiteFlow 链路一致:
LiteflowResponse response = flowExecutor.execute2Resp("deepseekChain", "用一句话介绍 LiteFlow");
if (response.isSuccess()) {
Object reply = response.getSlot().getResponseData();
}
# ctx() 上下文
在 process() 生命周期内,可以通过 ctx() 获取执行上下文 ReActAgentContext,提供以下信息:
| 方法 | 说明 |
|---|---|
getSlot() | 当前 LiteFlow Slot |
getConversationId() | 当前会话 ID |
getAgentKey() | 当前 Agent 标识 |
getWorkspaceDir() | 当前会话的 workspace 目录 |
getChatUsage() | 本次调用累计的 token 用量;需等本轮 reasoning step 完成后才非 null,建议在 handleReply() 中读取 |
注意
ctx() 只能在 process() 生命周期内调用,包括 systemPrompt()、userPrompt()、自定义工具回调、Hook 回调和 handleReply()。不要在构造器或异步线程中调用。
帮助我们改善此文档 (opens new window)
上次更新: 2026/06/02, 00:29:19


