参考 opsx-jenkins 发布清单: - 批量发布页新增「粘贴清单」模式:多行 Job 名粘贴(#注释/内联对/ 名称版本交替)、TAG/分支切换、分支快捷 chips、700ms 防抖解析预览、 串行/并发发布;未导入的 Jenkins Job 自动创建流水线后触发 - 解析器移植 opsx-jenkins parser(参数头识别/成对校验/重名检测) 全局 AI 智能助手(右下角悬浮): - 自然语言「发布一下 cex-agent-server 等 5 个」→ 解析服务+分支+环境 → 发布计划卡片(匹配状态着色)→ 确认 → 真实触发 → runId 回显 + 跳转发布记录 - LLM 接入:OpenAI 兼容接口(DeepSeek/GLM/Qwen),系统设置页配置 API Key(加密存储)+ 测试连接;未配置时自动降级本地规则模式, 服务名/分支关键词匹配,功能不中断 - 候选池 = 流水线 + Jenkins Job(60s 缓存);精确/忽略大小写/包含 三级匹配 新端点:POST /api/ai/chat、/api/ai/release、/api/batch/parse、 POST /api/settings/test-llm;SystemSetting 扩展 LLM 字段
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
// Package llm OpenAI 兼容 Chat 客户端(移植自 opsx-jenkins 生产实现)。
|
|
package llm
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
Endpoint string
|
|
APIKey string
|
|
Model string
|
|
MaxTokens int
|
|
}
|
|
|
|
func NewClient(endpoint, apiKey, model string, maxTokens int) *Client {
|
|
if maxTokens <= 0 {
|
|
maxTokens = 1200
|
|
}
|
|
return &Client{
|
|
Endpoint: strings.TrimRight(endpoint, "/"),
|
|
APIKey: apiKey,
|
|
Model: model,
|
|
MaxTokens: maxTokens,
|
|
}
|
|
}
|
|
|
|
type chatMessage struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
type chatRequest struct {
|
|
Model string `json:"model"`
|
|
Messages []chatMessage `json:"messages"`
|
|
Temperature float64 `json:"temperature"`
|
|
MaxTokens int `json:"max_tokens"`
|
|
}
|
|
|
|
type chatResponse struct {
|
|
Choices []struct {
|
|
Message struct {
|
|
Content string `json:"content"`
|
|
} `json:"message"`
|
|
} `json:"choices"`
|
|
Error *struct {
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
|
|
// Chat 单轮对话;endpoint/apiKey 为空时返回错误由调用方降级
|
|
func (c *Client) Chat(ctx context.Context, system, user string) (string, error) {
|
|
if c.Endpoint == "" || c.APIKey == "" || c.Model == "" {
|
|
return "", fmt.Errorf("LLM 未配置")
|
|
}
|
|
payload := chatRequest{
|
|
Model: c.Model,
|
|
Messages: []chatMessage{
|
|
{Role: "system", Content: system},
|
|
{Role: "user", Content: user},
|
|
},
|
|
Temperature: 0.2,
|
|
MaxTokens: c.MaxTokens,
|
|
}
|
|
body, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint+"/chat/completions", bytes.NewReader(body))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
|
|
|
resp, err := (&http.Client{Timeout: 90 * time.Second}).Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var out chatResponse
|
|
if err := json.Unmarshal(data, &out); err != nil {
|
|
return "", fmt.Errorf("响应解析失败: %s", truncate(string(data), 200))
|
|
}
|
|
if out.Error != nil && out.Error.Message != "" {
|
|
return "", fmt.Errorf("LLM 错误: %s", out.Error.Message)
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return "", fmt.Errorf("HTTP %d: %s", resp.StatusCode, truncate(string(data), 200))
|
|
}
|
|
if len(out.Choices) == 0 {
|
|
return "", fmt.Errorf("空响应")
|
|
}
|
|
return strings.TrimSpace(out.Choices[0].Message.Content), nil
|
|
}
|
|
|
|
func truncate(s string, n int) string {
|
|
if len(s) <= n {
|
|
return s
|
|
}
|
|
return s[:n] + "..."
|
|
}
|