|
|
@@ -1,5 +1,6 @@
|
|
|
package com.zsjz.ai.module.agent.config;
|
|
|
|
|
|
+import io.agentscope.core.model.Model;
|
|
|
import io.agentscope.harness.agent.HarnessAgent;
|
|
|
import io.agentscope.harness.agent.memory.compaction.CompactionConfig;
|
|
|
import io.agentscope.harness.agent.memory.compaction.ToolResultEvictionConfig;
|
|
|
@@ -27,22 +28,69 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Slf4j
|
|
|
public final class AgentRuntimeTuning {
|
|
|
|
|
|
+ /** 模型窗口缺失时的统一默认窗口(256K);正常链路由 AgentModelFactory 保证有值 */
|
|
|
+ private static final int DEFAULT_CONTEXT_WINDOW_SIZE = 262_144;
|
|
|
+
|
|
|
+ /** 非法触发比例的兜底(80%) */
|
|
|
+ private static final double DEFAULT_TRIGGER_RATIO = 0.8;
|
|
|
+
|
|
|
+ /** 老工具结果 prune:保护最近 15% 窗口的工具输出 */
|
|
|
+ private static final double PRUNE_PROTECT_RATIO = 0.15;
|
|
|
+
|
|
|
+ /** 老工具结果 prune:可裁剪量达到窗口 7.5% 才执行 */
|
|
|
+ private static final double PRUNE_MINIMUM_RATIO = 0.075;
|
|
|
+
|
|
|
+ /** 单条工具结果驱逐:窗口的 8%(tokens)× 4 chars/token */
|
|
|
+ private static final double EVICTION_RATIO = 0.08;
|
|
|
+
|
|
|
+ /** 单条工具结果驱逐的字符上下限(约 2K / 50K tokens) */
|
|
|
+ private static final int EVICTION_MIN_CHARS = 8_000;
|
|
|
+ private static final int EVICTION_MAX_CHARS = 200_000;
|
|
|
+
|
|
|
private AgentRuntimeTuning() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 按配置构建 CompactionConfig;只显式设配置项,其余保持框架默认
|
|
|
- * (keep 尾部动态 2k-8k token、老工具结果 prune、压缩前 flush memory、摘要用主模型)。
|
|
|
+ * 按配置构建 CompactionConfig(无模型窗口信息时按默认 256K)。
|
|
|
+ *
|
|
|
+ * <p>只显式设配置项,其余保持框架默认(keep 尾部动态 2k-8k token、
|
|
|
+ * 老工具结果 prune、压缩前 flush memory、摘要用主模型)。
|
|
|
*/
|
|
|
public static CompactionConfig buildCompactionConfig(AgentProperties props) {
|
|
|
+ return buildCompactionConfig(props, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按配置构建 CompactionConfig:触发线 = {@code 窗口 × triggerRatio}(默认 80%)。
|
|
|
+ *
|
|
|
+ * <p>窗口一定存在(模型页设置,未设置由 AgentModelFactory 统一按 256K 下发),
|
|
|
+ * 因此不再保留 trigger-tokens/reserved 静态阈值配置。
|
|
|
+ *
|
|
|
+ * @param props agent 配置
|
|
|
+ * @param contextWindowSize 模型上下文窗口(tokens);{@code <=0} 时按默认 256K
|
|
|
+ */
|
|
|
+ public static CompactionConfig buildCompactionConfig(AgentProperties props, int contextWindowSize) {
|
|
|
AgentProperties.Compaction compaction = props.getCompaction();
|
|
|
+ int window = contextWindowSize > 0 ? contextWindowSize : DEFAULT_CONTEXT_WINDOW_SIZE;
|
|
|
+ double ratio = normalizeRatio(compaction.getTriggerRatio());
|
|
|
+ int trigger = Math.max(1, (int) Math.floor(window * ratio));
|
|
|
+ int reserved = Math.max(1, window - trigger);
|
|
|
+ int pruneProtect = Math.max(1_024, (int) Math.floor(window * PRUNE_PROTECT_RATIO));
|
|
|
+ int pruneMinimum = Math.max(512, (int) Math.floor(window * PRUNE_MINIMUM_RATIO));
|
|
|
CompactionConfig.Builder builder = CompactionConfig.builder()
|
|
|
- .triggerMessages(compaction.getTriggerMessages());
|
|
|
- if (compaction.getTriggerTokens() > 0) {
|
|
|
- // 静态 token 阈值(运行时框架会钳制到 min(阈值, 模型窗口 − reserved),防超窗)
|
|
|
- builder.triggerTokens(compaction.getTriggerTokens());
|
|
|
- }
|
|
|
- builder.reserved(compaction.getReserved());
|
|
|
+ // 纯 token 策略:条数触发固定关闭
|
|
|
+ .triggerMessages(0)
|
|
|
+ .triggerTokens(trigger)
|
|
|
+ .reserved(reserved)
|
|
|
+ // 老工具结果 prune 同样按窗口比例:256K 下约 39K/20K,与框架默认基本一致;
|
|
|
+ // 64K 下约 9.8K/4.9K,避免保护窗口过大导致 prune 永不触发
|
|
|
+ .prune(CompactionConfig.PruneConfig.builder()
|
|
|
+ .protectTokens(pruneProtect)
|
|
|
+ .minimumTokens(pruneMinimum)
|
|
|
+ .maxOutputChars(2_000)
|
|
|
+ .build());
|
|
|
+ log.info("上下文压缩按模型窗口计算: window={}, ratio={}, trigger={}, reserved={}, pruneProtect={}, pruneMinimum={}",
|
|
|
+ window, ratio, trigger, reserved, pruneProtect, pruneMinimum);
|
|
|
if (compaction.getSummaryModelId() != null && !compaction.getSummaryModelId().isBlank()) {
|
|
|
// 项目模型不经 ModelRegistry 注册,CompactionConfig.model(String) 解析不可靠;
|
|
|
// 本轮只支持主模型做摘要,配置位预留
|
|
|
@@ -58,22 +106,49 @@ public final class AgentRuntimeTuning {
|
|
|
return builder.build();
|
|
|
}
|
|
|
|
|
|
- /** 按配置构建工具结果驱逐配置。 */
|
|
|
- public static ToolResultEvictionConfig buildToolResultEvictionConfig(AgentProperties props) {
|
|
|
+ /** 比例必须在 (0,1) 内;非法值回落到默认 80% */
|
|
|
+ private static double normalizeRatio(double ratio) {
|
|
|
+ return ratio > 0 && ratio < 1 ? ratio : DEFAULT_TRIGGER_RATIO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按模型窗口构建工具结果驱逐配置:单条结果上限 ≈ 窗口的 8%(tokens)× 4 chars/token。
|
|
|
+ *
|
|
|
+ * <p>256K 下约 84K 字符(与框架默认 80K 接近);64K 下约 21K 字符,避免单条结果
|
|
|
+ * 吃掉三分之一窗口。
|
|
|
+ */
|
|
|
+ public static ToolResultEvictionConfig buildToolResultEvictionConfig(int contextWindowSize) {
|
|
|
+ int window = contextWindowSize > 0 ? contextWindowSize : DEFAULT_CONTEXT_WINDOW_SIZE;
|
|
|
+ long chars = Math.round(window * EVICTION_RATIO * 4);
|
|
|
+ int maxResultChars = (int) Math.min(EVICTION_MAX_CHARS,
|
|
|
+ Math.max(EVICTION_MIN_CHARS, chars));
|
|
|
return ToolResultEvictionConfig.builder()
|
|
|
- .maxResultChars(props.getToolResultEviction().getMaxResultChars())
|
|
|
+ .maxResultChars(maxResultChars)
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
/** 把压缩 + 工具结果驱逐配置挂到 builder 上(幂等,重复调用覆盖前值)。 */
|
|
|
public static void apply(HarnessAgent.Builder builder, AgentProperties props) {
|
|
|
+ apply(builder, props, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把压缩 + 工具结果驱逐配置挂到 builder 上;有模型实例时按其上下文窗口计算
|
|
|
+ * 80% 压缩触发线。
|
|
|
+ */
|
|
|
+ public static void apply(HarnessAgent.Builder builder, AgentProperties props, Model model) {
|
|
|
+ int contextWindowSize = model == null ? 0 : model.getContextWindowSize();
|
|
|
+ int effectiveWindow = contextWindowSize > 0 ? contextWindowSize : DEFAULT_CONTEXT_WINDOW_SIZE;
|
|
|
if (props.getCompaction().isEnabled()) {
|
|
|
- builder.compaction(buildCompactionConfig(props));
|
|
|
+ builder.compaction(buildCompactionConfig(props, contextWindowSize));
|
|
|
} else {
|
|
|
builder.disableCompaction();
|
|
|
}
|
|
|
if (props.getToolResultEviction().isEnabled()) {
|
|
|
- builder.toolResultEviction(buildToolResultEvictionConfig(props));
|
|
|
+ ToolResultEvictionConfig eviction = buildToolResultEvictionConfig(contextWindowSize);
|
|
|
+ builder.toolResultEviction(eviction);
|
|
|
+ log.info("工具结果驱逐按模型窗口计算: window={}, maxResultChars={}",
|
|
|
+ effectiveWindow, eviction.getMaxResultChars());
|
|
|
} else {
|
|
|
builder.disableToolResultEviction();
|
|
|
}
|