Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat: NVIDIA chat model API support custom endpoint
Manx98a355ac889e47
diff --git a/frontend/src/components/ModelEditorDialog.vue b/frontend/src/components/ModelEditorDialog.vueindex b1918d30b..7db7a57ab 100644--- a/frontend/src/components/ModelEditorDialog.vue+++ b/frontend/src/components/ModelEditorDialog.vue@@ -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',diff --git a/internal/models/chat/chat.go b/internal/models/chat/chat.goindex 781804fb0..2cb1752a3 100644--- a/internal/models/chat/chat.go+++ b/internal/models/chat/chat.go@@ -121,6 +121,9 @@ func NewRemoteChat(config *ChatConfig) (Chat, error) {case provider.ProviderGeneric:// Generic provider (如 vLLM) 使用 ChatTemplateKwargsreturn NewGenericChat(config)+ case provider.ProviderNvidia:+ // NVIDIA provider 使用BaseURL为请求地址+ return NewNvidiaChat(config)default:// 其他 provider 使用标准 OpenAI 兼容实现return NewRemoteAPIChat(config)diff --git a/internal/models/chat/nvidia.go b/internal/models/chat/nvidia.gonew file mode 100644index 000000000..08ad176b3--- /dev/null+++ b/internal/models/chat/nvidia.go@@ -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+}diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.goindex 1900fd050..c7ad5c6f3 100644--- a/internal/models/chat/remote_api.go+++ b/internal/models/chat/remote_api.go@@ -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.ChatComc.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 anylogger.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)diff --git a/internal/models/provider/nvidia.go b/internal/models/provider/nvidia.goindex 9fb87c0de..6f2a93058 100644--- a/internal/models/provider/nvidia.go+++ b/internal/models/provider/nvidia.go@@ -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 的默认 BaseURLNvidiaRerankBaseURL = "https://ai.api.nvidia.com/v1/retrieval/nvidia/reranking")