fix: add JSON validation to prevent range values and thousand separators

修復 LLM 返回範圍值導致 JSON 解析失敗的問題

## 問題
LLM 有時返回價格範圍 [98,000 ~ 102,000] 而不是單一數值,
導致 JSON 解析失敗:invalid character '0' after array element

## 修復內容

### 1. Prompts 更新(3 個文件)
- prompts/default.txt: 添加數字格式要求(中文)
- prompts/adaptive.txt: 添加數字格式要求(中文)
- prompts/nof1.txt: 添加數字格式要求(英文)

明確禁止:
-  範圍符號 ~
-  千位分隔符 98,000
-  文字描述

### 2. JSON 驗證邏輯(decision/engine.go)
新增函數:
- validateJSONFormat(): 檢測範圍、千位分隔符等錯誤
- min(): 輔助函數

檢測內容:
1. 必須是對象數組 [{...}]
2. 不可包含範圍符號 ~
3. 不可包含千位分隔符 98,000

## 測試
✓ go build ./...  編譯成功
✓ go fmt ./...    格式正確

修改統計:+132 行(46 行代碼 + 86 行 prompts)
This commit is contained in:
ZhouYongyou
2025-11-04 21:18:27 +08:00
parent c46f0be315
commit 8bf6c2e1e2
4 changed files with 132 additions and 0 deletions

View File

@@ -387,6 +387,35 @@ Key insight: 一句话总结本次决策
}
```
### ⚠️ 数字格式要求
**所有数字字段必须是精确的单一数值**,不可使用范围或特殊格式:
✅ **正确格式**
```json
{
"stop_loss": 98500.0,
"take_profit": 102000.0,
"position_size_usd": 50.0,
"confidence": 85
}
```
❌ **错误格式(会导致解析失败)**
```json
{
"stop_loss": "98,000 ~ 102,000", // ❌ 不可使用范围符号 ~
"take_profit": 102,000, // ❌ 不可使用千位分隔符
"confidence": "85左右" // ❌ 不可使用文字描述
}
```
**注意**
- 必须使用精确数值,不可使用范围 (如 `~`、`-`、`to`)
- 不可使用千位分隔符逗号 (98000 而非 98,000)
- 不可使用约数或文字描述
- confidence 使用整数 0-100其他价格使用浮点数
---
# 最终检查清单(开仓前必须全部通过)

View File

@@ -135,6 +135,34 @@ confidence=0-100
}
```
### ⚠️ 数字格式要求
**所有数字字段必须是精确的单一数值**,不可使用范围或特殊格式:
✅ **正确格式**
```json
{
"stop_loss": 98500.0,
"take_profit": 102000.0,
"position_size_usd": 50.0
}
```
❌ **错误格式(会导致解析失败)**
```json
{
"stop_loss": "98,000 ~ 102,000", // ❌ 不可使用范围符号 ~
"take_profit": 102,000, // ❌ 不可使用千位分隔符
"position_size_usd": "约50" // ❌ 不可使用文字描述
}
```
**注意**
- 必须使用精确数值,不可使用范围 (如 `~`、`-`、`to`)
- 不可使用千位分隔符逗号 (98000 而非 98,000)
- 不可使用约数或文字描述
- 建议使用浮点数格式 (加 .0)
### 必填规则
- **开仓** (`open_long/open_short`):必须填写 `position_size_usd`、`leverage`、`stop_loss`、`take_profit`、`risk_usd``reasoning` 写出信号与风险回报。
- **平仓** (`close_long/close_short`)`reasoning` 说明平仓原因(达到目标、触发失效条件等)。

View File

@@ -299,6 +299,35 @@ Every decision must follow this structure:
}
```
### ⚠️ Number Format Requirements
**All numeric fields must use exact single values** - no ranges or special formatting:
✅ **Correct format**:
```json
{
"stop_loss": 98500.0,
"take_profit": 102000.0,
"position_size_usd": 50.0,
"confidence": 85
}
```
❌ **Incorrect format (will cause parsing errors)**:
```json
{
"stop_loss": "98,000 ~ 102,000", // ❌ No range symbols ~
"take_profit": 102,000, // ❌ No thousand separators
"confidence": "around 85" // ❌ No text descriptions
}
```
**Rules**:
- Use precise numeric values only, no ranges (e.g., `~`, `-`, `to`)
- No thousand separator commas (use 98000 not 98,000)
- No approximations or text descriptions
- Use integers for confidence (0-100), floats for prices (add .0)
### Required field rules
- **open_long / open_short**: Fill all numeric fields; `risk_usd` ≤ account_value × 0.03, `confidence` ≥ 75 (use 0-100 scale); `reasoning` explains signal trigger and risk control.
- **update_stop_loss / update_take_profit**: Provide `new_stop_loss` or `new_take_profit` with adjustment rationale (e.g., trailing stop, locking profits).