mirror of
https://github.com/laoxong/nofx.git
synced 2026-06-04 09:58:22 +08:00
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:
@@ -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:
|
||||
|
||||
+16
-1
@@ -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需要分析大量数据
|
||||
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 不支持
|
||||
|
||||
Reference in New Issue
Block a user