修改difyconfig值

This commit is contained in:
2025-11-26 10:34:32 +08:00
parent bc376d28a1
commit 8ff849d050
6 changed files with 93 additions and 73 deletions

View File

@@ -1,12 +1,14 @@
package org.xyzh.ai.config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.xyzh.api.system.config.SysConfigService;
import jakarta.annotation.PostConstruct;
/**
* @description Dify配置类
* @filename DifyConfig.java
@@ -14,9 +16,9 @@ import org.xyzh.api.system.config.SysConfigService;
* @copyright xyzh
* @since 2025-11-04
*/
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "dify")
public class DifyConfig {
@Autowired
@@ -55,21 +57,6 @@ public class DifyConfig {
*/
private Integer streamTimeout = 300;
/**
* 最大重试次数
*/
private Integer maxRetries = 3;
/**
* 重试间隔(毫秒)
*/
private Long retryInterval = 1000L;
/**
* 是否启用Dify集成
*/
private Boolean enabled = true;
/**
* 上传文件配置
*/
@@ -81,9 +68,69 @@ public class DifyConfig {
private Dataset dataset = new Dataset();
/**
* 对话配置
* 初始化配置,从数据库加载
*/
private Chat chat = new Chat();
@PostConstruct
public void init() {
try {
log.info("开始从数据库加载Dify配置...");
// 基础配置
loadStringConfig("dify.apiBaseUrl", val -> this.apiBaseUrl = val);
loadStringConfig("dify.apiKey", val -> this.apiKey = val);
loadStringConfig("dify.knowledgeApiKey", val -> this.knowledgeApiKey = val);
loadIntegerConfig("dify.timeout", val -> this.timeout = val);
loadIntegerConfig("dify.connectTimeout", val -> this.connectTimeout = val);
loadIntegerConfig("dify.readTimeout", val -> this.readTimeout = val);
loadIntegerConfig("dify.streamTimeout", val -> this.streamTimeout = val);
// Upload配置
loadStringConfig("dify.upload.allowedTypes", val -> {
if (val != null && !val.trim().isEmpty()) {
this.upload.allowedTypes = val.split(",");
}
});
loadIntegerConfig("dify.upload.maxSize", val -> this.upload.maxSize = val);
// Dataset配置
loadStringConfig("dify.dataset.defaultIndexingTechnique", val -> this.dataset.defaultIndexingTechnique = val);
loadStringConfig("dify.dataset.defaultEmbeddingModel", val -> this.dataset.defaultEmbeddingModel = val);
log.info("Dify配置加载完成 - API地址: {}", apiBaseUrl);
} catch (Exception e) {
log.error("加载Dify配置失败将使用默认值", e);
}
}
/**
* 加载字符串配置
*/
private void loadStringConfig(String key, java.util.function.Consumer<String> setter) {
try {
String value = sysConfigService.getStringConfig(key);
if (value != null && !value.trim().isEmpty()) {
setter.accept(value);
log.debug("加载配置: {} = {}", key, value);
}
} catch (Exception e) {
log.warn("加载配置失败: {}, 错误: {}", key, e.getMessage());
}
}
/**
* 加载整数配置
*/
private void loadIntegerConfig(String key, java.util.function.Consumer<Integer> setter) {
try {
Integer value = sysConfigService.getIntConfig(key);
if (value != null) {
setter.accept(value);
log.debug("加载配置: {} = {}", key, value);
}
} catch (Exception e) {
log.warn("加载配置失败: {}, 错误: {}", key, e.getMessage());
}
}
@Data
public static class Upload {
@@ -96,11 +143,6 @@ public class DifyConfig {
* 最大文件大小MB
*/
private Integer maxSize = 50;
/**
* 批量上传最大文件数
*/
private Integer batchMaxCount = 10;
}
@Data
@@ -114,56 +156,13 @@ public class DifyConfig {
* 默认Embedding模型
*/
private String defaultEmbeddingModel = "text-embedding-ada-002";
/**
* 文档分段策略
*/
private String segmentationStrategy = "automatic";
/**
* 分段最大长度
*/
private Integer maxSegmentLength = 1000;
/**
* 分段重叠长度
*/
private Integer segmentOverlap = 50;
}
@Data
public static class Chat {
/**
* 默认温度值
*/
private Double defaultTemperature = 0.7;
/**
* 默认最大Token数
*/
private Integer defaultMaxTokens = 2000;
/**
* 默认Top P值
*/
private Double defaultTopP = 1.0;
/**
* 是否启用流式响应
*/
private Boolean enableStream = true;
/**
* 对话上下文最大消息数
*/
private Integer maxContextMessages = 10;
}
/**
* 验证配置是否有效
*/
public boolean isValid() {
return enabled && apiBaseUrl != null && !apiBaseUrl.trim().isEmpty();
return apiBaseUrl != null && !apiBaseUrl.trim().isEmpty();
}
/**