代码示例
中文exp-2822未验证
cURL Python SDK 基础调用(启用深度思考) curl --location 'https://open.bigmodel.cn/api/paas/v4/chat/completions' \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "model": "glm-5.3", "messages": [ { "role": "user", "content": "详细解释量子计算的基本原理,并分析其在密码学领域的潜在影响" } ], "thinking": { "type": "enabled" }, "max_tokens": 4096, "temperature": 1.0 }' 流式调用(深度思考 + 流式输出) curl --location 'https://open.bigmodel.cn/api/paas/v4/chat/completions' \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "model": "glm-5.3", "messages": [ { "role": "user", "content": "设计一个电商网站的推荐系统架构,考虑用户行为、商品特征和实时性要求" } ], "thinking": { "type": "enabled" }, "stream": true, "max_tokens": 4096, "temperature": 1.0 }' 控制推理程度(reasoning_effort) curl --location 'https://open.bigmodel.cn/api/paas/v4/chat/completions' \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "model": "glm-5.3", "messages": [ { "role": "user", "content": "分析一下这道数学题的解题思路" } ], "thinking": { "type": "enabled" }, "reasoning_effort": "max" }' 禁用深度思考(GLM-5.3 已不再支持,仅 GLM-5.2 支持) curl --location 'https://open.bigmodel.cn/api/paas/v4/chat/completions' \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "model": "glm-5.2", "messages": [ { "role": "user", "content": "今天天气怎么样?" } ], "thinking": { "type": "disabled" } }' 安装 SDK # 安装最新版本 pip install zai-sdk # 或指定版本 pip install zai-sdk== 0.2.3 验证安装 import zai print (zai. __version__ ) 基础调用(启用深度思考) from zai import ZhipuAiClient # 初始化客户端 client = ZhipuAiClient( api_key = 'YOUR_API_KEY' ) # 创建深度思考请求 response = client.chat.completions.create( model = "glm-5.3" , messages = [ { "role" : "user" , "content" : "详细解释量子计算的基本原理,并分析其在密码学领域的潜在影响" } ], thinking = { "type" : "enabled" # 启用深度思考模式 }, max_tokens = 4096 , temperature = 1.0 ) print ( "模型响应:" ) print (response.choices[ 0 ].message.content) print ( " \n ---" ) print (response.choices[ 0 ].message.reasoning_content) 流式调用(深度思考 + 流式输出) from zai import ZhipuAiClient # 初始化客户端 client = ZhipuAiClient( api_key = 'YOUR_API_KEY' ) # 创建流式深度思考请求 response = client.chat.completions.create( model = "glm-5.3" , messages = [ { "role" : "user" , "content" : "设计一个电商网站的推荐系统架构,考虑用户行为、商品特征和实时性要求" } ], thinking = { "type" : "enabled" # 启用深度思考模式 }, stream = True , # 启用流式输出 max_tokens = 4096 , temperature = 1.0 ) # 处理流式响应 reasoning_content = "" thinking_phase = True for chunk in response: if not chunk.choices: continue delta = chunk.choices[ 0 ].delta # 处理思考过程(如果有) if hasattr (delta, 'reasoning_content' ) and delta.reasoning_content: reasoning_content += delta.reasoning_content if thinking_phase: print ( "🧠 思考中..." , end = "" , flush = True ) thinking_phase = False print (delta.reasoning_content, end = "" , flush = True ) # 处理回答内容 if hasattr (delta, 'content' ) and delta.content: if thinking_phase: print ( " \n\n 💡 回答:" ) thinking_phase = False print (delta.content, end = "" , flush = True ) 控制推理程度(reasoning_effort) from zai import ZhipuAiClient # 初始化客户端 client = ZhipuAiClient( api_key = 'YOUR_API_KEY' ) # 使用 reasoning_effort 控制推理程度 response = client.chat.completions.create( model = "glm-5.3" , messages = [ { "role" : "user" , "content" : "分析一下这道数学题的解题思路" } ], thinking = { "type" : "enabled" }, reasoning_effort = "high" # GLM-5.3 仅支持: max, high, low;GLM-5.2 另支持 xhigh, medium, low, minimal, none 并自动映射 ) print (response.choices[ 0 ].message.content) print (response.choices[ 0 ].message.reasoning_content) 禁用深度思考 from zai import ZhipuAiClient # 初始化客户端 client = ZhipuAiClient( api_key = 'YOUR_API_KEY' ) # 禁用深度思考,快速响应 response = client.chat.completions.create( model = "glm-5.2" , messages = [ { "role" : "user" , "content" : "今天天气怎么样?" } ], thinking = { "type" : "disabled" # 禁用深度思考模式 } ) print (response.choices[ 0 ].message.content)
响应示例
启用深度思考的响应格式:
{ "created" : 1677652288 , "model" : "glm-5.3" , "choices" : [ { "index" : 0 , "message" : { "role" : "assistant" , "content" : "人工智能在医疗诊断中具有巨大的应用前景..." , "reasoning_content" : "让我从多个角度来分析这个问题。首先,我需要考虑AI在医疗诊断中的技术优势..." }, "finish_reason" : "stop" } ], "usage" : { "completion_tokens" : 239 , "prompt_tokens" : 8 , "prompt_tokens_details" : { "cached_tokens" : 0 }, "total_tokens" : 247 } }
复制后粘贴到 AI 对话框发送,本次对话全程生效
出处:智谱 GLM 官方提示词教程 · docs.bigmodel.cn/cn/guide/capabilities/thinking · 可信度 S · 标签:中文 / 提示词工程 / 少样本 怎么用(对话型)复制后粘贴到对话框发送,本次对话全程生效。想长期用:支持自定义指令的产品(设置→个性化)可设为默认。