Refactor: Separate system and user prompts for AI API

- Split AI prompts into system (cacheable) and user (dynamic) messages
- Add confidence and risk_usd fields to trading decisions
- Reduce temperature to 0.5 for more stable JSON output
- Add retry mechanism for AI API calls
- Simplify candidate display (show top 10 only)
- Improve prompt structure for better AI decision quality
This commit is contained in:
tinkle-community
2025-10-28 23:51:55 +08:00
parent 867ce197a3
commit 7c2cc96458
2 changed files with 166 additions and 14 deletions

View File

@@ -212,7 +212,13 @@ func formatMarketDataForAI(data *MarketData) string {
}
// callDeepSeekAPI 调用AI API支持DeepSeek和Qwen带重试机制
// 兼容旧代码只传user prompt
func callDeepSeekAPI(prompt string) (string, error) {
return callAIWithMessages("", prompt)
}
// callAIWithMessages 使用 system + user prompt 调用AI API推荐
func callAIWithMessages(systemPrompt, userPrompt string) (string, error) {
if defaultConfig.APIKey == "" {
return "", fmt.Errorf("AI API密钥未设置请先调用 SetDeepSeekAPIKey() 或 SetQwenAPIKey()")
}
@@ -226,7 +232,7 @@ func callDeepSeekAPI(prompt string) (string, error) {
fmt.Printf("⚠️ AI API调用失败正在重试 (%d/%d)...\n", attempt, maxRetries)
}
result, err := callDeepSeekAPIOnce(prompt)
result, err := callAIWithMessagesOnce(systemPrompt, userPrompt)
if err == nil {
if attempt > 1 {
fmt.Printf("✓ AI API重试成功\n")
@@ -251,17 +257,34 @@ func callDeepSeekAPI(prompt string) (string, error) {
return "", fmt.Errorf("重试%d次后仍然失败: %w", maxRetries, lastErr)
}
// callDeepSeekAPIOnce 单次调用AI API
// callDeepSeekAPIOnce 单次调用AI API(兼容旧代码)
func callDeepSeekAPIOnce(prompt string) (string, error) {
return callAIWithMessagesOnce("", prompt)
}
// callAIWithMessagesOnce 单次调用AI API支持 system + user prompt
func callAIWithMessagesOnce(systemPrompt, userPrompt string) (string, error) {
// 构建 messages 数组
messages := []map[string]string{}
// 如果有 system prompt添加 system message
if systemPrompt != "" {
messages = append(messages, map[string]string{
"role": "system",
"content": systemPrompt,
})
}
// 添加 user message
messages = append(messages, map[string]string{
"role": "user",
"content": userPrompt,
})
// 构建请求体
requestBody := map[string]interface{}{
"model": defaultConfig.Model,
"messages": []map[string]string{
{
"role": "user",
"content": prompt,
},
},
"model": defaultConfig.Model,
"messages": messages,
"temperature": 0.5, // 降低temperature以提高JSON格式稳定性
"max_tokens": 2000,
}