Files
aiops/backend/internal/models/llm.go
T
attacker b8cab7ed0f feat: AIOps智能助手接入真实LLM后端(OpenAI兼容代理+配置持久化)
后端:
- models/llm.go: LLMConfig模型(单行配置)+请求/响应struct
- handlers/llm.go: GetLLMConfig/SaveLLMConfig(脱敏+upsert)/Chat(OpenAI兼容代理)/TestLLMConnection
- routes.go: 注册 /aiops/llm-config(GET/PUT/test)+/aiops/chat(POST)
- database.go: AutoMigrate LLMConfig
- 支持OpenAI/DeepSeek/通义千问/Moonshot/Ollama/自定义6种提供商
- API Key脱敏(仅保留后4位)+回传脱敏值保留原值
- 优雅错误处理(连接超时/401/未配置均返回友好消息)
- 120s超时+AIOps运维助手默认系统提示词

前端:
- AIOpsAssistant.tsx: 全量重写接入真实后端
  · 配置弹窗从后端加载(持久化),提供商切换自动填充endpoint/模型
  · 启用开关+测试连接按钮+保存反馈
  · 真实对话(发送最近10条上下文),思考中spinner,错误红色气泡
  · 状态徽章(未配置/在线+模型名),新对话清空
- api.ts: aiApi替代mock aiAssistantApi(getConfig/saveConfig/testConnection/chat)

验证: tsc零错误+build通过+/health正常+前后端联调通过(配置CRUD+API Key脱敏+masked-key保留+chat错误优雅降级) // Logan.Li
2026-06-18 02:12:17 +08:00

53 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import "time"
// LLMConfig 大语言模型配置(单行配置,类似 LDAPConfig) // Logan.Li
// 支持 OpenAI 兼容接口(OpenAI / DeepSeek / 通义千问 / Moonshot / Ollama / 自定义)
type LLMConfig struct {
ID uint `gorm:"primarykey" json:"id"`
Provider string `gorm:"size:50;not null;default:openai" json:"provider"` // openai/deepseek/qwen/moonshot/ollama/custom
Model string `gorm:"size:100;not null;default:gpt-4o-mini" json:"model"` // 模型名,如 gpt-4o-mini / deepseek-chat
APIKey string `gorm:"size:500" json:"api_key"` // API Key(GET 时会被脱敏)
Endpoint string `gorm:"size:500" json:"endpoint"` // 基础地址,如 https://api.openai.com/v1
Temperature float64 `gorm:"default:0.7" json:"temperature"` // 采样温度 0-2
MaxTokens int `gorm:"default:2000" json:"max_tokens"` // 最大输出 token
SystemPrompt string `gorm:"type:text" json:"system_prompt"` // 系统提示词(AIOps 运维助手角色)
Enabled bool `gorm:"default:false" json:"enabled"` // 是否启用
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 表名 // Logan.Li
func (LLMConfig) TableName() string { return "llm_configs" }
// SaveLLMConfigRequest 保存 LLM 配置请求 // Logan.Li
type SaveLLMConfigRequest struct {
Provider string `json:"provider"`
Model string `json:"model"`
APIKey string `json:"api_key"` // 若为脱敏值(含 ****)则后端保留原值
Endpoint string `json:"endpoint"`
Temperature float64 `json:"temperature"`
MaxTokens int `json:"max_tokens"`
SystemPrompt string `json:"system_prompt"`
Enabled bool `json:"enabled"`
}
// ChatMessage 对话消息 // Logan.Li
type ChatMessage struct {
Role string `json:"role"` // system / user / assistant
Content string `json:"content"`
}
// ChatRequest AI 对话请求 // Logan.Li
type ChatRequest struct {
Messages []ChatMessage `json:"messages" binding:"required,min=1,dive"`
}
// ChatResponse AI 对话响应 // Logan.Li
type ChatResponse struct {
Content string `json:"content"`
Model string `json:"model"`
TokensUsed int `json:"tokens_used"`
}