fix: add AI_MAX_TOKENS environment variable to prevent response truncation

## 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
This commit is contained in:
Liu Xiang Qian
2025-11-05 09:31:58 +08:00
parent 54f5637e34
commit f8edc0ec11
2 changed files with 21 additions and 5 deletions
+1
View File
@@ -17,6 +17,7 @@ services:
- /etc/localtime:/etc/localtime:ro # Sync host time - /etc/localtime:/etc/localtime:ro # Sync host time
environment: environment:
- TZ=${NOFX_TIMEZONE:-Asia/Shanghai} # Set timezone - TZ=${NOFX_TIMEZONE:-Asia/Shanghai} # Set timezone
- AI_MAX_TOKENS=4000 # AI响应的最大token数(默认2000,建议4000-8000
networks: networks:
- nofx-network - nofx-network
healthcheck: healthcheck:
+20 -5
View File
@@ -7,6 +7,8 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"os"
"strconv"
"strings" "strings"
"time" "time"
) )
@@ -28,15 +30,28 @@ type Client struct {
Model string Model string
Timeout time.Duration Timeout time.Duration
UseFullURL bool // 是否使用完整URL(不添加/chat/completions UseFullURL bool // 是否使用完整URL(不添加/chat/completions
MaxTokens int // AI响应的最大token数
} }
func New() *Client { 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{ return &Client{
Provider: ProviderDeepSeek, Provider: ProviderDeepSeek,
BaseURL: "https://api.deepseek.com/v1", BaseURL: "https://api.deepseek.com/v1",
Model: "deepseek-chat", Model: "deepseek-chat",
Timeout: 120 * time.Second, // 增加到120秒,因为AI需要分析大量数据 Timeout: 120 * time.Second, // 增加到120秒,因为AI需要分析大量数据
MaxTokens: maxTokens,
} }
} }
@@ -190,7 +205,7 @@ func (client *Client) callOnce(systemPrompt, userPrompt string) (string, error)
"model": client.Model, "model": client.Model,
"messages": messages, "messages": messages,
"temperature": 0.5, // 降低temperature以提高JSON格式稳定性 "temperature": 0.5, // 降低temperature以提高JSON格式稳定性
"max_tokens": 2000, "max_tokens": client.MaxTokens,
} }
// 注意:response_format 参数仅 OpenAI 支持,DeepSeek/Qwen 不支持 // 注意:response_format 参数仅 OpenAI 支持,DeepSeek/Qwen 不支持