Feature: Add support for custom OpenAI-compatible API

This update enables users to configure any OpenAI-compatible API endpoint,
allowing the use of:
- OpenAI official API (GPT-4, GPT-4o, etc.)
- OpenRouter (access to multiple models)
- Local deployed models (Ollama, LM Studio, etc.)
- Other OpenAI-format compatible API services
Changes:
- config: Add custom_api_url, custom_api_key, custom_model_name fields
- mcp: Add SetCustomAPI function and ProviderCustom constant
- trader: Update AI initialization logic to support custom API
- manager: Pass custom API config to trader instances
- Add CUSTOM_API.md documentation with usage examples
- Update config.json.example with custom API sample
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
btcman
2025-10-29 22:48:28 +08:00
parent 3735df24dc
commit a13f39afdd
6 changed files with 241 additions and 3 deletions

View File

@@ -28,6 +28,11 @@ type TraderConfig struct {
QwenKey string `json:"qwen_key,omitempty"`
DeepSeekKey string `json:"deepseek_key,omitempty"`
// 自定义AI API配置支持任何OpenAI格式的API
CustomAPIURL string `json:"custom_api_url,omitempty"`
CustomAPIKey string `json:"custom_api_key,omitempty"`
CustomModelName string `json:"custom_model_name,omitempty"`
InitialBalance float64 `json:"initial_balance"`
ScanIntervalMinutes int `json:"scan_interval_minutes"`
}
@@ -95,8 +100,8 @@ func (c *Config) Validate() error {
if trader.Name == "" {
return fmt.Errorf("trader[%d]: Name不能为空", i)
}
if trader.AIModel != "qwen" && trader.AIModel != "deepseek" {
return fmt.Errorf("trader[%d]: ai_model必须是 'qwen' 'deepseek'", i)
if trader.AIModel != "qwen" && trader.AIModel != "deepseek" && trader.AIModel != "custom" {
return fmt.Errorf("trader[%d]: ai_model必须是 'qwen', 'deepseek' 或 'custom'", i)
}
// 验证交易平台配置
@@ -124,6 +129,17 @@ func (c *Config) Validate() error {
if trader.AIModel == "deepseek" && trader.DeepSeekKey == "" {
return fmt.Errorf("trader[%d]: 使用DeepSeek时必须配置deepseek_key", i)
}
if trader.AIModel == "custom" {
if trader.CustomAPIURL == "" {
return fmt.Errorf("trader[%d]: 使用自定义API时必须配置custom_api_url", i)
}
if trader.CustomAPIKey == "" {
return fmt.Errorf("trader[%d]: 使用自定义API时必须配置custom_api_key", i)
}
if trader.CustomModelName == "" {
return fmt.Errorf("trader[%d]: 使用自定义API时必须配置custom_model_name", i)
}
}
if trader.InitialBalance <= 0 {
return fmt.Errorf("trader[%d]: initial_balance必须大于0", i)
}