Deep Thinking is an advanced reasoning feature that enables Chain of Thought mechanisms, allowing the model to perform deep analysis and reasoning before answering questions. This approach significantly improves the model’s accuracy and interpretability in complex tasks, particularly suitable for scenarios requiring multi-step reasoning, logical analysis, and problem-solving.
from zai import ZaiClient# Initialize clientclient = ZaiClient(api_key='your_api_key')# Create deep thinking requestresponse = client.chat.completions.create( model="glm-4.6", messages=[ {"role": "user", "content": "Explain in detail the basic principles of quantum computing and analyze its potential impact in the field of cryptography"} ], thinking={ "type": "enabled" # Enable deep thinking mode }, max_tokens=4096, temperature=1.0)print("Model response:")print(response.choices[0].message.content)print("\n---")print(response.choices[0].message.reasoning_content)
Streaming Call (Deep Thinking + Streaming Output)
Copy
Ask AI
from zai import ZaiClient# Initialize clientclient = ZaiClient(api_key='your_api_key')# Create streaming deep thinking requestresponse = client.chat.completions.create( model="glm-4.6", messages=[ {"role": "user", "content": "Design a recommendation system architecture for an e-commerce website, considering user behavior, product features, and real-time requirements"} ], thinking={ "type": "enabled" # Enable deep thinking mode }, stream=True, # Enable streaming output max_tokens=4096, temperature=1.0)# Process streaming responsereasoning_content = ""thinking_phase = Truefor chunk in response: if not chunk.choices: continue delta = chunk.choices[0].delta # Process thinking process (if any) if hasattr(delta, 'reasoning_content') and delta.reasoning_content: reasoning_content += delta.reasoning_content if thinking_phase: print("🧠Thinking...", end="", flush=True) thinking_phase = False print(delta.reasoning_content, end="", flush=True) # Process answer content if hasattr(delta, 'content') and delta.content: if thinking_phase: print("\n\n💡 Answer:") thinking_phase = False print(delta.content, end="", flush=True)
Disable Deep Thinking
Copy
Ask AI
from zai import ZaiClient# Initialize clientclient = ZaiClient(api_key='your_api_key')# Disable deep thinking for quick responseresponse = client.chat.completions.create( model="glm-4.6", messages=[ {"role": "user", "content": "How is the weather today?"} ], thinking={ "type": "disabled" # Disable deep thinking mode })print(response.choices[0].message.content)
{ "created": 1677652288, "model": "glm-4.6", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "Artificial intelligence has tremendous application prospects in medical diagnosis...", "reasoning_content": "Let me analyze this question from multiple angles. First, I need to consider the technical advantages of AI in medical diagnosis..." }, "finish_reason": "stop" } ], "usage": { "completion_tokens": 239, "prompt_tokens": 8, "prompt_tokens_details": { "cached_tokens": 0 }, "total_tokens": 247 }}