Files
nofx/mcp/qwen_client.go
Shui b60383f22b refactor(mcp) (#1042)
* improve(interface): replace with interface
* feat(mcp): 添加构建器模式支持
新增功能:
- RequestBuilder 构建器,支持流式 API
- 多轮对话支持(AddAssistantMessage)
- Function Calling / Tools 支持
- 精细参数控制(temperature, top_p, penalties 等)
- 3个预设场景(Chat, CodeGen, CreativeWriting)
- 完整的测试套件(19个新测试)
修复问题:
- Config 字段未使用(MaxRetries、Temperature 等)
- DeepSeek/Qwen SetAPIKey 的冗余 nil 检查
向后兼容:
- 保留 CallWithMessages API
- 新增 CallWithRequest API
测试:
- 81 个测试全部通过
- 覆盖率 80.6%
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
---------
Co-authored-by: zbhan <zbhan@freewheel.tv>
Co-authored-by: tinkle-community <tinklefund@gmail.com>
2025-11-15 23:04:53 -05:00

84 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package mcp
import (
"net/http"
)
const (
ProviderQwen = "qwen"
DefaultQwenBaseURL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
DefaultQwenModel = "qwen3-max"
)
type QwenClient struct {
*Client
}
// NewQwenClient 创建 Qwen 客户端(向前兼容)
//
// Deprecated: 推荐使用 NewQwenClientWithOptions 以获得更好的灵活性
func NewQwenClient() AIClient {
return NewQwenClientWithOptions()
}
// NewQwenClientWithOptions 创建 Qwen 客户端(支持选项模式)
//
// 使用示例:
// // 基础用法
// client := mcp.NewQwenClientWithOptions()
//
// // 自定义配置
// client := mcp.NewQwenClientWithOptions(
// mcp.WithAPIKey("sk-xxx"),
// mcp.WithLogger(customLogger),
// mcp.WithTimeout(60*time.Second),
// )
func NewQwenClientWithOptions(opts ...ClientOption) AIClient {
// 1. 创建 Qwen 预设选项
qwenOpts := []ClientOption{
WithProvider(ProviderQwen),
WithModel(DefaultQwenModel),
WithBaseURL(DefaultQwenBaseURL),
}
// 2. 合并用户选项(用户选项优先级更高)
allOpts := append(qwenOpts, opts...)
// 3. 创建基础客户端
baseClient := NewClient(allOpts...).(*Client)
// 4. 创建 Qwen 客户端
qwenClient := &QwenClient{
Client: baseClient,
}
// 5. 设置 hooks 指向 QwenClient实现动态分派
baseClient.hooks = qwenClient
return qwenClient
}
func (qwenClient *QwenClient) SetAPIKey(apiKey string, customURL string, customModel string) {
qwenClient.APIKey = apiKey
if len(apiKey) > 8 {
qwenClient.logger.Infof("🔧 [MCP] Qwen API Key: %s...%s", apiKey[:4], apiKey[len(apiKey)-4:])
}
if customURL != "" {
qwenClient.BaseURL = customURL
qwenClient.logger.Infof("🔧 [MCP] Qwen 使用自定义 BaseURL: %s", customURL)
} else {
qwenClient.logger.Infof("🔧 [MCP] Qwen 使用默认 BaseURL: %s", qwenClient.BaseURL)
}
if customModel != "" {
qwenClient.Model = customModel
qwenClient.logger.Infof("🔧 [MCP] Qwen 使用自定义 Model: %s", customModel)
} else {
qwenClient.logger.Infof("🔧 [MCP] Qwen 使用默认 Model: %s", qwenClient.Model)
}
}
func (qwenClient *QwenClient) setAuthHeader(reqHeaders http.Header) {
qwenClient.Client.setAuthHeader(reqHeaders)
}