This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathglm-model-detection.ts
More file actions
80 lines (74 loc) · 2.34 KB
/
Copy pathglm-model-detection.ts
File metadata and controls
80 lines (74 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Utility functions for detecting GLM (General Language Model) models.
*
* GLM models from Z.ai/THUDM may require special handling:
* - mergeToolResultText: true - prevents conversation flow disruption
* - parallel_tool_calls: false - some GLM models do not support this parameter
*/
/**
* Pattern to detect GLM models in model IDs.
*
* This regex matches "glm" anywhere in the model ID (case-insensitive),
* including common variations like:
* - "glm-4.5" (standard Z.ai format)
* - "glm4" (without hyphen)
* - "chatglm" (ChatGLM variants)
* - "mlx-community/GLM-4.5-4bit" (MLX format with prefix)
* - "GLM-4.5-UD-Q8_K_XL-00001-of-00008.gguf" (GGUF format)
* - "THUDM/glm-4-9b-chat" (HuggingFace format)
*/
const GLM_MODEL_PATTERN = /glm/i
/**
* Detects if a model ID represents a GLM (General Language Model) model.
*
* @param modelId - The model ID to check (e.g., "glm-4.5", "mlx-community/GLM-4.5-4bit")
* @returns true if the model ID indicates a GLM model, false otherwise
*
* @example
* ```typescript
* isGlmModel("glm-4.5") // true
* isGlmModel("mlx-community/GLM-4.5-4bit") // true
* isGlmModel("GLM-4.5-UD-Q8_K_XL.gguf") // true
* isGlmModel("chatglm-6b") // true
* isGlmModel("gpt-4") // false
* isGlmModel("llama-3.1") // false
* ```
*/
export function isGlmModel(modelId: string | undefined): boolean {
if (!modelId) {
return false
}
return GLM_MODEL_PATTERN.test(modelId)
}
/**
* Configuration options for GLM models when used via LM Studio
* or OpenAI-compatible endpoints.
*/
export interface GlmModelOptions {
/**
* If true, merge text content after tool_results into the last tool message
* instead of creating a separate user message. This prevents GLM models from
* losing context or reasoning_content after tool results.
*/
mergeToolResultText: boolean
/**
* If true, disable parallel_tool_calls parameter for GLM models
* since they may not support it.
*/
disableParallelToolCalls: boolean
}
/**
* Returns the recommended configuration options for a GLM model.
*
* @param modelId - The model ID to check
* @returns GlmModelOptions if GLM model detected, undefined otherwise
*/
export function getGlmModelOptions(modelId: string | undefined): GlmModelOptions | undefined {
if (!isGlmModel(modelId)) {
return undefined
}
return {
mergeToolResultText: true,
disableParallelToolCalls: true,
}
}