feat(llm): 大模型双协议(OpenAI+Anthropic)接入 + SSH凭证修复 + 容量预测AI解读
后端双协议: 新增 anthropic.go(Complete/Stream/工具转换) + client.go 工厂按 Protocol 路由; llm.Client 加 Stream; llm_chat 三方法收敛复用 llm.Client; resolveProtocol 按 provider 路由; Agent CLI 注入协议。编码套餐(GLM-5.2/Kimi/MiMo/Anthropic)走 Anthropic /v1/messages。 SSH凭证修复: applyCredential 正确解析凭证 JSON 的 private_key(旧实现把整个 JSON 当私钥 → ssh.ParsePrivateKey 失败 → ssh-key 登录不通); AssignCredential/批量导入/单台 CRUD 三处统一; 单台补齐 credential_id 字段(原被忽略)。 容量预测: 新增 AI 深度解读(buildCapacityInsightPrompt + PredictiveCapacityTab 按钮, 复用 aiApi.chat)。 其他: DeepSeek 模型名更新为 deepseek-v4-flash/pro; 智能助手去掉重复模型配置按钮; LLMConfig 精简生成参数(Claude Code 式)。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
c0ee8e4ddd
commit
0b2e43ea9e
16 files changed
+689
-296
No files matched your search
+52
-194
@@ -1,22 +1,41 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ezops/middleware"
|
||||
"ezops/model"
|
||||
"ezops/pkg/agentcli/llm"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Chat AI 对话代理(OpenAI 兼容接口) // Logan.Li
|
||||
// buildLLMClient 从数据库配置构造 LLM 客户端(按 provider 协议自动路由 OpenAI/Anthropic) // Logan.Li
|
||||
func (h *LLMHandler) buildLLMClient(cfg model.LLMConfig) llm.Client {
|
||||
return llm.NewClient(llm.Config{
|
||||
Protocol: resolveProtocol(cfg.Provider),
|
||||
Endpoint: resolveEndpoint(cfg.Provider, cfg.Endpoint),
|
||||
APIKey: cfg.APIKey,
|
||||
Model: cfg.Model,
|
||||
Temperature: cfg.Temperature,
|
||||
MaxTokens: cfg.MaxTokens,
|
||||
})
|
||||
}
|
||||
|
||||
// toLLMMessages model.ChatMessage[] → llm.Message[](智能助手纯对话,仅填 Role/Content) // Logan.Li
|
||||
func toLLMMessages(msgs []model.ChatMessage) []llm.Message {
|
||||
out := make([]llm.Message, len(msgs))
|
||||
for i, m := range msgs {
|
||||
out[i] = llm.Message{Role: m.Role, Content: m.Content}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Chat AI 对话代理(统一走 llm.Client,按配置协议自动适配 OpenAI/Anthropic) // Logan.Li
|
||||
// POST /api/v1/aiops/chat
|
||||
func (h *LLMHandler) Chat(c *gin.Context) {
|
||||
var cfg model.LLMConfig
|
||||
@@ -38,60 +57,22 @@ func (h *LLMHandler) Chat(c *gin.Context) {
|
||||
}
|
||||
messages := append([]model.ChatMessage{{Role: "system", Content: systemPrompt}}, req.Messages...)
|
||||
|
||||
// 构造 OpenAI 兼容请求
|
||||
payload := openaiChatRequest{
|
||||
Model: cfg.Model,
|
||||
Messages: messages,
|
||||
Temperature: cfg.Temperature,
|
||||
MaxTokens: cfg.MaxTokens,
|
||||
}
|
||||
body, _ := json.Marshal(payload)
|
||||
|
||||
endpoint := resolveEndpoint(cfg.Provider, cfg.Endpoint)
|
||||
url := endpoint + "/chat/completions"
|
||||
|
||||
httpReq, err := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, url, bytes.NewReader(body))
|
||||
resp, err := h.buildLLMClient(cfg).Complete(c.Request.Context(), &llm.CompletionRequest{
|
||||
Messages: toLLMMessages(messages),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 500, Message: "构造请求失败: " + err.Error()})
|
||||
c.JSON(http.StatusOK, model.Response{Code: 502, Message: "LLM 调用失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+cfg.APIKey)
|
||||
|
||||
resp, err := h.client.Do(httpReq)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 502, Message: "LLM 服务连接失败,请检查网络或 Endpoint 配置: " + err.Error()})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// 提取上游错误信息
|
||||
msg := fmt.Sprintf("LLM 服务返回错误 (HTTP %d)", resp.StatusCode)
|
||||
if upErr := parseOpenAIError(raw); upErr != "" {
|
||||
msg += ": " + upErr
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: resp.StatusCode, Message: msg})
|
||||
return
|
||||
}
|
||||
|
||||
var chatResp openaiChatResponse
|
||||
if err := json.Unmarshal(raw, &chatResp); err != nil || len(chatResp.Choices) == 0 {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 500, Message: "解析 LLM 响应失败"})
|
||||
return
|
||||
}
|
||||
replyContent := chatResp.Choices[0].Message.Content
|
||||
replyContent := resp.Message.Content
|
||||
|
||||
// 持久化对话历史(按用户隔离,成对写入 user + assistant) // Logan.Li
|
||||
// 仅在成功时入库;错误气泡为瞬时 UI 不持久化
|
||||
uid := middleware.GetUserID(c)
|
||||
if uid != 0 && len(req.Messages) > 0 {
|
||||
// 取最后一条用户消息(前端已附加当前提问)
|
||||
lastUser := req.Messages[len(req.Messages)-1]
|
||||
now := time.Now()
|
||||
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
|
||||
sid := req.SessionID
|
||||
sid := req.SessionID // 前端传入或后端生成(兼容旧版客户端)
|
||||
if sid == "" {
|
||||
sid = fmt.Sprintf("sess-%d", now.UnixMilli())
|
||||
}
|
||||
@@ -104,15 +85,15 @@ func (h *LLMHandler) Chat(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ChatResponse{
|
||||
Content: replyContent,
|
||||
Model: cfg.Model,
|
||||
TokensUsed: chatResp.Usage.TotalTokens,
|
||||
TokensUsed: resp.Usage.TotalTokens,
|
||||
}})
|
||||
}
|
||||
|
||||
// ChatStream AI 流式对话代理(SSE) // Logan.Li
|
||||
// POST /api/v1/aiops/chat/stream
|
||||
// 客户端用 EventSource/fetch-stream 消费,每条 SSE data 为 {"content": "..."}
|
||||
// 出错时发送 event: error,data 为 {"message": "..."}
|
||||
// 完成后服务端持久化 user + assistant 两条历史,event: done 结束
|
||||
// 客户端用 fetch-stream 消费,每条 SSE data 为 {"content": "..."};出错发 event: error;完成发 event: done。
|
||||
// 上游协议差异(OpenAI delta / Anthropic content_block_delta)由 llm.Client.Stream 屏蔽,
|
||||
// controller 只负责 SSE 转发 + 客户端断开检测 + 历史持久化。
|
||||
func (h *LLMHandler) ChatStream(c *gin.Context) {
|
||||
var cfg model.LLMConfig
|
||||
if err := h.db.First(&cfg).Error; err != nil || !cfg.Enabled {
|
||||
@@ -136,127 +117,37 @@ func (h *LLMHandler) ChatStream(c *gin.Context) {
|
||||
c.Writer.Header().Set("Connection", "keep-alive")
|
||||
c.Writer.Header().Set("X-Accel-Buffering", "no") // Nginx 关闭缓冲
|
||||
c.Writer.WriteHeader(http.StatusOK)
|
||||
// 注册写入心跳(立即 flush 一个注释行让客户端确认连接建立)
|
||||
c.Writer.Flush()
|
||||
c.Writer.Flush() // 立即 flush 让客户端确认连接建立
|
||||
|
||||
// 组装系统提示词 + 上下文
|
||||
systemPrompt := cfg.SystemPrompt
|
||||
if strings.TrimSpace(systemPrompt) == "" {
|
||||
systemPrompt = defaultSystemPrompt
|
||||
}
|
||||
messages := append([]model.ChatMessage{{Role: "system", Content: systemPrompt}}, req.Messages...)
|
||||
// 构造 OpenAI 兼容流式请求
|
||||
payload := struct {
|
||||
Model string `json:"model"`
|
||||
Messages []model.ChatMessage `json:"messages"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
Stream bool `json:"stream"`
|
||||
}{
|
||||
Model: cfg.Model,
|
||||
Messages: messages,
|
||||
Temperature: cfg.Temperature,
|
||||
MaxTokens: cfg.MaxTokens,
|
||||
Stream: true,
|
||||
}
|
||||
body, _ := json.Marshal(payload)
|
||||
|
||||
endpoint := resolveEndpoint(cfg.Provider, cfg.Endpoint)
|
||||
url := endpoint + "/chat/completions"
|
||||
|
||||
httpReq, err := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
writeSSEError(c, 500, "构造请求失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+cfg.APIKey)
|
||||
httpReq.Header.Set("Accept", "text/event-stream")
|
||||
|
||||
// 流式请求不能用全局 120s 超时客户端,构造无超时 client 让 ctx 控制 // Logan.Li
|
||||
streamClient := &http.Client{Timeout: 0}
|
||||
resp, err := streamClient.Do(httpReq)
|
||||
if err != nil {
|
||||
writeSSEError(c, 502, "LLM 服务连接失败,请检查网络或 Endpoint 配置: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
msg := fmt.Sprintf("LLM 服务返回错误 (HTTP %d)", resp.StatusCode)
|
||||
if e := parseOpenAIError(raw); e != "" {
|
||||
msg += ": " + e
|
||||
}
|
||||
writeSSEError(c, resp.StatusCode, msg)
|
||||
return
|
||||
}
|
||||
|
||||
// 逐行解析上游 SSE,转发增量内容给前端 // Logan.Li
|
||||
// 流式补全:逐 token 回调转发 SSE;ctx 取消(客户端断开)由 llm.Client 内部终止上游读取
|
||||
var contentBuilder strings.Builder
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
clientGone := false
|
||||
for {
|
||||
// 客户端断开时停止读取(c.Request.Context().Done() 在客户端断开时触发) // Logan.Li
|
||||
select {
|
||||
case <-c.Request.Context().Done():
|
||||
clientGone = true
|
||||
default:
|
||||
}
|
||||
if clientGone {
|
||||
break
|
||||
}
|
||||
|
||||
line, readErr := reader.ReadString('\n')
|
||||
line = strings.TrimSpace(line)
|
||||
if line != "" {
|
||||
// 仅处理 data: 开头的行
|
||||
if strings.HasPrefix(line, "data:") {
|
||||
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
||||
if data == "[DONE]" {
|
||||
// 上游结束,跳出循环准备持久化 + 发送 [DONE]
|
||||
break
|
||||
}
|
||||
// 解析 {"choices":[{"delta":{"content":"..."}}]}
|
||||
var chunk struct {
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"delta"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if json.Unmarshal([]byte(data), &chunk) == nil && len(chunk.Choices) > 0 {
|
||||
delta := chunk.Choices[0].Delta.Content
|
||||
if delta != "" {
|
||||
contentBuilder.WriteString(delta)
|
||||
// 转发给前端:data: {"content":"..."}
|
||||
writeSSEChunk(c, delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if readErr != nil {
|
||||
if readErr != io.EOF {
|
||||
// 读取出错(非正常结束)
|
||||
if contentBuilder.Len() == 0 {
|
||||
writeSSEError(c, 502, "LLM 流式响应读取失败: "+readErr.Error())
|
||||
return
|
||||
}
|
||||
// 已有部分内容则正常结束
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
_, streamErr := h.buildLLMClient(cfg).Stream(c.Request.Context(), &llm.CompletionRequest{
|
||||
Messages: toLLMMessages(messages),
|
||||
}, func(delta string) {
|
||||
contentBuilder.WriteString(delta)
|
||||
writeSSEChunk(c, delta)
|
||||
})
|
||||
fullContent := contentBuilder.String()
|
||||
|
||||
// 持久化对话历史(仅在成功且非空时入库;错误/中断不入库避免污染历史) // Logan.Li
|
||||
// 出错且无任何内容 → 发 error 事件
|
||||
if streamErr != nil && fullContent == "" {
|
||||
writeSSEError(c, 502, "LLM 流式响应失败: "+streamErr.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 持久化 + 结束事件(仅在非空且客户端未断开时入库;错误/中断不入库避免污染历史) // Logan.Li
|
||||
clientGone := c.Request.Context().Err() != nil
|
||||
if fullContent != "" && !clientGone {
|
||||
uid := middleware.GetUserID(c)
|
||||
if uid != 0 {
|
||||
lastUser := req.Messages[len(req.Messages)-1]
|
||||
now := time.Now()
|
||||
// session_id:前端传入或后端生成(兼容旧版客户端) // Logan.Li
|
||||
sid := req.SessionID
|
||||
if sid == "" {
|
||||
sid = fmt.Sprintf("sess-%d", now.UnixMilli())
|
||||
@@ -266,7 +157,6 @@ func (h *LLMHandler) ChatStream(c *gin.Context) {
|
||||
{UserID: uid, SessionID: sid, Role: "assistant", Content: fullContent, Model: cfg.Model, CreatedAt: now.Add(time.Millisecond)},
|
||||
})
|
||||
}
|
||||
// 发送结束标志
|
||||
c.Writer.WriteString("event: done\ndata: {\"model\":\"" + cfg.Model + "\"}\n\n")
|
||||
c.Writer.Flush()
|
||||
} else if !clientGone {
|
||||
@@ -298,19 +188,6 @@ func writeSSEError(c *gin.Context, code int, message string) {
|
||||
c.Writer.Flush()
|
||||
}
|
||||
|
||||
// parseOpenAIError 从上游错误响应中提取 message 字段 // Logan.Li
|
||||
func parseOpenAIError(raw []byte) string {
|
||||
var m struct {
|
||||
Error struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if json.Unmarshal(raw, &m) == nil && m.Error.Message != "" {
|
||||
return m.Error.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// TestLLMConnection 测试 LLM 连接(发送一条 ping 消息) // Logan.Li
|
||||
// POST /api/v1/aiops/llm-config/test
|
||||
func (h *LLMHandler) TestLLMConnection(c *gin.Context) {
|
||||
@@ -319,31 +196,12 @@ func (h *LLMHandler) TestLLMConnection(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 400, Message: "请先保存配置"})
|
||||
return
|
||||
}
|
||||
payload := openaiChatRequest{
|
||||
Model: cfg.Model,
|
||||
Messages: []model.ChatMessage{{Role: "user", Content: "ping"}},
|
||||
Temperature: 0,
|
||||
MaxTokens: 16,
|
||||
}
|
||||
body, _ := json.Marshal(payload)
|
||||
endpoint := resolveEndpoint(cfg.Provider, cfg.Endpoint)
|
||||
httpReq, _ := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, endpoint+"/chat/completions", bytes.NewReader(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+cfg.APIKey)
|
||||
resp, err := h.client.Do(httpReq)
|
||||
_, err := h.buildLLMClient(cfg).Complete(c.Request.Context(), &llm.CompletionRequest{
|
||||
Messages: []llm.Message{{Role: "user", Content: "ping"}},
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, model.Response{Code: 502, Message: "连接失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
msg := fmt.Sprintf("HTTP %d", resp.StatusCode)
|
||||
if e := parseOpenAIError(raw); e != "" {
|
||||
msg += ": " + e
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: resp.StatusCode, Message: msg})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "连接成功"})
|
||||
}
|
||||
Reference in new issue
Block a user