From af398f22e12fdc3ad87ff92adb63035e4ea98e25 Mon Sep 17 00:00:00 2001 From: Liu Xiang Qian Date: Wed, 5 Nov 2025 09:31:58 +0800 Subject: [PATCH] fix: add AI_MAX_TOKENS environment variable to prevent response truncation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem AI responses were being truncated due to a hardcoded max_tokens limit of 2000, causing JSON parsing failures. The error occurred when: 1. AI's thought process analysis was cut off mid-response 2. extractDecisions() incorrectly extracted MACD data arrays from the input prompt 3. Go failed to unmarshal numbers into Decision struct Error message: ``` json: cannot unmarshal number into Go value of type decision.Decision JSON内容: [-867.759, -937.406, -1020.435, ...] ``` ## Solution - Add MaxTokens field to mcp.Client struct - Read AI_MAX_TOKENS from environment variable (default: 2000) - Set AI_MAX_TOKENS=4000 in docker-compose.yml for production use - This provides enough tokens for complete analysis with the 800-line trading strategy prompt ## Testing - Verify environment variable is read correctly - Confirm AI responses are no longer truncated - Check decision logs for complete JSON output --- docker-compose.yml | 1 + mcp/client.go | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a9d35026..dc25bb44 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,7 @@ services: - /etc/localtime:/etc/localtime:ro # Sync host time environment: - TZ=${NOFX_TIMEZONE:-Asia/Shanghai} # Set timezone + - AI_MAX_TOKENS=4000 # AI响应的最大token数(默认2000,建议4000-8000) networks: - nofx-network healthcheck: diff --git a/mcp/client.go b/mcp/client.go index 9191dfaf..dd0f873a 100644 --- a/mcp/client.go +++ b/mcp/client.go @@ -7,6 +7,8 @@ import ( "io" "log" "net/http" + "os" + "strconv" "strings" "time" ) @@ -28,15 +30,28 @@ type Client struct { Model string Timeout time.Duration UseFullURL bool // 是否使用完整URL(不添加/chat/completions) + MaxTokens int // AI响应的最大token数 } func New() *Client { + // 从环境变量读取 MaxTokens,默认 2000 + maxTokens := 2000 + if envMaxTokens := os.Getenv("AI_MAX_TOKENS"); envMaxTokens != "" { + if parsed, err := strconv.Atoi(envMaxTokens); err == nil && parsed > 0 { + maxTokens = parsed + log.Printf("🔧 [MCP] 使用环境变量 AI_MAX_TOKENS: %d", maxTokens) + } else { + log.Printf("⚠️ [MCP] 环境变量 AI_MAX_TOKENS 无效 (%s),使用默认值: %d", envMaxTokens, maxTokens) + } + } + // 默认配置 return &Client{ - Provider: ProviderDeepSeek, - BaseURL: "https://api.deepseek.com/v1", - Model: "deepseek-chat", - Timeout: 120 * time.Second, // 增加到120秒,因为AI需要分析大量数据 + Provider: ProviderDeepSeek, + BaseURL: "https://api.deepseek.com/v1", + Model: "deepseek-chat", + Timeout: 120 * time.Second, // 增加到120秒,因为AI需要分析大量数据 + MaxTokens: maxTokens, } } @@ -190,7 +205,7 @@ func (client *Client) callOnce(systemPrompt, userPrompt string) (string, error) "model": client.Model, "messages": messages, "temperature": 0.5, // 降低temperature以提高JSON格式稳定性 - "max_tokens": 2000, + "max_tokens": client.MaxTokens, } // 注意:response_format 参数仅 OpenAI 支持,DeepSeek/Qwen 不支持