-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: strategic GLM model family detection for LM Studio and OpenAI-compatible providers #11092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
roomote
wants to merge
1
commit into
main
Choose a base branch
from
feature/glm-family-detection-strategic-11071
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+566
−6
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,18 +10,21 @@ import { NativeToolCallParser } from "../../core/assistant-message/NativeToolCal | |||||||
| import { TagMatcher } from "../../utils/tag-matcher" | ||||||||
|
|
||||||||
| import { convertToOpenAiMessages } from "../transform/openai-format" | ||||||||
| import { convertToZAiFormat } from "../transform/zai-format" | ||||||||
| import { ApiStream } from "../transform/stream" | ||||||||
|
|
||||||||
| import { BaseProvider } from "./base-provider" | ||||||||
| import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index" | ||||||||
| import { getModelsFromCache } from "./fetchers/modelCache" | ||||||||
| import { getApiRequestTimeout } from "./utils/timeout-config" | ||||||||
| import { handleOpenAIError } from "./utils/openai-error-handler" | ||||||||
| import { detectGlmModel, logGlmDetection, type GlmModelConfig } from "./utils/glm-model-detection" | ||||||||
|
|
||||||||
| export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler { | ||||||||
| protected options: ApiHandlerOptions | ||||||||
| private client: OpenAI | ||||||||
| private readonly providerName = "LM Studio" | ||||||||
| private glmConfig: GlmModelConfig | null = null | ||||||||
|
|
||||||||
| constructor(options: ApiHandlerOptions) { | ||||||||
| super() | ||||||||
|
|
@@ -35,16 +38,37 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |||||||
| apiKey: apiKey, | ||||||||
| timeout: getApiRequestTimeout(), | ||||||||
| }) | ||||||||
|
|
||||||||
| // Detect GLM model on construction if model ID is available | ||||||||
| const modelId = this.options.lmStudioModelId || "" | ||||||||
| if (modelId) { | ||||||||
| this.glmConfig = detectGlmModel(modelId) | ||||||||
| logGlmDetection(this.providerName, modelId, this.glmConfig) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| override async *createMessage( | ||||||||
| systemPrompt: string, | ||||||||
| messages: Anthropic.Messages.MessageParam[], | ||||||||
| metadata?: ApiHandlerCreateMessageMetadata, | ||||||||
| ): ApiStream { | ||||||||
| const modelId = this.getModel().id | ||||||||
|
|
||||||||
| // Re-detect GLM model if not already done or if model ID changed | ||||||||
| if (!this.glmConfig || this.glmConfig.originalModelId !== modelId) { | ||||||||
| this.glmConfig = detectGlmModel(modelId) | ||||||||
| logGlmDetection(this.providerName, modelId, this.glmConfig) | ||||||||
| } | ||||||||
|
|
||||||||
| // Convert messages based on whether this is a GLM model | ||||||||
| // GLM models benefit from mergeToolResultText to prevent reasoning_content loss | ||||||||
| const convertedMessages = this.glmConfig.isGlmModel | ||||||||
| ? convertToZAiFormat(messages, { mergeToolResultText: this.glmConfig.mergeToolResultText }) | ||||||||
| : convertToOpenAiMessages(messages) | ||||||||
|
|
||||||||
| const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ | ||||||||
| { role: "system", content: systemPrompt }, | ||||||||
| ...convertToOpenAiMessages(messages), | ||||||||
| ...convertedMessages, | ||||||||
| ] | ||||||||
|
|
||||||||
| // ------------------------- | ||||||||
|
|
@@ -83,14 +107,24 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |||||||
| let assistantText = "" | ||||||||
|
|
||||||||
| try { | ||||||||
| // Determine parallel_tool_calls setting | ||||||||
| // Disable for GLM models as they may not support it properly | ||||||||
| let parallelToolCalls: boolean | ||||||||
| if (this.glmConfig?.isGlmModel && this.glmConfig.disableParallelToolCalls) { | ||||||||
| parallelToolCalls = false | ||||||||
| console.log(`[${this.providerName}] parallel_tool_calls disabled for GLM model`) | ||||||||
| } else { | ||||||||
| parallelToolCalls = metadata?.parallelToolCalls ?? true | ||||||||
| } | ||||||||
|
|
||||||||
| const params: OpenAI.Chat.ChatCompletionCreateParamsStreaming & { draft_model?: string } = { | ||||||||
| model: this.getModel().id, | ||||||||
| model: modelId, | ||||||||
| messages: openAiMessages, | ||||||||
| temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE, | ||||||||
| stream: true, | ||||||||
| tools: this.convertToolsForOpenAI(metadata?.tools), | ||||||||
| tool_choice: metadata?.tool_choice, | ||||||||
| parallel_tool_calls: metadata?.parallelToolCalls ?? true, | ||||||||
| parallel_tool_calls: parallelToolCalls, | ||||||||
| } | ||||||||
|
|
||||||||
| if (this.options.lmStudioSpeculativeDecodingEnabled && this.options.lmStudioDraftModelId) { | ||||||||
|
|
@@ -124,6 +158,14 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Handle reasoning_content for GLM models with thinking support | ||||||||
| if (delta && this.glmConfig?.supportsThinking) { | ||||||||
| const deltaAny = delta as any | ||||||||
| if (deltaAny.reasoning_content) { | ||||||||
| yield { type: "reasoning", text: deltaAny.reasoning_content } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Handle tool calls in stream - emit partial chunks for NativeToolCallParser | ||||||||
| if (delta?.tool_calls) { | ||||||||
| for (const toolCall of delta.tool_calls) { | ||||||||
|
|
@@ -186,10 +228,22 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan | |||||||
| } | ||||||||
|
|
||||||||
| async completePrompt(prompt: string): Promise<string> { | ||||||||
| const modelId = this.getModel().id | ||||||||
|
|
||||||||
| // Re-detect GLM model if not already done or if model ID changed | ||||||||
| if (!this.glmConfig || this.glmConfig.originalModelId !== modelId) { | ||||||||
| this.glmConfig = detectGlmModel(modelId) | ||||||||
| logGlmDetection(this.providerName, modelId, this.glmConfig) | ||||||||
| } | ||||||||
|
|
||||||||
| try { | ||||||||
| // Determine parallel_tool_calls setting for GLM models | ||||||||
| const parallelToolCalls = | ||||||||
| this.glmConfig?.isGlmModel && this.glmConfig.disableParallelToolCalls ? false : true | ||||||||
|
Comment on lines
+240
to
+242
Contributor
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. Dead code:
Suggested change
Fix it with Roo Code or mention @roomote and request a fix. |
||||||||
|
|
||||||||
| // Create params object with optional draft model | ||||||||
| const params: any = { | ||||||||
| model: this.getModel().id, | ||||||||
| model: modelId, | ||||||||
| messages: [{ role: "user", content: prompt }], | ||||||||
| temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE, | ||||||||
| stream: false, | ||||||||
|
|
||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Missing
thinkingparameter for GLM-4.7 models. UnlikeBaseOpenAiCompatibleProvider, this handler does not add thethinkingparameter to requests whenthis.glmConfig.supportsThinkingis true. While the code correctly handlesreasoning_contentin responses (lines 161-167), without sending thethinkingparameter, GLM-4.7's thinking mode won't be activated. Consider adding the same logic used inBaseOpenAiCompatibleProvider(lines 138-145) here.Fix it with Roo Code or mention @roomote and request a fix.