docs/getting-started/custom-api.md
现在 NOFX 支持使用任何 OpenAI 格式兼容的 API,包括:
在 config.json 中添加使用自定义 API 的 trader(已弃用):
注意:现在通过Web界面配置自定义API和交易员,config.json仅保留基础设置
{
"traders": [
{
"id": "trader_custom",
"name": "My Custom AI Trader",
"ai_model": "custom",
"exchange": "binance",
"binance_api_key": "your_binance_api_key",
"binance_secret_key": "your_binance_secret_key",
"custom_api_url": "https://api.openai.com/v1",
"custom_api_key": "sk-your-openai-api-key",
"custom_model_name": "gpt-4o",
"initial_balance": 1000,
"scan_interval_minutes": 3
}
]
}
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
ai_model | string | ✅ | 设置为 "custom" 启用自定义 API |
custom_api_url | string | ✅ | API 的 Base URL (不含 /chat/completions)。特殊用法:如果以 # 结尾,则使用完整 URL(不自动添加路径) |
custom_api_key | string | ✅ | API 密钥 |
custom_model_name | string | ✅ | 模型名称 (如 gpt-4o, claude-3-5-sonnet 等) |
{
"ai_model": "custom",
"custom_api_url": "https://api.openai.com/v1",
"custom_api_key": "sk-proj-xxxxx",
"custom_model_name": "gpt-4o"
}
{
"ai_model": "custom",
"custom_api_url": "https://openrouter.ai/api/v1",
"custom_api_key": "sk-or-xxxxx",
"custom_model_name": "anthropic/claude-3.5-sonnet"
}
{
"ai_model": "custom",
"custom_api_url": "http://localhost:11434/v1",
"custom_api_key": "ollama",
"custom_model_name": "llama3.1:70b"
}
{
"ai_model": "custom",
"custom_api_url": "https://your-resource.openai.azure.com/openai/deployments/your-deployment",
"custom_api_key": "your-azure-api-key",
"custom_model_name": "gpt-4"
}
对于某些特殊的 API 端点,如果已经包含完整路径(包括 /chat/completions 或其他自定义路径),可以在 URL 末尾添加 # 来强制使用完整 URL:
{
"ai_model": "custom",
"custom_api_url": "https://api.example.com/v2/ai/chat/completions#",
"custom_api_key": "your-api-key",
"custom_model_name": "custom-model"
}
注意:# 会被自动去除,实际请求会发送到 https://api.example.com/v2/ai/chat/completions
自定义 API 必须:
POST 请求到 /chat/completions 端点(或在 URL 末尾添加 # 以使用自定义路径)Authorization: Bearer {api_key} 认证URL 格式:custom_api_url 应该是 Base URL,系统会自动添加 /chat/completions
https://api.openai.com/v1https://api.openai.com/v1/chat/completions/chat/completions),可以在 URL 末尾添加 #
https://api.example.com/custom/path/chat/completions## 并直接使用该完整 URL模型名称:确保 custom_model_name 与 API 提供商支持的模型名称完全一致
API 密钥:某些本地部署的模型可能不需要真实的 API 密钥,可以填写任意字符串
超时设置:默认超时时间为 120 秒,如果模型响应较慢可能需要调整
你可以同时配置多个不同 AI 的 trader 进行对比:
{
"traders": [
{
"id": "deepseek_trader",
"ai_model": "deepseek",
"deepseek_key": "sk-xxxxx",
...
},
{
"id": "gpt4_trader",
"ai_model": "custom",
"custom_api_url": "https://api.openai.com/v1",
"custom_api_key": "sk-xxxxx",
"custom_model_name": "gpt-4o",
...
},
{
"id": "claude_trader",
"ai_model": "custom",
"custom_api_url": "https://openrouter.ai/api/v1",
"custom_api_key": "sk-or-xxxxx",
"custom_model_name": "anthropic/claude-3.5-sonnet",
...
}
]
}
错误信息:使用自定义API时必须配置custom_api_url
解决方案:确保设置了 ai_model: "custom" 后,同时配置了:
custom_api_urlcustom_api_keycustom_model_name可能原因:
/chat/completions(系统会自动添加)#调试方法:查看日志中的错误信息,通常会包含 HTTP 状态码和错误详情
现有的 deepseek 和 qwen 配置完全不受影响,可以继续使用:
{
"ai_model": "deepseek",
"deepseek_key": "sk-xxxxx"
}
或
{
"ai_model": "qwen",
"qwen_key": "sk-xxxxx"
}