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