@wuzhiguocarter/zhipu-ai-provider 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -2
- package/dist/index.d.mts +29 -3
- package/dist/index.d.ts +29 -3
- package/dist/index.js +26 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -74,7 +74,10 @@ import { zhipu } from 'zhipu-ai-provider';
|
|
|
74
74
|
|
|
75
75
|
const { text } = await generateText({
|
|
76
76
|
model: zhipu('glm-4.7', {
|
|
77
|
-
thinking: {
|
|
77
|
+
thinking: {
|
|
78
|
+
type: 'enabled', // Enable deep thinking
|
|
79
|
+
clear_thinking: true // Include reasoning process in response
|
|
80
|
+
}
|
|
78
81
|
}),
|
|
79
82
|
prompt: 'Explain quantum computing in detail',
|
|
80
83
|
});
|
|
@@ -90,6 +93,58 @@ const { text: quickText } = await generateText({
|
|
|
90
93
|
|
|
91
94
|
- `thinking: { type: 'enabled' }` - Enable dynamic thinking based on task complexity (default for GLM-4.5+)
|
|
92
95
|
- `thinking: { type: 'disabled' }` - Disable thinking for faster, more direct responses
|
|
96
|
+
- `thinking: { clear_thinking: true }` - Include the reasoning process in the response (`reasoning_content` field)
|
|
97
|
+
- `thinking: { clear_thinking: false }` - Hide the reasoning process, only show final answer
|
|
98
|
+
|
|
99
|
+
### Tool Streaming (GLM-4.6/4.7)
|
|
100
|
+
|
|
101
|
+
GLM-4.7 and GLM-4.6 models support controlling tool call streaming:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
const result = await generateText({
|
|
105
|
+
model: zhipu('glm-4.7', {
|
|
106
|
+
toolStream: true // Stream tool call parameters
|
|
107
|
+
}),
|
|
108
|
+
tools: {
|
|
109
|
+
getWeather: {
|
|
110
|
+
description: 'Get weather information',
|
|
111
|
+
parameters: z.object({
|
|
112
|
+
city: z.string(),
|
|
113
|
+
}),
|
|
114
|
+
execute: async ({ city }) => `${city} Sunny 25°C`
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
prompt: 'What is the weather in Beijing today?'
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
- `toolStream: true` - Tool call parameters are streamed in chunks for faster feedback
|
|
122
|
+
- `toolStream: false` - Wait for complete tool call before returning
|
|
123
|
+
|
|
124
|
+
### Response Format
|
|
125
|
+
|
|
126
|
+
Control the output format of the model:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
// JSON mode
|
|
130
|
+
const { text } = await generateText({
|
|
131
|
+
model: zhipu('glm-4-flash'),
|
|
132
|
+
responseFormat: { type: 'json' },
|
|
133
|
+
prompt: 'List three fruits in JSON array format'
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Text mode (explicit)
|
|
137
|
+
const { text } = await generateText({
|
|
138
|
+
model: zhipu('glm-4-flash'),
|
|
139
|
+
responseFormat: { type: 'text' },
|
|
140
|
+
prompt: 'Write a poem'
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- `{ type: 'text' }` - Plain text output (default)
|
|
145
|
+
- `{ type: 'json' }` - JSON format output (text models only)
|
|
146
|
+
|
|
147
|
+
**Note:** Vision and reasoning models do not support JSON format.
|
|
93
148
|
|
|
94
149
|
## Embedding Example
|
|
95
150
|
```ts
|
|
@@ -124,6 +179,35 @@ const { image, providerMetadata } = await generateImage({
|
|
|
124
179
|
console.log(providerMetadata.zhipu.images[0].url)
|
|
125
180
|
```
|
|
126
181
|
|
|
182
|
+
## Testing
|
|
183
|
+
|
|
184
|
+
### Unit Tests
|
|
185
|
+
The project includes comprehensive unit tests that use mock data and do not require API calls:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
# Run all unit tests
|
|
189
|
+
pnpm test src
|
|
190
|
+
|
|
191
|
+
# Run specific test file
|
|
192
|
+
pnpm test src/zhipu-chat-language-model.test.ts
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Integration Tests
|
|
196
|
+
Integration tests use the real Zhipu AI API and require an API key:
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
# Set up your API key in .env file
|
|
200
|
+
echo "ZHIPU_API_KEY=your-api-key-here" > .env
|
|
201
|
+
|
|
202
|
+
# Run integration tests
|
|
203
|
+
pnpm test:node
|
|
204
|
+
|
|
205
|
+
# Run specific integration test
|
|
206
|
+
pnpm test tests/integration/chat/thinking-mode.test.ts
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**Note**: Integration tests consume API quota. See [tests/INTEGRATION_TEST_GUIDE.md](tests/INTEGRATION_TEST_GUIDE.md) for detailed information.
|
|
210
|
+
|
|
127
211
|
## Features Support
|
|
128
212
|
- [x] Text generation
|
|
129
213
|
- [x] Text embedding
|
|
@@ -141,4 +225,4 @@ console.log(providerMetadata.zhipu.images[0].url)
|
|
|
141
225
|
## Documentation
|
|
142
226
|
- **[Zhipu documentation](https://bigmodel.cn/dev/welcome)**
|
|
143
227
|
- **[Vercel AI SDK documentation](https://sdk.vercel.ai/docs/introduction)**
|
|
144
|
-
- **[Zhipu AI Provider Repo](https://github.com/
|
|
228
|
+
- **[Zhipu AI Provider Repo](https://github.com/wuzhiguocarter/zhipu-ai-provider)**
|
package/dist/index.d.mts
CHANGED
|
@@ -24,7 +24,7 @@ interface ZhipuChatSettings {
|
|
|
24
24
|
* @example
|
|
25
25
|
* ```ts
|
|
26
26
|
* const model = zhipu('glm-4.7', {
|
|
27
|
-
* thinking: { type: 'enabled' }
|
|
27
|
+
* thinking: { type: 'enabled', clear_thinking: true }
|
|
28
28
|
* })
|
|
29
29
|
* ```
|
|
30
30
|
*
|
|
@@ -32,12 +32,38 @@ interface ZhipuChatSettings {
|
|
|
32
32
|
* Only supported by GLM-4.5, GLM-4.6, and GLM-4.7 models. When enabled, the model will use
|
|
33
33
|
* chain-of-thought reasoning for complex tasks. Default is controlled by the API (usually "enabled").
|
|
34
34
|
*
|
|
35
|
-
* - "enabled"
|
|
36
|
-
* - "disabled"
|
|
35
|
+
* - `type: "enabled"`: Model uses dynamic thinking based on task complexity
|
|
36
|
+
* - `type: "disabled"`: Model responds immediately without deep reasoning
|
|
37
|
+
* - `clear_thinking: true`: Include reasoning content in the response (reasoning_content field)
|
|
38
|
+
* - `clear_thinking: false`: Hide reasoning content, only show final answer
|
|
37
39
|
*/
|
|
38
40
|
thinking?: {
|
|
39
41
|
type: "enabled" | "disabled";
|
|
42
|
+
/**
|
|
43
|
+
* Whether to include the reasoning process in the response.
|
|
44
|
+
* Only supported by GLM-4.5, GLM-4.6, and GLM-4.7 models.
|
|
45
|
+
* When true, the response includes a reasoning_content field with the model's thinking process.
|
|
46
|
+
*/
|
|
47
|
+
clear_thinking?: boolean;
|
|
40
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Controls whether tool calls use streaming.
|
|
51
|
+
* Only supported by GLM-4.7 and GLM-4.6 models.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const model = zhipu('glm-4.7', {
|
|
56
|
+
* toolStream: true
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* - `true`: Tool calls are streamed in chunks (faster feedback)
|
|
62
|
+
* - `false`: Tool calls are returned in complete form (wait for full tool call)
|
|
63
|
+
*
|
|
64
|
+
* Default behavior is controlled by the API (typically true for streaming requests).
|
|
65
|
+
*/
|
|
66
|
+
toolStream?: boolean;
|
|
41
67
|
}
|
|
42
68
|
|
|
43
69
|
type ZhipuEmbeddingModelId = "embedding-2" | "embedding-3" | (string & {});
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ interface ZhipuChatSettings {
|
|
|
24
24
|
* @example
|
|
25
25
|
* ```ts
|
|
26
26
|
* const model = zhipu('glm-4.7', {
|
|
27
|
-
* thinking: { type: 'enabled' }
|
|
27
|
+
* thinking: { type: 'enabled', clear_thinking: true }
|
|
28
28
|
* })
|
|
29
29
|
* ```
|
|
30
30
|
*
|
|
@@ -32,12 +32,38 @@ interface ZhipuChatSettings {
|
|
|
32
32
|
* Only supported by GLM-4.5, GLM-4.6, and GLM-4.7 models. When enabled, the model will use
|
|
33
33
|
* chain-of-thought reasoning for complex tasks. Default is controlled by the API (usually "enabled").
|
|
34
34
|
*
|
|
35
|
-
* - "enabled"
|
|
36
|
-
* - "disabled"
|
|
35
|
+
* - `type: "enabled"`: Model uses dynamic thinking based on task complexity
|
|
36
|
+
* - `type: "disabled"`: Model responds immediately without deep reasoning
|
|
37
|
+
* - `clear_thinking: true`: Include reasoning content in the response (reasoning_content field)
|
|
38
|
+
* - `clear_thinking: false`: Hide reasoning content, only show final answer
|
|
37
39
|
*/
|
|
38
40
|
thinking?: {
|
|
39
41
|
type: "enabled" | "disabled";
|
|
42
|
+
/**
|
|
43
|
+
* Whether to include the reasoning process in the response.
|
|
44
|
+
* Only supported by GLM-4.5, GLM-4.6, and GLM-4.7 models.
|
|
45
|
+
* When true, the response includes a reasoning_content field with the model's thinking process.
|
|
46
|
+
*/
|
|
47
|
+
clear_thinking?: boolean;
|
|
40
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Controls whether tool calls use streaming.
|
|
51
|
+
* Only supported by GLM-4.7 and GLM-4.6 models.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const model = zhipu('glm-4.7', {
|
|
56
|
+
* toolStream: true
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* - `true`: Tool calls are streamed in chunks (faster feedback)
|
|
62
|
+
* - `false`: Tool calls are returned in complete form (wait for full tool call)
|
|
63
|
+
*
|
|
64
|
+
* Default behavior is controlled by the API (typically true for streaming requests).
|
|
65
|
+
*/
|
|
66
|
+
toolStream?: boolean;
|
|
41
67
|
}
|
|
42
68
|
|
|
43
69
|
type ZhipuEmbeddingModelId = "embedding-2" | "embedding-3" | (string & {});
|
package/dist/index.js
CHANGED
|
@@ -13986,7 +13986,7 @@ var ZhipuChatLanguageModel = class {
|
|
|
13986
13986
|
tools,
|
|
13987
13987
|
toolChoice
|
|
13988
13988
|
}) {
|
|
13989
|
-
var _a3;
|
|
13989
|
+
var _a3, _b;
|
|
13990
13990
|
const warnings = [];
|
|
13991
13991
|
if (!this.config.isMultiModel && prompt.every(
|
|
13992
13992
|
(msg) => msg.role === "user" && !msg.content.every((part) => part.type === "text")
|
|
@@ -14062,6 +14062,20 @@ var ZhipuChatLanguageModel = class {
|
|
|
14062
14062
|
details: "JSON response format schema is only supported with structuredOutputs, provide the schema."
|
|
14063
14063
|
});
|
|
14064
14064
|
}
|
|
14065
|
+
if (((_a3 = this.settings.thinking) == null ? void 0 : _a3.clear_thinking) !== void 0 && !this.modelId.match(/^(glm-4\.[567]|glm-4\.5v)$/)) {
|
|
14066
|
+
warnings.push({
|
|
14067
|
+
type: "unsupported-setting",
|
|
14068
|
+
setting: "thinking.clear_thinking",
|
|
14069
|
+
details: "clear_thinking is only supported by GLM-4.5, GLM-4.6, and GLM-4.7 models."
|
|
14070
|
+
});
|
|
14071
|
+
}
|
|
14072
|
+
if (this.settings.toolStream !== void 0 && !this.modelId.match(/^glm-4\.[67]/)) {
|
|
14073
|
+
warnings.push({
|
|
14074
|
+
type: "unsupported-setting",
|
|
14075
|
+
setting: "toolStream",
|
|
14076
|
+
details: "tool_stream is only supported by GLM-4.7 and GLM-4.6 models."
|
|
14077
|
+
});
|
|
14078
|
+
}
|
|
14065
14079
|
const baseArgs = {
|
|
14066
14080
|
// model id:
|
|
14067
14081
|
model: this.modelId,
|
|
@@ -14070,19 +14084,24 @@ var ZhipuChatLanguageModel = class {
|
|
|
14070
14084
|
do_sample: this.settings.doSample,
|
|
14071
14085
|
request_id: this.settings.requestId,
|
|
14072
14086
|
thinking: this.settings.thinking ? {
|
|
14073
|
-
type: this.settings.thinking.type
|
|
14087
|
+
type: this.settings.thinking.type,
|
|
14088
|
+
...this.settings.thinking.clear_thinking !== void 0 && {
|
|
14089
|
+
clear_thinking: this.settings.thinking.clear_thinking
|
|
14090
|
+
}
|
|
14074
14091
|
} : void 0,
|
|
14075
14092
|
// standardized settings:
|
|
14076
14093
|
max_tokens: maxOutputTokens,
|
|
14077
14094
|
temperature,
|
|
14078
14095
|
top_p: topP,
|
|
14079
14096
|
// response format:
|
|
14080
|
-
response_format:
|
|
14097
|
+
response_format: responseFormat ? {
|
|
14098
|
+
type: responseFormat.type === "json" ? "json_object" : "text"
|
|
14099
|
+
} : void 0,
|
|
14081
14100
|
// messages:
|
|
14082
14101
|
messages: convertToZhipuChatMessages(prompt),
|
|
14083
14102
|
// tools:
|
|
14084
14103
|
tool_choice: toolChoice != null ? toolChoice : "auto",
|
|
14085
|
-
tools: (
|
|
14104
|
+
tools: (_b = tools == null ? void 0 : tools.filter((tool) => tool.type === "function").map((tool) => {
|
|
14086
14105
|
var _a4;
|
|
14087
14106
|
return {
|
|
14088
14107
|
type: "function",
|
|
@@ -14092,8 +14111,10 @@ var ZhipuChatLanguageModel = class {
|
|
|
14092
14111
|
parameters: tool.inputSchema
|
|
14093
14112
|
}
|
|
14094
14113
|
};
|
|
14095
|
-
})) != null ?
|
|
14114
|
+
})) != null ? _b : void 0,
|
|
14096
14115
|
// TODO: add provider-specific tool (web_search|retrieval)
|
|
14116
|
+
// tool streaming:
|
|
14117
|
+
tool_stream: this.settings.toolStream
|
|
14097
14118
|
};
|
|
14098
14119
|
return {
|
|
14099
14120
|
args: baseArgs,
|