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
@@ -4,17 +4,20 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config LLM 客户端配置(endpoint 已由上层解析为完整 base url) // Logan.Li
|
||||
type Config struct {
|
||||
Protocol string // "openai" | "anthropic",由 client.go 工厂决定具体实现
|
||||
Endpoint string // 完整 base url,如 https://api.deepseek.com/v1
|
||||
APIKey string // Bearer token
|
||||
Model string // 模型名,如 deepseek-chat / gpt-4o-mini
|
||||
@@ -29,8 +32,9 @@ type OpenAIClient struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// NewClient 创建 OpenAI 兼容客户端 // Logan.Li
|
||||
func NewClient(cfg Config) Client {
|
||||
// NewOpenAIClient 创建 OpenAI 兼容客户端 // Logan.Li
|
||||
// 顶层工厂在 client.go(按 Protocol 路由到 OpenAI / Anthropic 实现)。
|
||||
func NewOpenAIClient(cfg Config) Client {
|
||||
timeout := cfg.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = 180 * time.Second
|
||||
@@ -52,6 +56,7 @@ type openaiRequest struct {
|
||||
ToolChoice string `json:"tool_choice,omitempty"` // "auto" 让模型自主决定
|
||||
Temperature float64 `json:"temperature,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"` // 流式补全(Stream 方法置 true)
|
||||
}
|
||||
|
||||
// openaiResponse OpenAI chat/completions 响应体 // Logan.Li
|
||||
@@ -131,6 +136,88 @@ func (c *OpenAIClient) Complete(ctx context.Context, req *CompletionRequest) (*C
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Stream 流式补全(OpenAI 兼容 SSE),逐 token 回调 onDelta,返回聚合响应 // Logan.Li
|
||||
// 解析 data: {"choices":[{"delta":{"content":"..."}}]} 增量;[DONE] 结束;末块 usage 若有则取。
|
||||
func (c *OpenAIClient) Stream(ctx context.Context, req *CompletionRequest, onDelta func(string)) (*CompletionResponse, error) {
|
||||
payload := openaiRequest{
|
||||
Model: c.cfg.Model,
|
||||
Messages: req.Messages,
|
||||
Tools: req.Tools,
|
||||
ToolChoice: "auto",
|
||||
Temperature: c.cfg.Temperature,
|
||||
MaxTokens: c.cfg.MaxTokens,
|
||||
Stream: true,
|
||||
}
|
||||
if payload.MaxTokens <= 0 {
|
||||
payload.MaxTokens = 4096
|
||||
}
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("序列化请求失败: %w", err)
|
||||
}
|
||||
url := c.cfg.Endpoint + "/chat/completions"
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("构造请求失败: %w", err)
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
|
||||
httpReq.Header.Set("Accept", "text/event-stream")
|
||||
|
||||
streamClient := &http.Client{Timeout: 0} // 流式由 ctx 控制超时
|
||||
resp, err := streamClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("LLM 连接失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("LLM 返回错误 (HTTP %d): %s", resp.StatusCode, extractErrorMessage(raw))
|
||||
}
|
||||
|
||||
var contentBuilder strings.Builder
|
||||
var usage Usage
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
for {
|
||||
line, readErr := reader.ReadString('\n')
|
||||
line = strings.TrimSpace(line)
|
||||
if line != "" && strings.HasPrefix(line, "data:") {
|
||||
data := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
||||
if data == "[DONE]" {
|
||||
break
|
||||
}
|
||||
var chunk struct {
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"delta"`
|
||||
} `json:"choices"`
|
||||
Usage *Usage `json:"usage"`
|
||||
}
|
||||
if json.Unmarshal([]byte(data), &chunk) == nil {
|
||||
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
|
||||
delta := chunk.Choices[0].Delta.Content
|
||||
contentBuilder.WriteString(delta)
|
||||
if onDelta != nil {
|
||||
onDelta(delta)
|
||||
}
|
||||
}
|
||||
if chunk.Usage != nil {
|
||||
usage = *chunk.Usage
|
||||
}
|
||||
}
|
||||
}
|
||||
if readErr != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return &CompletionResponse{
|
||||
Model: c.cfg.Model,
|
||||
Message: Message{Role: "assistant", Content: contentBuilder.String()},
|
||||
Usage: usage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// extractErrorMessage 从上游错误响应提取 message 字段 // Logan.Li
|
||||
func extractErrorMessage(raw []byte) string {
|
||||
var m struct {
|
||||
|
||||
Reference in new issue
Block a user