optimize(mcp/client): correct receiver name

This commit is contained in:
tpkeeper
2025-10-31 22:24:10 +08:00
parent 93e5703df8
commit 69445594e3

View File

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