96930b585c
## 主要改进 ### 1. 数据库驱动的模型配置 - 新增 GetActiveProviderWithModel() 方法,从数据库获取优先级最高的 Provider 及其默认模型 - 支持多 Provider 配置,通过 priority 字段控制优先级 - 实现 5 分钟缓存机制,减少数据库查询 ### 2. 应用模型配置优化 - 移除应用层硬编码模型配置 - 应用自动使用优先级最高的 Provider 的默认模型 - 支持应用级模型覆盖(可选) ### 3. 工具脚本 - check-providers: 查询数据库中的 Provider 和应用配置 - fix-providers: 修复 Provider 配置(补全 config 和 models 字段) - clear-app-models: 清空应用硬编码模型配置 ### 4. 代码质量 - 删除未使用的 getProvider() 方法 - 修复 fmt.Println 冗余换行警告 - 统一代码格式 ## 技术细节 **降级策略**: 数据库 Provider (优先级) → 环境变量 Provider → 应用配置模型 **当前配置**: - 优先级 110: 本地LLM (qwen2.5-7b-instruct) - 优先级 100: 阿里云百炼 (qwen-plus) 所有 41 个应用现在自动使用本地模型,无需手动配置。
113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
// 加载环境变量
|
|
_ = godotenv.Load("../../.env")
|
|
|
|
dbURL := os.Getenv("DATABASE_URL")
|
|
if dbURL == "" {
|
|
dbURL = "postgres://freedak:@localhost:5432/govai_portal?sslmode=disable"
|
|
}
|
|
|
|
pool, err := pgxpool.New(context.Background(), dbURL)
|
|
if err != nil {
|
|
log.Fatalf("无法连接数据库: %v", err)
|
|
}
|
|
defer pool.Close()
|
|
|
|
fmt.Println("=== 清空应用硬编码模型配置 ===")
|
|
fmt.Println("目标:让所有应用自动使用优先级最高的 Provider 的默认模型")
|
|
|
|
// 1. 查看当前状态
|
|
fmt.Println("【步骤1】查看当前应用模型配置...")
|
|
var totalApps, configuredApps int
|
|
err = pool.QueryRow(context.Background(), `
|
|
SELECT
|
|
COUNT(*) as total,
|
|
COUNT(CASE WHEN app_config->>'model' IS NOT NULL AND app_config->>'model' != '' THEN 1 END) as configured
|
|
FROM applications
|
|
WHERE status = 'approved'
|
|
`).Scan(&totalApps, &configuredApps)
|
|
|
|
if err != nil {
|
|
log.Fatalf("查询失败: %v", err)
|
|
}
|
|
|
|
fmt.Printf(" - 已批准应用总数: %d\n", totalApps)
|
|
fmt.Printf(" - 配置了模型的应用: %d\n", configuredApps)
|
|
fmt.Printf(" - 未配置模型的应用: %d\n\n", totalApps-configuredApps)
|
|
|
|
// 2. 清空所有应用的模型配置
|
|
fmt.Println("【步骤2】清空所有应用的模型配置...")
|
|
result, err := pool.Exec(context.Background(), `
|
|
UPDATE applications
|
|
SET app_config = app_config - 'model'
|
|
WHERE status = 'approved'
|
|
AND app_config ? 'model'
|
|
`)
|
|
|
|
if err != nil {
|
|
log.Fatalf("更新失败: %v", err)
|
|
}
|
|
|
|
rowsAffected := result.RowsAffected()
|
|
fmt.Printf("✅ 已清空 %d 个应用的模型配置\n\n", rowsAffected)
|
|
|
|
// 3. 验证结果
|
|
fmt.Println("【步骤3】验证结果...")
|
|
var remainingConfigured int
|
|
err = pool.QueryRow(context.Background(), `
|
|
SELECT COUNT(*)
|
|
FROM applications
|
|
WHERE status = 'approved'
|
|
AND app_config->>'model' IS NOT NULL
|
|
AND app_config->>'model' != ''
|
|
`).Scan(&remainingConfigured)
|
|
|
|
if err != nil {
|
|
log.Fatalf("验证查询失败: %v", err)
|
|
}
|
|
|
|
if remainingConfigured == 0 {
|
|
fmt.Println("✅ 所有应用的模型配置已清空")
|
|
} else {
|
|
fmt.Printf("⚠️ 还有 %d 个应用仍有模型配置\n", remainingConfigured)
|
|
}
|
|
|
|
// 4. 显示当前优先级最高的 Provider
|
|
fmt.Println("\n【步骤4】当前优先级最高的 Provider...")
|
|
var providerName, defaultModel string
|
|
var priority int
|
|
err = pool.QueryRow(context.Background(), `
|
|
SELECT name, COALESCE(config->>'default_model', '未配置'), priority
|
|
FROM model_providers
|
|
WHERE is_active = true
|
|
ORDER BY priority DESC, created_at
|
|
LIMIT 1
|
|
`).Scan(&providerName, &defaultModel, &priority)
|
|
|
|
if err != nil {
|
|
log.Printf("查询 Provider 失败: %v", err)
|
|
} else {
|
|
fmt.Printf(" Provider: %s\n", providerName)
|
|
fmt.Printf(" 默认模型: %s\n", defaultModel)
|
|
fmt.Printf(" 优先级: %d\n", priority)
|
|
}
|
|
|
|
fmt.Println("\n=== 配置清理完成 ===")
|
|
fmt.Println("\n说明:")
|
|
fmt.Println(" - 现在所有应用将自动使用优先级最高的 Provider 的默认模型")
|
|
fmt.Println(" - 当前会使用: " + providerName + " / " + defaultModel)
|
|
fmt.Println(" - 如需特殊应用使用指定模型,可在应用配置中单独设置")
|
|
}
|