-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: add NVIDIA NIM support for thinking models (Kimi-K2) #11112
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,7 +89,22 @@ | |
| const modelId = this.options.openAiModelId ?? "" | ||
| const enabledR1Format = this.options.openAiR1FormatEnabled ?? false | ||
| const isAzureAiInference = this._isAzureAiInference(modelUrl) | ||
| const deepseekReasoner = modelId.includes("deepseek-reasoner") || enabledR1Format | ||
| const isNvidiaNim = this._isNvidiaNim(modelUrl) | ||
|
|
||
| // Auto-detect reasoning/thinking models that require R1 format: | ||
| // - DeepSeek Reasoner models | ||
| // - Models with "thinking" suffix (e.g., kimi-k2-thinking) | ||
| // - DeepSeek R1 models (deepseek-r1, deepseek/deepseek-r1) | ||
| // - QWQ models | ||
| // - Or when user explicitly enables R1 format | ||
| const modelIdLower = modelId.toLowerCase() | ||
| const useR1Format = | ||
| enabledR1Format || | ||
| modelIdLower.includes("deepseek-reasoner") || | ||
| modelIdLower.includes("-thinking") || | ||
| modelIdLower.includes("deepseek-r1") || | ||
| modelIdLower.includes("/deepseek-r1") || | ||
| modelIdLower.includes("qwq") | ||
|
|
||
| if (modelId.includes("o1") || modelId.includes("o3") || modelId.includes("o4")) { | ||
| yield* this.handleO3FamilyMessage(modelId, systemPrompt, messages, metadata) | ||
|
|
@@ -104,7 +119,7 @@ | |
| if (this.options.openAiStreamingEnabled ?? true) { | ||
| let convertedMessages | ||
|
|
||
| if (deepseekReasoner) { | ||
| if (useR1Format) { | ||
| convertedMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) | ||
| } else { | ||
| if (modelInfo.supportsPromptCache) { | ||
|
|
@@ -152,16 +167,25 @@ | |
|
|
||
| const isGrokXAI = this._isGrokXAI(this.options.openAiBaseUrl) | ||
|
|
||
| // Determine if this is a thinking model that needs NVIDIA NIM specific parameters | ||
| const isThinkingModel = modelIdLower.includes("-thinking") || modelIdLower.includes("kimi-k2") | ||
|
|
||
| const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { | ||
| model: modelId, | ||
| temperature: this.options.modelTemperature ?? (deepseekReasoner ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0), | ||
| temperature: this.options.modelTemperature ?? (useR1Format ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0), | ||
| messages: convertedMessages, | ||
| stream: true as const, | ||
| ...(isGrokXAI ? {} : { stream_options: { include_usage: true } }), | ||
| ...(reasoning && reasoning), | ||
| tools: this.convertToolsForOpenAI(metadata?.tools), | ||
| tool_choice: metadata?.tool_choice, | ||
| parallel_tool_calls: metadata?.parallelToolCalls ?? true, | ||
| // Add NVIDIA NIM specific parameters for thinking models | ||
| // See: https://build.nvidia.com/moonshotai/kimi-k2-thinking | ||
| ...(isNvidiaNim && isThinkingModel && { | ||
| chat_template_kwargs: { thinking: true }, | ||
| reasoning_effort: "high", | ||
| }), | ||
| } | ||
|
|
||
| // Add max_tokens if needed | ||
|
|
@@ -221,15 +245,24 @@ | |
| yield this.processUsageMetrics(lastUsage, modelInfo) | ||
| } | ||
| } else { | ||
| // Determine if this is a thinking model that needs NVIDIA NIM specific parameters | ||
| const isThinkingModel = modelIdLower.includes("-thinking") || modelIdLower.includes("kimi-k2") | ||
|
|
||
| const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = { | ||
| model: modelId, | ||
| messages: deepseekReasoner | ||
| messages: useR1Format | ||
| ? convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) | ||
| : [systemMessage, ...convertToOpenAiMessages(messages)], | ||
| // Tools are always present (minimum ALWAYS_AVAILABLE_TOOLS) | ||
| tools: this.convertToolsForOpenAI(metadata?.tools), | ||
| tool_choice: metadata?.tool_choice, | ||
| parallel_tool_calls: metadata?.parallelToolCalls ?? true, | ||
| // Add NVIDIA NIM specific parameters for thinking models | ||
| // See: https://build.nvidia.com/moonshotai/kimi-k2-thinking | ||
| ...(isNvidiaNim && isThinkingModel && { | ||
| chat_template_kwargs: { thinking: true }, | ||
| reasoning_effort: "high", | ||
| }), | ||
| } | ||
|
|
||
| // Add max_tokens if needed | ||
|
|
@@ -508,6 +541,16 @@ | |
| return urlHost.endsWith(".services.ai.azure.com") | ||
| } | ||
|
|
||
| /** | ||
| * Check if the base URL is NVIDIA NIM API. | ||
| * NVIDIA NIM uses integrate.api.nvidia.com for their API endpoint. | ||
| * See: https://build.nvidia.com/docs/overview | ||
| */ | ||
| private _isNvidiaNim(baseUrl?: string): boolean { | ||
| const urlHost = this._getUrlHost(baseUrl) | ||
| return urlHost.includes("api.nvidia.com") || urlHost.includes("nvidia.com") | ||
|
Check failure on line 551 in src/api/providers/openai.ts
|
||
Check failureCode scanning / CodeQL Incomplete URL substring sanitization High
'
api.nvidia.com Error loading related location Loading Check failureCode scanning / CodeQL Incomplete URL substring sanitization High
'
nvidia.com Error loading related location Loading |
||
| } | ||
|
Comment on lines
+544
to
+552
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR adds new detection methods and parameters but lacks unit tests. The existing
Fix it with Roo Code or mention @roomote and request a fix. |
||
|
|
||
| /** | ||
| * Adds max_completion_tokens to the request body if needed based on provider configuration | ||
| * Note: max_tokens is deprecated in favor of max_completion_tokens as per OpenAI documentation | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block is spread after
...(reasoning && reasoning)on line 179, so if a user has configured a different reasoning effort through their model settings, this hardcoded"high"value will silently override their preference. If NVIDIA NIM only supports "high" for thinking models, consider adding a comment explaining this constraint. Otherwise, consider using the user's configured value:reasoning_effort: reasoningEffort ?? "high".Fix it with Roo Code or mention @roomote and request a fix.