Fix mcp defaultConfig override issue in multi-trader, multi-AI model scenario

This commit is contained in:
tpkeeper
2025-10-30 15:46:17 +08:00
parent 897ec1b833
commit 1083c06d1f
3 changed files with 97 additions and 90 deletions

View File

@@ -19,71 +19,74 @@ const (
ProviderCustom Provider = "custom"
)
// Config AI API配置
type Config struct {
Provider Provider
APIKey string
SecretKey string // 阿里云需要
BaseURL string
Model string
Timeout time.Duration
// Client AI API配置
type Client struct {
Provider Provider
APIKey string
SecretKey string // 阿里云需要
BaseURL string
Model string
Timeout time.Duration
UseFullURL bool // 是否使用完整URL不添加/chat/completions
}
// 默认配置
var defaultConfig = Config{
Provider: ProviderDeepSeek,
BaseURL: "https://api.deepseek.com/v1",
Model: "deepseek-chat",
Timeout: 120 * time.Second, // 增加到120秒因为AI需要分析大量数据
func New() *Client {
// 默认配置
var defaultClient = Client{
Provider: ProviderDeepSeek,
BaseURL: "https://api.deepseek.com/v1",
Model: "deepseek-chat",
Timeout: 120 * time.Second, // 增加到120秒因为AI需要分析大量数据
}
return &defaultClient
}
// SetDeepSeekAPIKey 设置DeepSeek API密钥
func SetDeepSeekAPIKey(apiKey string) {
defaultConfig.Provider = ProviderDeepSeek
defaultConfig.APIKey = apiKey
defaultConfig.BaseURL = "https://api.deepseek.com/v1"
defaultConfig.Model = "deepseek-chat"
func (cfg *Client) SetDeepSeekAPIKey(apiKey string) {
cfg.Provider = ProviderDeepSeek
cfg.APIKey = apiKey
cfg.BaseURL = "https://api.deepseek.com/v1"
cfg.Model = "deepseek-chat"
}
// SetQwenAPIKey 设置阿里云Qwen API密钥
func SetQwenAPIKey(apiKey, secretKey string) {
defaultConfig.Provider = ProviderQwen
defaultConfig.APIKey = apiKey
defaultConfig.SecretKey = secretKey
defaultConfig.BaseURL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
defaultConfig.Model = "qwen-plus" // 可选: qwen-turbo, qwen-plus, qwen-max
func (cfg *Client) SetQwenAPIKey(apiKey, secretKey string) {
cfg.Provider = ProviderQwen
cfg.APIKey = apiKey
cfg.SecretKey = secretKey
cfg.BaseURL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
cfg.Model = "qwen-plus" // 可选: qwen-turbo, qwen-plus, qwen-max
}
// SetCustomAPI 设置自定义OpenAI兼容API
func SetCustomAPI(apiURL, apiKey, modelName string) {
defaultConfig.Provider = ProviderCustom
defaultConfig.APIKey = apiKey
func (cfg *Client) SetCustomAPI(apiURL, apiKey, modelName string) {
cfg.Provider = ProviderCustom
cfg.APIKey = apiKey
// 检查URL是否以#结尾如果是则使用完整URL不添加/chat/completions
if strings.HasSuffix(apiURL, "#") {
defaultConfig.BaseURL = strings.TrimSuffix(apiURL, "#")
defaultConfig.UseFullURL = true
cfg.BaseURL = strings.TrimSuffix(apiURL, "#")
cfg.UseFullURL = true
} else {
defaultConfig.BaseURL = apiURL
defaultConfig.UseFullURL = false
cfg.BaseURL = apiURL
cfg.UseFullURL = false
}
defaultConfig.Model = modelName
defaultConfig.Timeout = 120 * time.Second
cfg.Model = modelName
cfg.Timeout = 120 * time.Second
}
// SetConfig 设置完整的AI配置高级用户
func SetConfig(config Config) {
if config.Timeout == 0 {
config.Timeout = 30 * time.Second
// SetClient 设置完整的AI配置高级用户
func (cfg *Client) SetClient(Client Client) {
if Client.Timeout == 0 {
Client.Timeout = 30 * time.Second
}
defaultConfig = config
cfg = &Client
}
// CallWithMessages 使用 system + user prompt 调用AI API推荐
func CallWithMessages(systemPrompt, userPrompt string) (string, error) {
if defaultConfig.APIKey == "" {
func (cfg *Client) CallWithMessages(systemPrompt, userPrompt string) (string, error) {
if cfg.APIKey == "" {
return "", fmt.Errorf("AI API密钥未设置请先调用 SetDeepSeekAPIKey() 或 SetQwenAPIKey()")
}
@@ -96,7 +99,7 @@ func CallWithMessages(systemPrompt, userPrompt string) (string, error) {
fmt.Printf("⚠️ AI API调用失败正在重试 (%d/%d)...\n", attempt, maxRetries)
}
result, err := callOnce(systemPrompt, userPrompt)
result, err := cfg.callOnce(systemPrompt, userPrompt)
if err == nil {
if attempt > 1 {
fmt.Printf("✓ AI API重试成功\n")
@@ -122,7 +125,7 @@ func CallWithMessages(systemPrompt, userPrompt string) (string, error) {
}
// callOnce 单次调用AI API内部使用
func callOnce(systemPrompt, userPrompt string) (string, error) {
func (cfg *Client) callOnce(systemPrompt, userPrompt string) (string, error) {
// 构建 messages 数组
messages := []map[string]string{}
@@ -142,7 +145,7 @@ func callOnce(systemPrompt, userPrompt string) (string, error) {
// 构建请求体
requestBody := map[string]interface{}{
"model": defaultConfig.Model,
"model": cfg.Model,
"messages": messages,
"temperature": 0.5, // 降低temperature以提高JSON格式稳定性
"max_tokens": 2000,
@@ -158,12 +161,12 @@ func callOnce(systemPrompt, userPrompt string) (string, error) {
// 创建HTTP请求
var url string
if defaultConfig.UseFullURL {
if cfg.UseFullURL {
// 使用完整URL不添加/chat/completions
url = defaultConfig.BaseURL
url = cfg.BaseURL
} else {
// 默认行为:添加/chat/completions
url = fmt.Sprintf("%s/chat/completions", defaultConfig.BaseURL)
url = fmt.Sprintf("%s/chat/completions", cfg.BaseURL)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
@@ -173,19 +176,19 @@ func callOnce(systemPrompt, userPrompt string) (string, error) {
req.Header.Set("Content-Type", "application/json")
// 根据不同的Provider设置认证方式
switch defaultConfig.Provider {
switch cfg.Provider {
case ProviderDeepSeek:
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", defaultConfig.APIKey))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.APIKey))
case ProviderQwen:
// 阿里云Qwen使用API-Key认证
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", defaultConfig.APIKey))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.APIKey))
// 注意:如果使用的不是兼容模式,可能需要不同的认证方式
default:
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", defaultConfig.APIKey))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.APIKey))
}
// 发送请求
client := &http.Client{Timeout: defaultConfig.Timeout}
client := &http.Client{Timeout: cfg.Timeout}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("发送请求失败: %w", err)