|
|
|
@ -3,6 +3,7 @@ package com.wok.supportbot.config; |
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.jdbc.core.JdbcTemplate; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
@ -17,6 +18,18 @@ public class DatabaseInitConfig { |
|
|
|
@Autowired |
|
|
|
private JdbcTemplate jdbcTemplate; |
|
|
|
|
|
|
|
@Value("${spring.ai.dashscope.api-key:}") |
|
|
|
private String dashscopeApiKey; |
|
|
|
|
|
|
|
@Value("${spring.ai.dashscope.chat.options.model:qwen-turbo}") |
|
|
|
private String chatModelName; |
|
|
|
|
|
|
|
@Value("${spring.ai.dashscope.chat.options.temperature:0.7}") |
|
|
|
private Double chatTemperature; |
|
|
|
|
|
|
|
@Value("${spring.ai.dashscope.embedding.options.model:text-embedding-v2}") |
|
|
|
private String embeddingModelName; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
try { |
|
|
|
@ -75,6 +88,9 @@ public class DatabaseInitConfig { |
|
|
|
syncDefaultCustomerServiceRoles(); |
|
|
|
syncDefaultCustomerAccounts(); |
|
|
|
|
|
|
|
createAiModelConfigTable(); |
|
|
|
seedDefaultAiModelConfigs(); |
|
|
|
|
|
|
|
log.info("数据库初始化完成"); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("数据库初始化失败", e); |
|
|
|
@ -347,4 +363,65 @@ public class DatabaseInitConfig { |
|
|
|
log.warn("添加 content_hash 列时出错", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
insertDefaultModelConfig("Chat Default", "CHAT", "dashscope", |
|
|
|
dashscopeApiKey, chatModelName, chatTemperature, 2000, |
|
|
|
true, 100, "Default chat model config from application.yml"); |
|
|
|
insertDefaultModelConfig("Product Extract Default", "PRODUCT_EXTRACT", "dashscope", |
|
|
|
dashscopeApiKey, chatModelName, 0.3, 2000, |
|
|
|
true, 90, "Default product extraction model config"); |
|
|
|
insertDefaultModelConfig("Embedding Default", "EMBEDDING", "dashscope", |
|
|
|
dashscopeApiKey, embeddingModelName, null, null, |
|
|
|
true, 80, "Default embedding model config"); |
|
|
|
insertDefaultModelConfig("RAG Rewrite Default", "RAG_REWRITE", "dashscope", |
|
|
|
dashscopeApiKey, chatModelName, 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); |
|
|
|
} |
|
|
|
|
|
|
|
} |