|
@@ -18,6 +18,8 @@ import com.zsjz.ai.common.compat.StringUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
|
|
|
+import java.util.Locale;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 智能体模型构造工厂:按模型配置构造对话模型与生成参数,并解析各可选模型槽位。
|
|
* 智能体模型构造工厂:按模型配置构造对话模型与生成参数,并解析各可选模型槽位。
|
|
@@ -39,11 +41,35 @@ public class AgentModelFactory {
|
|
|
/**
|
|
/**
|
|
|
* 解析智能体定义的压缩摘要模型(REQ-AI-06 槽位)。
|
|
* 解析智能体定义的压缩摘要模型(REQ-AI-06 槽位)。
|
|
|
*
|
|
*
|
|
|
|
|
+ * <p>槽位未配置时用主对话模型的配置另建一个实例。摘要结果只接受纯文本,
|
|
|
|
|
+ * 而思考模型返回的 reasoning 内容会被框架判定为非文本并拒绝,故摘要实例一律关闭思考。</p>
|
|
|
|
|
+ *
|
|
|
* @param definition 智能体定义
|
|
* @param definition 智能体定义
|
|
|
- * @return 压缩摘要模型;不可用时返回 null
|
|
|
|
|
|
|
+ * @return 压缩摘要模型;配置不可用时返回 null
|
|
|
*/
|
|
*/
|
|
|
Model resolveSummaryModel(AiAgent definition) {
|
|
Model resolveSummaryModel(AiAgent definition) {
|
|
|
- return resolveSlotModel(definition, definition.getSummaryModelId(), "压缩摘要模型");
|
|
|
|
|
|
|
+ AiModelConfig config = resolveSummaryConfig(definition);
|
|
|
|
|
+ return config == null ? null : buildChatModel(config, null, true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 取压缩摘要所用的模型配置:槽位可用时优先,否则回退主对话模型配置。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param definition 智能体定义
|
|
|
|
|
+ * @return 模型配置;无法解析时返回 null
|
|
|
|
|
+ */
|
|
|
|
|
+ private AiModelConfig resolveSummaryConfig(AiAgent definition) {
|
|
|
|
|
+ Long slotModelId = definition.getSummaryModelId();
|
|
|
|
|
+ if (slotModelId != null && !slotModelId.equals(definition.getModelId())) {
|
|
|
|
|
+ AiModelConfig slotConfig = modelConfigMapper.selectById(slotModelId);
|
|
|
|
|
+ if (slotConfig != null && StringUtils.isNotBlank(slotConfig.getModelCode())
|
|
|
|
|
+ && StringUtils.isNotBlank(slotConfig.getBaseUrl()) && !slotConfig.getBaseUrl().contains("{")) {
|
|
|
|
|
+ log.info("压缩摘要模型:agent={}, model={}", definition.getAgentCode(), slotConfig.getModelCode());
|
|
|
|
|
+ return slotConfig;
|
|
|
|
|
+ }
|
|
|
|
|
+ log.warn("压缩摘要模型不可用,回退主模型:agent={}, modelId={}", definition.getAgentCode(), slotModelId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return definition.getModelId() == null ? null : modelConfigMapper.selectById(definition.getModelId());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -96,15 +122,7 @@ public class AgentModelFactory {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 按模型配置构造对话模型。
|
|
|
|
|
- *
|
|
|
|
|
- * <p>优先走 AgentScope 的 {@link ModelProvider} SPI(模型 id 形如 {@code provider:modelCode}):
|
|
|
|
|
- * 命中时可获得该提供商**专用的 formatter**、结构化输出默认值与**上下文窗口大小**
|
|
|
|
|
- * (例如 DeepSeek 使用 {@code DeepSeekFormatter} 且关闭原生结构化输出)。</p>
|
|
|
|
|
- *
|
|
|
|
|
- * <p>未命中则回退为显式构造 OpenAI 兼容客户端,以下两种情况必定回退,保证行为不劣化:
|
|
|
|
|
- * ① provider 未引入对应扩展模块(如 {@code dashscope} / {@code ollama} / {@code custom});
|
|
|
|
|
- * ② 密钥为空(如本地 Ollama 等免鉴权端点,SPI 提供商会因缺少 apiKey 直接抛异常)。</p>
|
|
|
|
|
|
|
+ * 按模型配置构造对话模型(思考行为保持配置原样)。
|
|
|
*
|
|
*
|
|
|
* @param config 模型配置
|
|
* @param config 模型配置
|
|
|
* @return 对话模型
|
|
* @return 对话模型
|
|
@@ -117,9 +135,29 @@ public class AgentModelFactory {
|
|
|
* 按模型配置构造对话模型(思考模式覆盖:true 强制开启 / false 强制关闭 / null 跟随配置)。
|
|
* 按模型配置构造对话模型(思考模式覆盖:true 强制开启 / false 强制关闭 / null 跟随配置)。
|
|
|
*/
|
|
*/
|
|
|
public Model buildChatModel(AiModelConfig config, Boolean enableThinkingOverride) {
|
|
public Model buildChatModel(AiModelConfig config, Boolean enableThinkingOverride) {
|
|
|
|
|
+ return buildChatModel(config, enableThinkingOverride, false);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 按模型配置构造对话模型。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>优先走 AgentScope 的 {@link ModelProvider} SPI(模型 id 形如 {@code provider:modelCode}):
|
|
|
|
|
+ * 命中时可获得该提供商**专用的 formatter**、结构化输出默认值与**上下文窗口大小**
|
|
|
|
|
+ * (例如 DeepSeek 使用 {@code DeepSeekFormatter} 且关闭原生结构化输出)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>未命中则回退为显式构造 OpenAI 兼容客户端,以下两种情况必定回退,保证行为不劣化:
|
|
|
|
|
+ * ① provider 未引入对应扩展模块(如 {@code dashscope} / {@code ollama} / {@code custom});
|
|
|
|
|
+ * ② 密钥为空(如本地 Ollama 等免鉴权端点,SPI 提供商会因缺少 apiKey 直接抛异常)。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param config 模型配置
|
|
|
|
|
+ * @param enableThinkingOverride 思考模式覆盖(null 跟随配置)
|
|
|
|
|
+ * @param disableThinking 是否关闭思考(摘要调用需要纯文本结果)
|
|
|
|
|
+ * @return 对话模型
|
|
|
|
|
+ */
|
|
|
|
|
+ Model buildChatModel(AiModelConfig config, Boolean enableThinkingOverride, boolean disableThinking) {
|
|
|
String baseUrl = resolveBaseUrl(config.getBaseUrl());
|
|
String baseUrl = resolveBaseUrl(config.getBaseUrl());
|
|
|
String apiKey = modelConfigService.decryptApiKey(config.getId());
|
|
String apiKey = modelConfigService.decryptApiKey(config.getId());
|
|
|
- GenerateOptions generateOptions = buildGenerateOptions(config);
|
|
|
|
|
|
|
+ GenerateOptions generateOptions = buildGenerateOptions(config, disableThinking);
|
|
|
// 思考模式覆盖并入 GenerateOptions:提供商扩展 SPI 路径与 OpenAI 兼容回退路径同时生效
|
|
// 思考模式覆盖并入 GenerateOptions:提供商扩展 SPI 路径与 OpenAI 兼容回退路径同时生效
|
|
|
// (模型级 enable_thinking 配置列,null 保持现状;docs/design/chat-process-timeline-plan.md §12 R1)
|
|
// (模型级 enable_thinking 配置列,null 保持现状;docs/design/chat-process-timeline-plan.md §12 R1)
|
|
|
if (enableThinkingOverride != null) {
|
|
if (enableThinkingOverride != null) {
|
|
@@ -133,16 +171,24 @@ public class AgentModelFactory {
|
|
|
String provider = StringUtils.trim(config.getProvider());
|
|
String provider = StringUtils.trim(config.getProvider());
|
|
|
if (StringUtils.isNotBlank(provider) && StringUtils.isNotBlank(apiKey)) {
|
|
if (StringUtils.isNotBlank(provider) && StringUtils.isNotBlank(apiKey)) {
|
|
|
String modelId = provider + ":" + StringUtils.trim(config.getModelCode());
|
|
String modelId = provider + ":" + StringUtils.trim(config.getModelCode());
|
|
|
- ModelCreationContext context = ModelCreationContext.builder()
|
|
|
|
|
|
|
+ ModelCreationContext.Builder contextBuilder = ModelCreationContext.builder()
|
|
|
.apiKey(apiKey)
|
|
.apiKey(apiKey)
|
|
|
.baseUrl(baseUrl)
|
|
.baseUrl(baseUrl)
|
|
|
.stream(true)
|
|
.stream(true)
|
|
|
- .component(GenerateOptions.class, generateOptions)
|
|
|
|
|
- .build();
|
|
|
|
|
|
|
+ .component(GenerateOptions.class, generateOptions);
|
|
|
|
|
+ if (disableThinking) {
|
|
|
|
|
+ contextBuilder.enableThinking(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ ModelCreationContext context = contextBuilder.build();
|
|
|
if (ModelRegistry.canResolve(modelId, context)) {
|
|
if (ModelRegistry.canResolve(modelId, context)) {
|
|
|
log.info("模型解析命中提供商扩展:provider={}, modelId={}, baseUrl={}",
|
|
log.info("模型解析命中提供商扩展:provider={}, modelId={}, baseUrl={}",
|
|
|
provider, modelId, baseUrl);
|
|
provider, modelId, baseUrl);
|
|
|
- return ModelRegistry.resolve(modelId, context);
|
|
|
|
|
|
|
+ Model resolved = ModelRegistry.resolve(modelId, context);
|
|
|
|
|
+ if (config.getContextWindow() != null && resolved.getContextWindowSize() <= 0) {
|
|
|
|
|
+ log.info("提供商扩展未声明上下文窗口,页面配置值 {} 仅用于压缩阈值参考(v1 不覆盖提供商模型):model={}",
|
|
|
|
|
+ config.getContextWindow(), config.getModelCode());
|
|
|
|
|
+ }
|
|
|
|
|
+ return resolved;
|
|
|
}
|
|
}
|
|
|
log.info("提供商 {} 无可用扩展模块,回退为显式构造 OpenAI 兼容客户端:model={}",
|
|
log.info("提供商 {} 无可用扩展模块,回退为显式构造 OpenAI 兼容客户端:model={}",
|
|
|
provider, config.getModelCode());
|
|
provider, config.getModelCode());
|
|
@@ -152,6 +198,9 @@ public class AgentModelFactory {
|
|
|
.modelName(config.getModelCode())
|
|
.modelName(config.getModelCode())
|
|
|
.baseUrl(baseUrl)
|
|
.baseUrl(baseUrl)
|
|
|
.stream(true);
|
|
.stream(true);
|
|
|
|
|
+ if (config.getContextWindow() != null && config.getContextWindow() > 0) {
|
|
|
|
|
+ builder.contextWindowSize(config.getContextWindow());
|
|
|
|
|
+ }
|
|
|
if (generateOptions != null) {
|
|
if (generateOptions != null) {
|
|
|
builder.generateOptions(generateOptions);
|
|
builder.generateOptions(generateOptions);
|
|
|
}
|
|
}
|
|
@@ -159,31 +208,72 @@ public class AgentModelFactory {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 由模型配置构造生成参数(超时与重试)。
|
|
|
|
|
|
|
+ * 由模型配置构造生成参数(超时、重试与摘要调用的思考开关)。
|
|
|
*
|
|
*
|
|
|
* <p>{@code ai_model_config.timeout_seconds} / {@code max_retries} 此前只落库未生效,
|
|
* <p>{@code ai_model_config.timeout_seconds} / {@code max_retries} 此前只落库未生效,
|
|
|
- * 现经 {@link ExecutionConfig} 接入模型调用;两项均未配置时返回 {@code null} 以保持默认行为。</p>
|
|
|
|
|
|
|
+ * 现经 {@link ExecutionConfig} 接入模型调用;两项均未配置且无需关闭思考时返回 {@code null}
|
|
|
|
|
+ * 以保持默认行为。</p>
|
|
|
*
|
|
*
|
|
|
- * @param config 模型配置
|
|
|
|
|
|
|
+ * @param config 模型配置
|
|
|
|
|
+ * @param disableThinking 是否关闭思考
|
|
|
* @return 生成参数;无可配置项时返回 null
|
|
* @return 生成参数;无可配置项时返回 null
|
|
|
*/
|
|
*/
|
|
|
- private GenerateOptions buildGenerateOptions(AiModelConfig config) {
|
|
|
|
|
|
|
+ GenerateOptions buildGenerateOptions(AiModelConfig config, boolean disableThinking) {
|
|
|
boolean hasTimeout = config.getTimeoutSeconds() != null && config.getTimeoutSeconds() > 0;
|
|
boolean hasTimeout = config.getTimeoutSeconds() != null && config.getTimeoutSeconds() > 0;
|
|
|
boolean hasRetries = config.getMaxRetries() != null && config.getMaxRetries() > 0;
|
|
boolean hasRetries = config.getMaxRetries() != null && config.getMaxRetries() > 0;
|
|
|
- if (!hasTimeout && !hasRetries) {
|
|
|
|
|
|
|
+ GenerateOptions.Builder builder = GenerateOptions.builder();
|
|
|
|
|
+ boolean configured = false;
|
|
|
|
|
+ if (hasTimeout || hasRetries) {
|
|
|
|
|
+ ExecutionConfig.Builder execution = ExecutionConfig.builder();
|
|
|
|
|
+ if (hasTimeout) {
|
|
|
|
|
+ execution.timeout(Duration.ofSeconds(config.getTimeoutSeconds()));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (hasRetries) {
|
|
|
|
|
+ execution.maxAttempts(config.getMaxRetries() + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ builder.executionConfig(execution.build());
|
|
|
|
|
+ configured = true;
|
|
|
|
|
+ log.info("模型超时与重试已接入:model={}, timeout={}s, maxAttempts={}",
|
|
|
|
|
+ config.getModelCode(), hasTimeout ? config.getTimeoutSeconds() : "默认",
|
|
|
|
|
+ hasRetries ? config.getMaxRetries() + 1 : 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (disableThinking) {
|
|
|
|
|
+ String param = thinkingDisabledParam(config.getProvider());
|
|
|
|
|
+ if (param == null) {
|
|
|
|
|
+ log.info("摘要调用无法关闭思考(提供商未适配):provider={}, model={}",
|
|
|
|
|
+ config.getProvider(), config.getModelCode());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ builder.additionalBodyParam(param,
|
|
|
|
|
+ "thinking".equals(param) ? Map.of("type", "disabled") : Boolean.FALSE);
|
|
|
|
|
+ configured = true;
|
|
|
|
|
+ log.info("摘要调用已关闭思考:model={}, param={}", config.getModelCode(), param);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return configured ? builder.build() : null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 取关闭思考所需的请求体参数名。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>DashScope 兼容端点用 {@code enable_thinking},DeepSeek 官方接口用
|
|
|
|
|
+ * {@code thinking:{type:disabled}};其他提供商暂不适配,返回 null。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param provider 提供商
|
|
|
|
|
+ * @return 参数名;未适配时返回 null
|
|
|
|
|
+ */
|
|
|
|
|
+ private String thinkingDisabledParam(String provider) {
|
|
|
|
|
+ String normalized = StringUtils.trim(provider);
|
|
|
|
|
+ if (StringUtils.isBlank(normalized)) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
- ExecutionConfig.Builder execution = ExecutionConfig.builder();
|
|
|
|
|
- if (hasTimeout) {
|
|
|
|
|
- execution.timeout(Duration.ofSeconds(config.getTimeoutSeconds()));
|
|
|
|
|
|
|
+ normalized = normalized.toLowerCase(Locale.ROOT);
|
|
|
|
|
+ if ("dashscope".equals(normalized) || "aliyun".equals(normalized)) {
|
|
|
|
|
+ return "enable_thinking";
|
|
|
}
|
|
}
|
|
|
- if (hasRetries) {
|
|
|
|
|
- execution.maxAttempts(config.getMaxRetries() + 1);
|
|
|
|
|
|
|
+ if ("deepseek".equals(normalized)) {
|
|
|
|
|
+ return "thinking";
|
|
|
}
|
|
}
|
|
|
- log.info("模型超时与重试已接入:model={}, timeout={}s, maxAttempts={}",
|
|
|
|
|
- config.getModelCode(), hasTimeout ? config.getTimeoutSeconds() : "默认",
|
|
|
|
|
- hasRetries ? config.getMaxRetries() + 1 : 1);
|
|
|
|
|
- return GenerateOptions.builder().executionConfig(execution.build()).build();
|
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|