|
|
@ -3,7 +3,6 @@ package com.wok.supportbot.config; |
|
|
import jakarta.annotation.PostConstruct; |
|
|
import jakarta.annotation.PostConstruct; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
@ -18,9 +17,6 @@ public class DatabaseInitConfig { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private JdbcTemplate jdbcTemplate; |
|
|
private JdbcTemplate jdbcTemplate; |
|
|
|
|
|
|
|
|
@Value("${spring.ai.dashscope.api-key:}") |
|
|
|
|
|
private String dashscopeApiKey; |
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
@PostConstruct |
|
|
public void init() { |
|
|
public void init() { |
|
|
try { |
|
|
try { |
|
|
@ -79,9 +75,6 @@ public class DatabaseInitConfig { |
|
|
syncDefaultCustomerServiceRoles(); |
|
|
syncDefaultCustomerServiceRoles(); |
|
|
syncDefaultCustomerAccounts(); |
|
|
syncDefaultCustomerAccounts(); |
|
|
|
|
|
|
|
|
createAiModelConfigTable(); |
|
|
|
|
|
seedDefaultAiModelConfigs(); |
|
|
|
|
|
|
|
|
|
|
|
log.info("数据库初始化完成"); |
|
|
log.info("数据库初始化完成"); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("数据库初始化失败", e); |
|
|
log.error("数据库初始化失败", e); |
|
|
@ -355,70 +348,4 @@ public class DatabaseInitConfig { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void createAiModelConfigTable() { |
|
|
|
|
|
String sql = """ |
|
|
|
|
|
CREATE TABLE IF NOT EXISTS ai_model_config ( |
|
|
|
|
|
id BIGSERIAL PRIMARY KEY, |
|
|
|
|
|
name VARCHAR(100) NOT NULL, |
|
|
|
|
|
app_type VARCHAR(50) NOT NULL, |
|
|
|
|
|
provider VARCHAR(50) NOT NULL DEFAULT 'dashscope', |
|
|
|
|
|
api_key VARCHAR(512) NOT NULL, |
|
|
|
|
|
model_name VARCHAR(100) NOT NULL, |
|
|
|
|
|
temperature DOUBLE PRECISION DEFAULT 0.7, |
|
|
|
|
|
max_tokens INTEGER DEFAULT 2000, |
|
|
|
|
|
base_url VARCHAR(512), |
|
|
|
|
|
extra_config JSONB DEFAULT '{}' NOT NULL, |
|
|
|
|
|
is_active BOOLEAN DEFAULT FALSE NOT NULL, |
|
|
|
|
|
priority INTEGER DEFAULT 0 NOT NULL, |
|
|
|
|
|
description TEXT, |
|
|
|
|
|
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
|
|
|
|
|
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
|
|
|
|
|
is_delete BOOLEAN DEFAULT FALSE NOT NULL |
|
|
|
|
|
) |
|
|
|
|
|
"""; |
|
|
|
|
|
jdbcTemplate.execute(sql); |
|
|
|
|
|
jdbcTemplate.execute("CREATE INDEX IF NOT EXISTS idx_ai_model_config_app_type ON ai_model_config (app_type)"); |
|
|
|
|
|
jdbcTemplate.execute("CREATE INDEX IF NOT EXISTS idx_ai_model_config_is_active ON ai_model_config (is_active)"); |
|
|
|
|
|
jdbcTemplate.execute("CREATE INDEX IF NOT EXISTS idx_ai_model_config_provider ON ai_model_config (provider)"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void seedDefaultAiModelConfigs() { |
|
|
|
|
|
Integer count = jdbcTemplate.queryForObject( |
|
|
|
|
|
"SELECT COUNT(*) FROM ai_model_config WHERE is_delete = false", |
|
|
|
|
|
Integer.class); |
|
|
|
|
|
if (count != null && count > 0) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// yml 未配置 api-key 时种子值留空:不影响启动,用户需在前端「AI 大模型配置管理」页面填入 api_key 后才能调用 AI 功能 |
|
|
|
|
|
if (dashscopeApiKey == null || dashscopeApiKey.isBlank()) { |
|
|
|
|
|
log.warn("未检测到 spring.ai.dashscope.api-key,已写入空 api_key 的默认配置;" + |
|
|
|
|
|
"请在前端「AI 大模型配置管理」页面为各应用类型填入 API Key 后再使用 AI 功能"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
insertDefaultModelConfig("Chat Default", "CHAT", "dashscope", |
|
|
|
|
|
dashscopeApiKey, "qwen-turbo", 0.7, 2000, |
|
|
|
|
|
true, 100, "Default chat model config"); |
|
|
|
|
|
insertDefaultModelConfig("Product Extract Default", "PRODUCT_EXTRACT", "dashscope", |
|
|
|
|
|
dashscopeApiKey, "qwen-turbo", 0.3, 2000, |
|
|
|
|
|
true, 90, "Default product extraction model config"); |
|
|
|
|
|
insertDefaultModelConfig("Embedding Default", "EMBEDDING", "dashscope", |
|
|
|
|
|
dashscopeApiKey, "text-embedding-v2", null, null, |
|
|
|
|
|
true, 80, "Default embedding model config"); |
|
|
|
|
|
insertDefaultModelConfig("RAG Rewrite Default", "RAG_REWRITE", "dashscope", |
|
|
|
|
|
dashscopeApiKey, "qwen-turbo", 0.5, 1000, |
|
|
|
|
|
true, 70, "Default RAG rewrite model config"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void insertDefaultModelConfig(String name, String appType, String provider, |
|
|
|
|
|
String apiKey, String modelName, Double temperature, |
|
|
|
|
|
Integer maxTokens, boolean isActive, int priority, |
|
|
|
|
|
String description) { |
|
|
|
|
|
jdbcTemplate.update(""" |
|
|
|
|
|
INSERT INTO ai_model_config (name, app_type, provider, api_key, model_name, temperature, max_tokens, is_active, priority, description) |
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
|
|
|
|
|
""", name, appType, provider, apiKey, modelName, temperature, maxTokens, isActive, priority, description); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |