mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 09:54:35 +08:00
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:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user