mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
feat: 贯通 context_window 配置并按 token 预算裁剪对话历史
This commit is contained in:
@@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS `agent_company_llm_settings` (
|
||||
`api_key_encrypted` varchar(2000) NOT NULL COMMENT 'API Key 密文',
|
||||
`api_key_suffix` varchar(16) NOT NULL DEFAULT '' COMMENT 'Key 末几位(展示掩码)',
|
||||
`model` varchar(200) NOT NULL COMMENT '默认模型名',
|
||||
`context_window` int UNSIGNED NULL COMMENT '模型上下文窗口(token);空则 Agent 按模型名估计',
|
||||
`extra_headers_json` json NULL COMMENT '可选额外请求头',
|
||||
`create_by` bigint UNSIGNED NULL COMMENT '创建人用户ID',
|
||||
`update_by` bigint UNSIGNED NULL COMMENT '更新人用户ID',
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
-- 公司 LLM 配置增加上下文窗口(token),供 Agent 裁剪历史;可空=由 Agent 按模型名估计
|
||||
ALTER TABLE `agent_company_llm_settings`
|
||||
ADD COLUMN `context_window` int UNSIGNED NULL COMMENT '模型上下文窗口(token),空则 Agent 启发式' AFTER `model`;
|
||||
+6
@@ -17,4 +17,10 @@ public class UpsertAgentLlmSettingsDTO {
|
||||
|
||||
/** 首次必填;后续留空表示不改 Key */
|
||||
private String apiKey;
|
||||
|
||||
/**
|
||||
* 模型上下文窗口(token)。空/不传表示不改(更新时)或由 Agent 估计(新建)。
|
||||
* 传正整数则写入;部分前端可用 0 表示清空为「自动估计」。
|
||||
*/
|
||||
private Integer contextWindow;
|
||||
}
|
||||
|
||||
+4
@@ -33,6 +33,10 @@ public class AgentCompanyLlmSettingsPO {
|
||||
@TableField("model")
|
||||
private String model;
|
||||
|
||||
/** 模型上下文窗口(token);null 表示由 Agent 按模型名估计 */
|
||||
@TableField("context_window")
|
||||
private Integer contextWindow;
|
||||
|
||||
@TableField("extra_headers_json")
|
||||
private String extraHeadersJson;
|
||||
|
||||
|
||||
+2
@@ -12,5 +12,7 @@ public class AgentLlmRuntimeVO {
|
||||
private String baseUrl;
|
||||
private String apiKey;
|
||||
private String model;
|
||||
/** 上下文窗口 token;可能为 null */
|
||||
private Integer contextWindow;
|
||||
private Map<String, String> extraHeaders;
|
||||
}
|
||||
|
||||
+2
@@ -10,6 +10,8 @@ public class AgentLlmSettingsVO {
|
||||
private boolean configured;
|
||||
private String baseUrl;
|
||||
private String model;
|
||||
/** 上下文窗口 token;null 表示未配置、由 Agent 估计 */
|
||||
private Integer contextWindow;
|
||||
private String apiKeyMasked;
|
||||
private String updateTime;
|
||||
}
|
||||
|
||||
+28
@@ -86,6 +86,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
po.setApiKeyEncrypted(encrypted);
|
||||
po.setApiKeySuffix(suffix);
|
||||
po.setModel(model);
|
||||
po.setContextWindow(normalizeContextWindow(dto.getContextWindow()));
|
||||
po.setCreateBy(userId);
|
||||
po.setUpdateBy(userId);
|
||||
mapper.insert(po);
|
||||
@@ -95,6 +96,9 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
existing.setApiKeyEncrypted(encrypted);
|
||||
existing.setApiKeySuffix(suffix);
|
||||
existing.setModel(model);
|
||||
if (dto.getContextWindow() != null) {
|
||||
existing.setContextWindow(normalizeContextWindow(dto.getContextWindow()));
|
||||
}
|
||||
existing.setUpdateBy(userId);
|
||||
mapper.updateById(existing);
|
||||
}
|
||||
@@ -132,10 +136,19 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
vo.setBaseUrl(po.getBaseUrl());
|
||||
vo.setApiKey(crypto.decrypt(po.getApiKeyEncrypted()));
|
||||
vo.setModel(po.getModel());
|
||||
vo.setContextWindow(po.getContextWindow());
|
||||
vo.setExtraHeaders(parseHeaders(po.getExtraHeadersJson()));
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** 0 或负数视为清空(自动估计);正整数写入。 */
|
||||
private static Integer normalizeContextWindow(Integer raw) {
|
||||
if (raw == null || raw <= 0) {
|
||||
return null;
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
private Map<String, String> parseHeaders(String json) {
|
||||
if (!StringUtils.hasText(json)) {
|
||||
return Map.of();
|
||||
@@ -163,6 +176,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
vo.setConfigured(true);
|
||||
vo.setBaseUrl(po.getBaseUrl());
|
||||
vo.setModel(po.getModel());
|
||||
vo.setContextWindow(po.getContextWindow());
|
||||
vo.setApiKeyMasked("****" + (po.getApiKeySuffix() == null ? "" : po.getApiKeySuffix()));
|
||||
if (po.getUpdateTime() != null) {
|
||||
vo.setUpdateTime(po.getUpdateTime().format(ISO));
|
||||
@@ -180,6 +194,7 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
payload.put("base_url", runtime.getBaseUrl());
|
||||
payload.put("api_key", runtime.getApiKey());
|
||||
payload.put("model", runtime.getModel());
|
||||
payload.put("context_window", runtime.getContextWindow());
|
||||
payload.put("extra_headers", runtime.getExtraHeaders() == null ? Map.of() : runtime.getExtraHeaders());
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
redisKey(companyId),
|
||||
@@ -202,6 +217,19 @@ public class AgentCompanyLlmSettingsServiceImpl implements AgentCompanyLlmSettin
|
||||
vo.setBaseUrl(String.valueOf(map.get("base_url")));
|
||||
vo.setApiKey(String.valueOf(map.get("api_key")));
|
||||
vo.setModel(String.valueOf(map.get("model")));
|
||||
Object cw = map.get("context_window");
|
||||
if (cw instanceof Number n) {
|
||||
vo.setContextWindow(n.intValue());
|
||||
} else if (cw != null && StringUtils.hasText(String.valueOf(cw))
|
||||
&& !"null".equalsIgnoreCase(String.valueOf(cw))) {
|
||||
try {
|
||||
vo.setContextWindow(Integer.parseInt(String.valueOf(cw)));
|
||||
} catch (NumberFormatException ignored) {
|
||||
vo.setContextWindow(null);
|
||||
}
|
||||
} else {
|
||||
vo.setContextWindow(null);
|
||||
}
|
||||
Object headers = map.get("extra_headers");
|
||||
if (headers instanceof Map<?, ?> m) {
|
||||
Map<String, String> h = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user