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
This commit is contained in:
attacker committed 2026-06-18 02:12:17 +08:00
1 parent 2e174fe7eb
commit b8cab7ed0f
6 files changed
+627 -176

No files matched your search

+29 -19
View File
@@ -634,23 +634,33 @@ export const dashboardApi = {
createDatasource: async (_data: any) => ({ success: false }),
};
// AI Assistant API(无后端)
export const aiAssistantApi = {
chat: async (message: string, history: any[]) => {
return {
data: {
reply: `我收到了您的问题: "${message}"。作为AIOps智能助手,我可以帮您分析告警、诊断问题、生成报告等。`,
suggestions: ['分析当前告警', '查看性能趋势', '生成运维报告'],
},
};
},
analyzeAlert: async (alertId: string) => {
return {
data: {
analysis: '根据告警信息分析,可能是由于数据库连接池耗尽导致的。建议检查数据库连接配置和当前连接数。',
recommendations: ['增加连接池大小', '检查慢查询', '重启应用服务'],
},
};
},
generateReport: async (params: any) => ({ data: { report: '运维报告已生成...' } }),
// AI 智能助手 API(后端 LLM 配置 + 对话代理,OpenAI 兼容) // Logan.Li
export interface LLMConfigData {
configured?: boolean;
provider: string;
model: string;
api_key: string;
endpoint: string;
temperature: number;
max_tokens: number;
system_prompt: string;
enabled: boolean;
}
export interface ChatResult {
content: string;
model: string;
tokens_used: number;
}
export const aiApi = {
// 获取 LLM 配置(API Key 已脱敏)
getConfig: () => http.get<LLMConfigData>('/aiops/llm-config'),
// 保存 LLM 配置(api_key 若为脱敏值则后端保留原值)
saveConfig: (data: LLMConfigData) => http.put<LLMConfigData>('/aiops/llm-config', data),
// 测试 LLM 连接(发送 ping)
testConnection: () => http.post('/aiops/llm-config/test'),
// AI 对话(messages 含历史 + 当前用户消息)
chat: (messages: { role: 'user' | 'assistant'; content: string }[]) =>
http.post<ChatResult>('/aiops/chat', { messages }),
};