Skip to content

Commit f35f1ad

Browse files
authored
feat: 添加AI请求耗时记录,优化性能评估 (#587)
# Conflicts: # decision/engine.go
1 parent 3c90d2d commit f35f1ad

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

decision/engine.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"nofx/mcp"
99
"nofx/pool"
1010
"regexp"
11+
"strconv"
1112
"strings"
1213
"time"
1314
)
@@ -114,6 +115,8 @@ type FullDecision struct {
114115
CoTTrace string `json:"cot_trace"` // 思维链分析(AI输出)
115116
Decisions []Decision `json:"decisions"` // 具体决策列表
116117
Timestamp time.Time `json:"timestamp"`
118+
// AIRequestDurationMs 记录 AI API 调用耗时(毫秒)方便排查延迟问题
119+
AIRequestDurationMs int64 `json:"ai_request_duration_ms,omitempty"`
117120
}
118121

119122
// GetFullDecision 获取AI的完整交易决策(批量分析所有币种和持仓)
@@ -133,13 +136,24 @@ func GetFullDecisionWithCustomPrompt(ctx *Context, mcpClient *mcp.Client, custom
133136
userPrompt := buildUserPrompt(ctx)
134137

135138
// 3. 调用AI API(使用 system + user prompt)
139+
aiCallStart := time.Now()
136140
aiResponse, err := mcpClient.CallWithMessages(systemPrompt, userPrompt)
141+
aiCallDuration := time.Since(aiCallStart)
137142
if err != nil {
138143
return nil, fmt.Errorf("调用AI API失败: %w", err)
139144
}
140145

141146
// 4. 解析AI响应
142147
decision, err := parseFullDecisionResponse(aiResponse, ctx.Account.TotalEquity, ctx.BTCETHLeverage, ctx.AltcoinLeverage)
148+
149+
// 无论是否有错误,都要保存 SystemPrompt 和 UserPrompt(用于调试和决策未执行后的问题定位)
150+
if decision != nil {
151+
decision.Timestamp = time.Now()
152+
decision.SystemPrompt = systemPrompt // 保存系统prompt
153+
decision.UserPrompt = userPrompt // 保存输入prompt
154+
decision.AIRequestDurationMs = aiCallDuration.Milliseconds()
155+
}
156+
143157
if err != nil {
144158
return decision, fmt.Errorf("解析AI响应失败: %w", err)
145159
}

logger/decision_logger.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type DecisionRecord struct {
2525
ExecutionLog []string `json:"execution_log"` // 执行日志
2626
Success bool `json:"success"` // 是否成功
2727
ErrorMessage string `json:"error_message"` // 错误信息(如果有)
28+
// AIRequestDurationMs 记录 AI API 调用耗时(毫秒),方便评估调用性能
29+
AIRequestDurationMs int64 `json:"ai_request_duration_ms,omitempty"`
2830
}
2931

3032
// AccountSnapshot 账户状态快照

trader/auto_trader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ func (at *AutoTrader) runCycle() error {
459459
log.Printf("🤖 正在请求AI分析并决策... [模板: %s]", at.systemPromptTemplate)
460460
decision, err := decision.GetFullDecisionWithCustomPrompt(ctx, at.mcpClient, at.customPrompt, at.overrideBasePrompt, at.systemPromptTemplate)
461461

462+
if decision != nil && decision.AIRequestDurationMs > 0 {
463+
record.AIRequestDurationMs = decision.AIRequestDurationMs
464+
log.Printf("⏱️ AI调用耗时: %.2f 秒", float64(record.AIRequestDurationMs)/1000)
465+
record.ExecutionLog = append(record.ExecutionLog,
466+
fmt.Sprintf("AI调用耗时: %d ms", record.AIRequestDurationMs))
467+
}
468+
462469
// 即使有错误,也保存思维链、决策和输入prompt(用于debug)
463470
if decision != nil {
464471
record.SystemPrompt = decision.SystemPrompt // 保存系统提示词

0 commit comments

Comments
 (0)