feat: NVIDIA chat model API support custom endpoint

This commit is contained in:
Manx98
2026-03-11 10:48:26 +08:00
committed by lyingbug
parent 5afc81d00b
commit a355ac889e
5 changed files with 76 additions and 11 deletions
@@ -360,7 +360,7 @@ const fallbackProviderOptions = computed(() => [
value: 'nvidia',
label: t('model.editor.providers.nvidia.label'),
defaultUrls: {
chat: 'https://integrate.api.nvidia.com/v1',
chat: 'https://integrate.api.nvidia.com/v1/chat/completions',
embedding: 'https://integrate.api.nvidia.com/v1',
rerank: 'https://ai.api.nvidia.com/v1/retrieval/nvidia/reranking',
vllm: 'https://integrate.api.nvidia.com/v1',
+3
View File
@@ -121,6 +121,9 @@ func NewRemoteChat(config *ChatConfig) (Chat, error) {
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)
+34
View File
@@ -0,0 +1,34 @@
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
}
+37 -9
View File
@@ -28,6 +28,10 @@ type RemoteAPIChat struct {
// requestCustomizer 允许子类自定义请求
// 返回自定义请求体(如果为 nil 则使用标准请求)和是否需要使用原始 HTTP 请求
requestCustomizer func(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (customReq any, useRawHTTP bool)
// endpointCustomizer 允许子类自定义请求的 endpoint
// 返回是否使用自定义请求地址, 返回空则使用默认OpenAI格式地址
endpointCustomizer func(baseURL string, modelID string, isStream bool) (endpoint string)
}
// NewRemoteAPIChat 创建远程 API 聊天实例
@@ -58,6 +62,11 @@ func (c *RemoteAPIChat) SetRequestCustomizer(customizer func(req *openai.ChatCom
c.requestCustomizer = customizer
}
// SetEndpointCustomizer 设置请求地址自定义器
func (c *RemoteAPIChat) SetEndpointCustomizer(customizer func(baseURL string, modelID string, isStream bool) string) {
c.endpointCustomizer = customizer
}
// ConvertMessages 转换消息格式为 OpenAI 格式(导出供子类使用)
func (c *RemoteAPIChat) ConvertMessages(messages []Message) []openai.ChatCompletionMessage {
openaiMessages := make([]openai.ChatCompletionMessage, 0, len(messages))
@@ -178,15 +187,23 @@ func (c *RemoteAPIChat) logRequest(ctx context.Context, req any, isStream bool)
// Chat 进行非流式聊天
func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *ChatOptions) (*types.ChatResponse, error) {
req := c.BuildChatCompletionRequest(messages, opts, false)
var customEndpoint string
if c.endpointCustomizer != nil {
customEndpoint = c.endpointCustomizer(c.baseURL, c.modelID, true)
}
// 检查是否需要自定义请求
if c.requestCustomizer != nil {
customReq, useRawHTTP := c.requestCustomizer(&req, opts, false)
if useRawHTTP && customReq != nil {
return c.chatWithRawHTTP(ctx, customReq)
return c.chatWithRawHTTP(ctx, customEndpoint, customReq)
}
}
// 使用自定义请求地址
if customEndpoint != "" {
return c.chatWithRawHTTP(ctx, customEndpoint, &req)
}
c.logRequest(ctx, req, false)
resp, err := c.client.CreateChatCompletion(ctx, req)
@@ -198,15 +215,16 @@ func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *Chat
}
// chatWithRawHTTP 使用原始 HTTP 请求进行聊天(供自定义请求使用)
func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, customReq any) (*types.ChatResponse, error) {
func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, customReq any) (*types.ChatResponse, error) {
jsonData, err := json.Marshal(customReq)
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
}
logger.Infof(ctx, "[LLM Request] model=%s, raw HTTP request:\n%s", c.modelName, string(jsonData))
endpoint := c.baseURL + "/chat/completions"
if endpoint == "" {
endpoint = c.baseURL + "/chat/completions"
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
@@ -304,14 +322,22 @@ func removeThinkingContent(content string) string {
func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) {
req := c.BuildChatCompletionRequest(messages, opts, true)
var customEndpoint string
if c.endpointCustomizer != nil {
customEndpoint = c.endpointCustomizer(c.baseURL, c.modelID, true)
}
// 检查是否需要自定义请求
if c.requestCustomizer != nil {
customReq, useRawHTTP := c.requestCustomizer(&req, opts, true)
if useRawHTTP && customReq != nil {
return c.chatStreamWithRawHTTP(ctx, customReq)
return c.chatStreamWithRawHTTP(ctx, customEndpoint, customReq)
}
}
// 使用自定义请求地址
if customEndpoint != "" {
return c.chatStreamWithRawHTTP(ctx, customEndpoint, &req)
}
c.logRequest(ctx, req, true)
streamChan := make(chan types.StreamResponse)
@@ -328,7 +354,7 @@ func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts
}
// chatStreamWithRawHTTP 使用原始 HTTP 请求进行流式聊天
func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, customReq any) (<-chan types.StreamResponse, error) {
func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, endpoint string, customReq any) (<-chan types.StreamResponse, error) {
jsonData, err := json.Marshal(customReq)
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
@@ -336,7 +362,9 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, customReq any
logger.Infof(ctx, "[LLM Stream] model=%s", c.modelName)
endpoint := c.baseURL + "/chat/completions"
if endpoint == "" {
endpoint = c.baseURL + "/chat/completions"
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
+1 -1
View File
@@ -8,7 +8,7 @@ import (
const (
// NvidiaChatBaseURL NVIDIA Chat 的默认 BaseURL
NvidiaChatBaseURL = "https://integrate.api.nvidia.com/v1"
NvidiaChatBaseURL = "https://integrate.api.nvidia.com/v1/chat/completions"
// NvidiaRerankBaseURL NVIDIA Rerank 的默认 BaseURL
NvidiaRerankBaseURL = "https://ai.api.nvidia.com/v1/retrieval/nvidia/reranking"
)