feat(chat): introduce provider-specific request customizers and specs

- Added a new `ProviderSpec` struct to define provider-specific behaviors for chat completions.
- Implemented request and endpoint customizers for various providers including Qwen3, LKEAP, DeepSeek, and Generic.
- Refactored the `NewRemoteChat` function to utilize the new provider specs for customizing requests based on the provider and model name.
- Removed legacy chat implementations for LKEAP, NVIDIA, DeepSeek, and Qwen, consolidating their logic into the new structure.
This commit is contained in:
wizardchen
2026-03-19 20:56:25 +08:00
committed by lyingbug
parent e936e0b347
commit 9f4fb39e10
6 changed files with 178 additions and 278 deletions
+15 -22
View File
@@ -120,27 +120,20 @@ func NewRemoteChat(config *ChatConfig) (Chat, error) {
providerName = provider.DetectProvider(config.BaseURL)
}
switch providerName {
case provider.ProviderLKEAP:
// LKEAP 有特殊的 thinking 参数格式
return NewLKEAPChat(config)
case provider.ProviderAliyun:
// 检查是否为 Qwen3 模型(需要特殊处理 enable_thinking)
if provider.IsQwen3Model(config.ModelName) {
return NewQwenChat(config)
}
return NewRemoteAPIChat(config)
case provider.ProviderDeepSeek:
// DeepSeek 不支持 tool_choice
return NewDeepSeekChat(config)
case provider.ProviderGeneric:
// Generic provider (如 vLLM) 使用 ChatTemplateKwargs
return NewGenericChat(config)
case provider.ProviderNvidia:
// NVIDIA provider 使用BaseURL为请求地址
return NewNvidiaChat(config)
default:
// 其他 provider 使用标准 OpenAI 兼容实现
return NewRemoteAPIChat(config)
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
// Look up provider-specific behavior from spec registry
if spec := findProviderSpec(providerName, config.ModelName); spec != nil {
if spec.RequestCustomizer != nil {
remoteChat.SetRequestCustomizer(spec.RequestCustomizer)
}
if spec.EndpointCustomizer != nil {
remoteChat.SetEndpointCustomizer(spec.EndpointCustomizer)
}
}
return remoteChat, nil
}
+163
View File
@@ -0,0 +1,163 @@
package chat
import (
"context"
"strings"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/sashabaranov/go-openai"
)
// ProviderSpec describes provider-specific behavior for chat completions.
// Each spec is registered with a ProviderName and optionally a model matcher.
type ProviderSpec struct {
Provider provider.ProviderName
// ModelMatcher: if non-nil, this spec only applies when the model name matches.
// Used for sub-provider routing (e.g. Qwen3 within Aliyun).
ModelMatcher func(modelName string) bool
// RequestCustomizer: provider-specific request modification.
RequestCustomizer func(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool)
// EndpointCustomizer: provider-specific endpoint URL override.
EndpointCustomizer func(baseURL string, modelID string, isStream bool) string
}
// chatProviderSpecs is the ordered list of provider specs.
// Order matters: more specific specs (with ModelMatcher) should come before generic ones.
var chatProviderSpecs = []ProviderSpec{
// Qwen3 (must be before generic Aliyun — only matches Qwen3 models)
{
Provider: provider.ProviderAliyun,
ModelMatcher: func(name string) bool { return provider.IsQwen3Model(name) },
RequestCustomizer: qwen3RequestCustomizer,
},
// LKEAP
{
Provider: provider.ProviderLKEAP,
RequestCustomizer: lkeapRequestCustomizer,
},
// DeepSeek
{
Provider: provider.ProviderDeepSeek,
RequestCustomizer: deepseekRequestCustomizer,
},
// Generic (vLLM)
{
Provider: provider.ProviderGeneric,
RequestCustomizer: genericRequestCustomizer,
},
// NVIDIA
{
Provider: provider.ProviderNvidia,
EndpointCustomizer: nvidiaEndpointCustomizer,
},
}
// findProviderSpec finds the matching spec for the given provider and model name.
func findProviderSpec(providerName provider.ProviderName, modelName string) *ProviderSpec {
for i := range chatProviderSpecs {
spec := &chatProviderSpecs[i]
if spec.Provider != providerName {
continue
}
if spec.ModelMatcher != nil && !spec.ModelMatcher(modelName) {
continue
}
return spec
}
return nil
}
// --- Type definitions (moved from wrapper files) ---
// QwenChatCompletionRequest Qwen 模型的自定义请求结构体
type QwenChatCompletionRequest struct {
openai.ChatCompletionRequest
EnableThinking *bool `json:"enable_thinking,omitempty"`
}
// LKEAPThinkingConfig 思维链配置(LKEAP 特有格式)
type LKEAPThinkingConfig struct {
Type string `json:"type"` // "enabled" 或 "disabled"
}
// LKEAPChatCompletionRequest LKEAP 自定义请求结构体
type LKEAPChatCompletionRequest struct {
openai.ChatCompletionRequest
Thinking *LKEAPThinkingConfig `json:"thinking,omitempty"` // 思维链开关(仅 V3.x 系列)
}
// --- Customizer functions ---
// qwen3RequestCustomizer 自定义 Qwen3 请求
// Qwen3 模型在非流式请求时需要显式禁用 thinking
func qwen3RequestCustomizer(
req *openai.ChatCompletionRequest, _ *ChatOptions, isStream bool,
) (any, bool) {
if !isStream {
qwenReq := QwenChatCompletionRequest{
ChatCompletionRequest: *req,
}
enableThinking := false
qwenReq.EnableThinking = &enableThinking
return qwenReq, true
}
return nil, false
}
// lkeapRequestCustomizer 自定义 LKEAP 请求
// 仅对 DeepSeek V3.x 系列模型设置 thinking 参数;R1 系列默认开启思维链
// 参考:https://cloud.tencent.com/document/product/1772/115963
func lkeapRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
modelName := req.Model
if !strings.Contains(strings.ToLower(modelName), "deepseek-v3") || opts == nil || opts.Thinking == nil {
return nil, false
}
lkeapReq := LKEAPChatCompletionRequest{
ChatCompletionRequest: *req,
}
thinkingType := "disabled"
if *opts.Thinking {
thinkingType = "enabled"
}
lkeapReq.Thinking = &LKEAPThinkingConfig{Type: thinkingType}
return lkeapReq, true
}
// deepseekRequestCustomizer 自定义 DeepSeek 请求
// DeepSeek 模型不支持 tool_choice 参数,需要清除
func deepseekRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
if opts != nil && opts.ToolChoice != "" {
logger.Infof(context.Background(), "deepseek model, skip tool_choice")
req.ToolChoice = nil
}
return nil, false
}
// genericRequestCustomizer 自定义 Generic 请求
// Generic provider(如 vLLM)使用 ChatTemplateKwargs 传递 thinking 参数
func genericRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
thinking := false
if opts != nil && opts.Thinking != nil {
thinking = *opts.Thinking
}
req.ChatTemplateKwargs = map[string]interface{}{
"enable_thinking": thinking,
}
return nil, false
}
// nvidiaEndpointCustomizer 自定义 NVIDIA 请求地址
// NVIDIA 模型使用 BaseURL 作为完整请求地址
func nvidiaEndpointCustomizer(baseURL string, _ string, _ bool) string {
return baseURL
}
-77
View File
@@ -1,77 +0,0 @@
package chat
import (
"strings"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/sashabaranov/go-openai"
)
// LKEAPChat 腾讯云知识引擎原子能力 (LKEAP) 聊天实现
// 支持 DeepSeek-R1, DeepSeek-V3 系列模型,具备思维链能力
// 参考:https://cloud.tencent.com/document/product/1772/115963
//
// 与标准 OpenAI API 的区别:
// 1. thinking 参数格式不同:LKEAP 使用 {"type": "enabled"/"disabled"}
// 2. 仅 DeepSeek V3.x 系列需要显式设置 thinking 参数,R1 系列默认开启
type LKEAPChat struct {
*RemoteAPIChat
}
// LKEAPThinkingConfig 思维链配置(LKEAP 特有格式)
type LKEAPThinkingConfig struct {
Type string `json:"type"` // "enabled" 或 "disabled"
}
// LKEAPChatCompletionRequest LKEAP 自定义请求结构体
type LKEAPChatCompletionRequest struct {
openai.ChatCompletionRequest
Thinking *LKEAPThinkingConfig `json:"thinking,omitempty"` // 思维链开关(仅 V3.x 系列)
}
// NewLKEAPChat 创建 LKEAP 聊天实例
func NewLKEAPChat(config *ChatConfig) (*LKEAPChat, error) {
// 确保 provider 设置正确
config.Provider = string(provider.ProviderLKEAP)
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
chat := &LKEAPChat{
RemoteAPIChat: remoteChat,
}
// 设置请求自定义器,添加 LKEAP 特有的 thinking 参数
remoteChat.SetRequestCustomizer(chat.customizeRequest)
return chat, nil
}
// isDeepSeekV3Model 检查是否为 DeepSeek V3.x 系列模型
func (c *LKEAPChat) isDeepSeekV3Model() bool {
return strings.Contains(strings.ToLower(c.GetModelName()), "deepseek-v3")
}
// customizeRequest 自定义 LKEAP 请求
func (c *LKEAPChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
// 仅对 DeepSeek V3.x 系列模型需要特殊处理 thinking 参数
// R1 系列模型默认开启思维链,无需额外参数
if !c.isDeepSeekV3Model() || opts == nil || opts.Thinking == nil {
return nil, false // 使用标准请求
}
// 构建 LKEAP 特有请求
lkeapReq := LKEAPChatCompletionRequest{
ChatCompletionRequest: *req,
}
thinkingType := "disabled"
if *opts.Thinking {
thinkingType = "enabled"
}
lkeapReq.Thinking = &LKEAPThinkingConfig{Type: thinkingType}
return lkeapReq, true // 使用原始 HTTP 请求
}
-34
View File
@@ -1,34 +0,0 @@
package chat
import (
"github.com/Tencent/WeKnora/internal/models/provider"
)
// NvidiaChat NVIDIA 模型聊天实现
// NVIDIA 模型需要自定义请求地址
type NvidiaChat struct {
*RemoteAPIChat
}
// NewNvidiaChat 创建 NVIDIA 聊天实例
func NewNvidiaChat(config *ChatConfig) (*NvidiaChat, error) {
config.Provider = string(provider.ProviderAliyun)
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
chat := &NvidiaChat{
RemoteAPIChat: remoteChat,
}
// 设置请求地址自定义器
remoteChat.SetEndpointCustomizer(chat.endpointCustomizer)
return chat, nil
}
// customizeRequest 自定义 Qwen 请求
func (c *NvidiaChat) endpointCustomizer(baseURL string, modelID string, isStream bool) string {
return baseURL
}
-83
View File
@@ -1,83 +0,0 @@
package chat
import (
"context"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/sashabaranov/go-openai"
)
// DeepSeekChat DeepSeek 模型聊天实现
// DeepSeek 模型不支持 tool_choice 参数
type DeepSeekChat struct {
*RemoteAPIChat
}
// NewDeepSeekChat 创建 DeepSeek 聊天实例
func NewDeepSeekChat(config *ChatConfig) (*DeepSeekChat, error) {
config.Provider = string(provider.ProviderDeepSeek)
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
chat := &DeepSeekChat{
RemoteAPIChat: remoteChat,
}
// 设置请求自定义器
remoteChat.SetRequestCustomizer(chat.customizeRequest)
return chat, nil
}
// customizeRequest 自定义 DeepSeek 请求
func (c *DeepSeekChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
// DeepSeek 模型不支持 tool_choice,需要清除
if opts != nil && opts.ToolChoice != "" {
logger.Infof(context.Background(), "deepseek model, skip tool_choice")
req.ToolChoice = nil
}
return nil, false
}
// GenericChat 通用 OpenAI 兼容实现(如 vLLM)
// 支持 ChatTemplateKwargs 参数
type GenericChat struct {
*RemoteAPIChat
}
// NewGenericChat 创建通用聊天实例
func NewGenericChat(config *ChatConfig) (*GenericChat, error) {
config.Provider = string(provider.ProviderGeneric)
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
chat := &GenericChat{
RemoteAPIChat: remoteChat,
}
// 设置请求自定义器
remoteChat.SetRequestCustomizer(chat.customizeRequest)
return chat, nil
}
// customizeRequest 自定义 Generic 请求
func (c *GenericChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
// Generic provider(如 vLLM)使用 ChatTemplateKwargs 传递 thinking 参数
thinking := false
if opts != nil && opts.Thinking != nil {
thinking = *opts.Thinking
}
req.ChatTemplateKwargs = map[string]interface{}{
"enable_thinking": thinking,
}
return req, true
}
-62
View File
@@ -1,62 +0,0 @@
package chat
import (
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/sashabaranov/go-openai"
)
// QwenChat 阿里云 Qwen 模型聊天实现
// Qwen3 模型需要特殊处理 enable_thinking 参数
type QwenChat struct {
*RemoteAPIChat
}
// QwenChatCompletionRequest Qwen 模型的自定义请求结构体
type QwenChatCompletionRequest struct {
openai.ChatCompletionRequest
EnableThinking *bool `json:"enable_thinking,omitempty"`
}
// NewQwenChat 创建 Qwen 聊天实例
func NewQwenChat(config *ChatConfig) (*QwenChat, error) {
config.Provider = string(provider.ProviderAliyun)
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
chat := &QwenChat{
RemoteAPIChat: remoteChat,
}
// 设置请求自定义器
remoteChat.SetRequestCustomizer(chat.customizeRequest)
return chat, nil
}
// isQwen3Model 检查是否为 Qwen3 模型
func (c *QwenChat) isQwen3Model() bool {
return provider.IsQwen3Model(c.GetModelName())
}
// customizeRequest 自定义 Qwen 请求
func (c *QwenChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
// 仅 Qwen3 模型需要特殊处理
if !c.isQwen3Model() {
return nil, false
}
// 非流式请求需要显式禁用 thinking
if !isStream {
qwenReq := QwenChatCompletionRequest{
ChatCompletionRequest: *req,
}
enableThinking := false
qwenReq.EnableThinking = &enableThinking
return qwenReq, true
}
return nil, false
}