From 4e8bcdd68e87799405ca63841aa149fa8fd632de Mon Sep 17 00:00:00 2001 From: unclesam <2036253946@qq.com> Date: Sat, 22 Aug 2026 20:39:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(llm):=20=E5=85=BC=E5=AE=B9=E6=8E=A8?= =?UTF-8?q?=E7=90=86/Beta=E6=A8=A1=E5=9E=8B=EF=BC=88=E5=A6=82=20o1/o3/gpt-?= =?UTF-8?q?5.6=E7=B3=BB=E5=88=97=EF=BC=89=E5=9B=BA=E5=AE=9A=20temperature?= =?UTF-8?q?=3D1=20=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/llm/client.go | 50 ++++++++++++++++++++++++++++++++----- internal/updater/checker.go | 2 +- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/internal/llm/client.go b/internal/llm/client.go index 0026cf7..4baf78b 100644 --- a/internal/llm/client.go +++ b/internal/llm/client.go @@ -238,16 +238,41 @@ func NewClient(cfg Config) (*Client, error) { }, nil } +// isReasoningModel 判断是否为推理或带有 Beta 限制的模型(这类模型要求 temperature 固定为 1) +func isReasoningModel(model string) bool { + m := strings.ToLower(model) + return strings.HasPrefix(m, "o1") || + strings.HasPrefix(m, "o3") || + strings.HasPrefix(m, "o4") || + strings.Contains(m, "reasoner") || + strings.Contains(m, "gpt-5") || + strings.Contains(m, "luna") || + strings.Contains(m, "terra") || + strings.Contains(m, "sol") +} + // Review 一次性完整返回代码评审结果 func (c *Client) Review(ctx context.Context, systemPrompt, diff string) (string, error) { - resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ + temp := float32(0.2) + if isReasoningModel(c.model) { + temp = 1.0 + } + + req := openai.ChatCompletionRequest{ Model: c.model, Messages: []openai.ChatCompletionMessage{ {Role: openai.ChatMessageRoleSystem, Content: systemPrompt}, {Role: openai.ChatMessageRoleUser, Content: diff}, }, - Temperature: 0.2, // 代码评审低随机性 - }) + Temperature: temp, + } + + resp, err := c.client.CreateChatCompletion(ctx, req) + // 针对限制了 temperature 的模型,自动降级为 1.0 重试 + if err != nil && (strings.Contains(strings.ToLower(err.Error()), "temperature") || strings.Contains(strings.ToLower(err.Error()), "beta-limitations")) { + req.Temperature = 1.0 + resp, err = c.client.CreateChatCompletion(ctx, req) + } if err != nil { return "", fmt.Errorf("代码评审调用失败: %w", err) @@ -376,18 +401,31 @@ func (u *Usage) EstimateCost() float64 { // ReviewStream 流式逐字返回评审结果(打字机效果),同时返回 Token 使用量 func (c *Client) ReviewStream(ctx context.Context, systemPrompt, diff string, onChunk func(chunk string)) (*Usage, error) { - stream, err := c.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{ + temp := float32(0.2) + if isReasoningModel(c.model) { + temp = 1.0 + } + + req := openai.ChatCompletionRequest{ Model: c.model, Messages: []openai.ChatCompletionMessage{ {Role: openai.ChatMessageRoleSystem, Content: systemPrompt}, {Role: openai.ChatMessageRoleUser, Content: diff}, }, - Temperature: 0.2, + Temperature: temp, Stream: true, StreamOptions: &openai.StreamOptions{ IncludeUsage: true, }, - }) + } + + stream, err := c.client.CreateChatCompletionStream(ctx, req) + // 针对限制了 temperature 的模型,自动降级为 1.0 重试 + if err != nil && (strings.Contains(strings.ToLower(err.Error()), "temperature") || strings.Contains(strings.ToLower(err.Error()), "beta-limitations")) { + req.Temperature = 1.0 + stream, err = c.client.CreateChatCompletionStream(ctx, req) + } + if err != nil { return nil, fmt.Errorf("创建流式请求失败: %w", err) } diff --git a/internal/updater/checker.go b/internal/updater/checker.go index cd0b552..45db928 100644 --- a/internal/updater/checker.go +++ b/internal/updater/checker.go @@ -12,7 +12,7 @@ import ( ) // CurrentVersion 当前编译版本号 -const CurrentVersion = "v1.0.5" +const CurrentVersion = "v1.0.6" const ( githubAPIURL = "https://api.github.com/repos/unclesam-ly/git-air/releases/latest"