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 个应用现在自动使用本地模型,无需手动配置。
114 lines
2.9 KiB
Go
114 lines
2.9 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("=== 开始修复数据库配置 ===")
|
|
|
|
// 1. 修复本地模型配置
|
|
fmt.Println("【任务1】修复本地模型配置...")
|
|
result, err := pool.Exec(context.Background(), `
|
|
UPDATE model_providers
|
|
SET config = '{
|
|
"provider": "openai",
|
|
"default_model": "qwen2.5-7b-instruct",
|
|
"supports_streaming": true,
|
|
"supports_function_calling": false
|
|
}'::jsonb,
|
|
models = '[
|
|
{"id": "qwen2.5-7b-instruct", "name": "Qwen2.5-7B-Instruct", "type": "chat"}
|
|
]'::jsonb
|
|
WHERE name = '本地LLM (Qwen2.5-7B)'
|
|
`)
|
|
if err != nil {
|
|
log.Printf("❌ 修复本地模型失败: %v", err)
|
|
} else {
|
|
rowsAffected := result.RowsAffected()
|
|
if rowsAffected > 0 {
|
|
fmt.Printf("✅ 成功修复本地模型配置 (影响 %d 行)\n\n", rowsAffected)
|
|
} else {
|
|
fmt.Println("⚠️ 未找到本地模型记录")
|
|
}
|
|
}
|
|
|
|
// 2. 为未配置模型的应用设置默认模型
|
|
fmt.Println("【任务2】为未配置模型的应用设置默认模型...")
|
|
result, err = pool.Exec(context.Background(), `
|
|
UPDATE applications
|
|
SET app_config = jsonb_set(
|
|
COALESCE(app_config, '{}'::jsonb),
|
|
'{model}',
|
|
'"qwen2.5-7b-instruct"'
|
|
)
|
|
WHERE (app_config->>'model' IS NULL OR app_config->>'model' = '')
|
|
AND status = 'approved'
|
|
`)
|
|
if err != nil {
|
|
log.Printf("❌ 更新应用配置失败: %v", err)
|
|
} else {
|
|
rowsAffected := result.RowsAffected()
|
|
if rowsAffected > 0 {
|
|
fmt.Printf("✅ 成功为 %d 个应用设置默认模型\n\n", rowsAffected)
|
|
} else {
|
|
fmt.Println("ℹ️ 所有应用已配置模型")
|
|
}
|
|
}
|
|
|
|
// 3. 验证修复结果
|
|
fmt.Println("【验证】检查修复结果...")
|
|
|
|
var localConfig, localModels string
|
|
err = pool.QueryRow(context.Background(), `
|
|
SELECT
|
|
COALESCE(config::text, 'null') as config,
|
|
COALESCE(models::text, 'null') as models
|
|
FROM model_providers
|
|
WHERE name = '本地LLM (Qwen2.5-7B)'
|
|
`).Scan(&localConfig, &localModels)
|
|
|
|
if err != nil {
|
|
log.Printf("查询验证失败: %v", err)
|
|
} else {
|
|
fmt.Printf("本地模型 config: %s\n", localConfig)
|
|
fmt.Printf("本地模型 models: %s\n\n", localModels)
|
|
}
|
|
|
|
var unconfiguredCount int
|
|
err = pool.QueryRow(context.Background(), `
|
|
SELECT COUNT(*)
|
|
FROM applications
|
|
WHERE (app_config->>'model' IS NULL OR app_config->>'model' = '')
|
|
AND status = 'approved'
|
|
`).Scan(&unconfiguredCount)
|
|
|
|
if err != nil {
|
|
log.Printf("查询未配置应用数量失败: %v", err)
|
|
} else {
|
|
fmt.Printf("未配置模型的应用数量: %d\n", unconfiguredCount)
|
|
}
|
|
|
|
fmt.Println("\n=== 修复完成 ===")
|
|
}
|