From f8edc0ec11bf50641283c6228309e24fd547e08a Mon Sep 17 00:00:00 2001 From: Liu Xiang Qian Date: Wed, 5 Nov 2025 09:31:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20add=20AI=5FMAX=5FTOKENS=20environment=20?= =?UTF-8?q?variable=20to=20prevent=20response=20truncation=20##=20Problem?= =?UTF-8?q?=20AI=20responses=20were=20being=20truncated=20due=20to=20a=20h?= =?UTF-8?q?ardcoded=20max=5Ftokens=20limit=20of=202000,=20causing=20JSON?= =?UTF-8?q?=20parsing=20failures.=20The=20error=20occurred=20when:=201.=20?= =?UTF-8?q?AI's=20thought=20process=20analysis=20was=20cut=20off=20mid-res?= =?UTF-8?q?ponse=202.=20extractDecisions()=20incorrectly=20extracted=20MAC?= =?UTF-8?q?D=20data=20arrays=20from=20the=20input=20prompt=203.=20Go=20fai?= =?UTF-8?q?led=20to=20unmarshal=20numbers=20into=20Decision=20struct=20Err?= =?UTF-8?q?or=20message:=20```=20json:=20cannot=20unmarshal=20number=20int?= =?UTF-8?q?o=20Go=20value=20of=20type=20decision.Decision=20JSON=E5=86=85?= =?UTF-8?q?=E5=AE=B9:=20[-867.759,=20-937.406,=20-1020.435,=20...]=20```?= =?UTF-8?q?=20##=20Solution=20-=20Add=20MaxTokens=20field=20to=20mcp.Clien?= =?UTF-8?q?t=20struct=20-=20Read=20AI=5FMAX=5FTOKENS=20from=20environment?= =?UTF-8?q?=20variable=20(default:=202000)=20-=20Set=20AI=5FMAX=5FTOKENS?= =?UTF-8?q?=3D4000=20in=20docker-compose.yml=20for=20production=20use=20-?= =?UTF-8?q?=20This=20provides=20enough=20tokens=20for=20complete=20analysi?= =?UTF-8?q?s=20with=20the=20800-line=20trading=20strategy=20prompt=20##=20?= =?UTF-8?q?Testing=20-=20Verify=20environment=20variable=20is=20read=20cor?= =?UTF-8?q?rectly=20-=20Confirm=20AI=20responses=20are=20no=20longer=20tru?= =?UTF-8?q?ncated=20-=20Check=20decision=20logs=20for=20complete=20JSON=20?= =?UTF-8?q?output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 不支持