Este tópico descreve como usar APIs para chamar modelos da série GLM na plataforma Alibaba Cloud Model Studio. Cada modelo inclui 1 milhão de tokens gratuitos.
Cada modelo inclui 1 milhão de tokens gratuitos.
ImportanteOs modelos glm-4,6 e glm-4,7 serão descontinuados em 9 de julho de 2026. Recomendamos a migração para: qwen3.7-plus, qwen3.8-max e qwen3.8-flash.
Endpoints de service
Os endpoints de service variam conforme a região. Configure a URL base correspondente à região selecionada.
OpenAI compatible
China (Beijing)
A base_url para chamadas via SDK é: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions
US (Virginia)
A base_url para chamadas via SDK é: https://dashscope-us.aliyuncs.com/compatible-mode/v1
Endpoint de solicitação HTTP: POST https://dashscope-us.aliyuncs.com/compatible-mode/v1/chat/completions
Germany (Frankfurt)
A base_url para chamadas via SDK é: https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
China (Hong Kong)
A base_url para chamadas via SDK é: https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/chat/completions
Singapore
Corpo da aba
A base_url para chamadas via SDK é: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions
DashScope
China (Beijing)
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation
A base_url para chamadas via SDK é:
Python
dashscope.base_http_api_url = 'https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1'
Java
- Método 1:
import com.alibaba.dashscope.protocol.Protocol;
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1");
- Método 2:
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";
US (Virginia)
Endpoint de solicitação HTTP: POST https://dashscope-us.aliyuncs.com/api/v1/services/aigc/text-generation/generation
A base_url para chamadas via SDK é:
Python
dashscope.base_http_api_url = 'https://dashscope-us.aliyuncs.com/api/v1'
Java
- Método 1:
import com.alibaba.dashscope.protocol.Protocol;
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-us.aliyuncs.com/api/v1");
- Método 2:
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl="https://dashscope-us.aliyuncs.com/api/v1";
Germany (Frankfurt)
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation
A base_url para chamadas via SDK é:
Python
dashscope.base_http_api_url = 'https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/api/v1'
Java
- Método 1:
import com.alibaba.dashscope.protocol.Protocol;
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/api/v1");
- Método 2:
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl="https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/api/v1";
China (Hong Kong)
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation
A base_url para chamadas via SDK é:
Python
dashscope.base_http_api_url = 'https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/api/v1'
Java
- Método 1:
import com.alibaba.dashscope.protocol.Protocol;
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/api/v1");
- Método 2:
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl="https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/api/v1";
Singapore
Conteúdo da aba
Endpoint de solicitação HTTP: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation
A base_url para chamadas via SDK é: dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"
Substitua {WorkspaceId} pelo seu workspace ID real.
Primeiros passos
Os modelos glm-5,2 e glm-5,2-fast-preview são os mais recentes da série GLM. Eles possuem comprimento de contexto de 1M e permitem definir o modo de raciocínio e o modo sem raciocínio por meio do parâmetro enable_thinking. Execute o código abaixo para chamar rapidamente o modelo glm-5,2 no modo de raciocínio.
Antes de começar, obtenha uma chave de API e configure-a como variável de ambiente. Se você utilizar um SDK, também será necessário instale o SDK da OpenAI ou do DashScope.
OpenAI compatible
ObservaçãoO parâmetro enable_thinking não é um parâmetro padrão da OpenAI. No SDK Python da OpenAI, ele é transmitido via extra_body. No SDK Node.js, ele é passado como um parâmetro de nível superior.
Python
Código de exemplo
from openai import OpenAI
import os
# Initialize the OpenAI client
client = OpenAI(
# If the environment variable is not configured, replace the value with your Model Studio API key: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
# When you make a call, replace {WorkspaceId} with your actual Workspace ID.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
messages = [{"role": "user", "content": "Who are you?"}]
completion = client.chat.completions.create(
model="glm-5.2",
messages=messages,
# Set enable_thinking in extra_body to enable the thinking mode
extra_body={"enable_thinking": True},
stream=True,
stream_options={
"include_usage": True
},
)
reasoning_content = "" # Complete thinking process
answer_content = "" # Complete response
is_answering = False # Indicates whether the response phase has started
print("\n" + "=" * 20 + "Thinking process" + "=" * 20 + "\n")
for chunk in completion:
if not chunk.choices:
print("\n" + "=" * 20 + "Token usage" + "=" * 20 + "\n")
print(chunk.usage)
continue
delta = chunk.choices[0].delta
# Collect only the thinking content
if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
if not is_answering:
print(delta.reasoning_content, end="", flush=True)
reasoning_content += delta.reasoning_content
# After content is received, start the response
if hasattr(delta, "content") and delta.content:
if not is_answering:
print("\n" + "=" * 20 + "Complete response" + "=" * 20 + "\n")
is_answering = True
print(delta.content, end="", flush=True)
answer_content += delta.content
Resposta
====================Thinking process====================
Let me carefully consider the user's question. It seems simple, but it actually has depth.
From a linguistic perspective, the user is using Chinese, which means I should respond in Chinese. This is a basic self-introduction question, but it may have multiple layers of meaning.
First, I need to be clear that as a language model, I should honestly state my identity and nature. I am not a human, nor do I have real emotional awareness. I am an AI assistant trained with deep learning technology. This is the most basic fact.
Second, considering the user's potential scenarios, they might want to know:
1. What services can I provide?
2. What are my areas of expertise?
3. What are my limitations?
4. How can they interact with me better?
In my answer, I should express a friendly and open attitude while remaining professional and accurate. I should state my main areas of expertise, such as knowledge Q&A, writing assistance, and creative support, but also frankly point out my limitations, such as the lack of real emotional experience.
In addition, to make the answer more complete, I should also express a positive attitude of being willing to help users solve problems. I can appropriately guide users to ask more specific questions to better demonstrate my abilities.
Considering this is an open-ended opening, the answer should be both concise and informative, giving the user a clear understanding of my basic situation and laying a good foundation for subsequent conversations.
Finally, the tone should be humble and professional, neither too technical nor too casual, to make the user feel comfortable and natural.
====================Complete response====================
I am a GLM large language model trained by Zhipu AI, designed to provide users with information and help solve problems. I am designed to understand and generate human language, and I can answer questions, provide explanations, or participate in discussions on various topics.
I do not store your personal data, and our conversations are anonymous. Is there any topic I can help you understand or discuss?
====================Token usage====================
CompletionUsage(completion_tokens=344, prompt_tokens=7, total_tokens=351, completion_tokens_details=None, prompt_tokens_details=None)
Node.js
Código de exemplo
import OpenAI from "openai";
import process from 'process';
// Initialize the OpenAI client
const openai = new OpenAI({
// If the environment variable is not configured, replace the value with your Model Studio API key: apiKey: "sk-xxx"
apiKey: process.env.DASHSCOPE_API_KEY,
// When you make a call, replace {WorkspaceId} with your actual Workspace ID.
baseURL: 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1'
});
let reasoningContent = ''; // Complete thinking process
let answerContent = ''; // Complete response
let isAnswering = false; // Indicates whether the response phase has started
async function main() {
try {
const messages = [{ role: 'user', content: 'Who are you?' }];
const stream = await openai.chat.completions.create({
model: 'glm-5.2',
messages,
// Note: In the Node.js SDK, non-standard parameters such as enable_thinking are passed as top-level properties and do not need to be placed in extra_body.
enable_thinking: true,
stream: true,
stream_options: {
include_usage: true
},
});
console.log('\n' + '='.repeat(20) + 'Thinking process' + '='.repeat(20) + '\n');
for await (const chunk of stream) {
if (!chunk.choices?.length) {
console.log('\n' + '='.repeat(20) + 'Token usage' + '='.repeat(20) + '\n');
console.log(chunk.usage);
continue;
}
const delta = chunk.choices[0].delta;
// Collect only the thinking content
if (delta.reasoning_content !== undefined && delta.reasoning_content !== null) {
if (!isAnswering) {
process.stdout.write(delta.reasoning_content);
}
reasoningContent += delta.reasoning_content;
}
// After content is received, start the response
if (delta.content !== undefined && delta.content) {
if (!isAnswering) {
console.log('\n' + '='.repeat(20) + 'Complete response' + '='.repeat(20) + '\n');
isAnswering = true;
}
process.stdout.write(delta.content);
answerContent += delta.content;
}
}
} catch (error) {
console.error('Error:', error);
}
}
main();
Resposta
====================Thinking process====================
Let me carefully think about the user's question, "Who are you?". This needs to be analyzed and answered from multiple perspectives.
First, this is a basic identity question. As a GLM large language model, I need to accurately express my identity. I should clearly state that I am an AI assistant developed by Zhipu AI.
Second, I need to consider the user's possible intentions for asking this question. They might be first-time users who want to understand my basic functions, or they might want to confirm if I can provide specific help, or they might just be testing my response style. Therefore, I need to give an open and friendly answer.
I also need to consider the completeness of the answer. In addition to introducing my identity, I should also briefly explain my main functions, such as Q&A, creation, and analysis, so that users can understand how to use this assistant.
Finally, I need to ensure a friendly and approachable tone and express my willingness to help. I can use expressions like "I'm happy to serve you" to make the user feel the warmth of the communication.
Based on these thoughts, I can organize a concise and clear answer that not only answers the user's question but also guides subsequent communication.
====================Complete response====================
I am GLM, a large language model trained by Zhipu AI. I am trained on a massive amount of text data to understand and generate human language, helping users answer questions, provide information, and engage in conversations.
I will continue to learn and improve to provide better services. I am happy to answer your questions or provide help! What can I do for you?
====================Token usage====================
{ prompt_tokens: 7, completion_tokens: 248, total_tokens: 255 }
HTTP
Código de exemplo
curl
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.2",
"messages": [
{
"role": "user",
"content": "Who are you?"
}
],
"stream": true,
"stream_options": {
"include_usage": true
},
"enable_thinking": true
}'
DashScope
Python
Código de exemplo
import os
from dashscope import Generation
# Initialize request parameters
messages = [{"role": "user", "content": "Who are you?"}]
completion = Generation.call(
# If the environment variable is not configured, replace the value with your Model Studio API key: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="glm-5.2",
messages=messages,
result_format="message", # Set the result format to message
enable_thinking=True, # Enable the thinking mode
stream=True, # Enable streaming output
incremental_output=True, # Enable incremental output
)
reasoning_content = "" # Complete thinking process
answer_content = "" # Complete response
is_answering = False # Indicates whether the response phase has started
print("\n" + "=" * 20 + "Thinking process" + "=" * 20 + "\n")
for chunk in completion:
message = chunk.output.choices[0].message
# Collect only the thinking content
if "reasoning_content" in message:
if not is_answering:
print(message.reasoning_content, end="", flush=True)
reasoning_content += message.reasoning_content
# After content is received, start the response
if message.content:
if not is_answering:
print("\n" + "=" * 20 + "Complete response" + "=" * 20 + "\n")
is_answering = True
print(message.content, end="", flush=True)
answer_content += message.content
print("\n" + "=" * 20 + "Token usage" + "=" * 20 + "\n")
print(chunk.usage)
Resposta
====================Thinking process====================
Let me carefully consider the user's question, "Who are you?". First, I need to analyze the user's intent. This could be curiosity from a first-time user, or they might want to understand my specific functions and capabilities.
From a professional perspective, I should clearly state my identity - as a GLM large language model, I need to explain my basic positioning and main functions. I should avoid overly technical descriptions and use easy-to-understand language.
At the same time, I should also consider some practical issues that users may be concerned about, such as privacy protection and data security. These are points that users are very concerned about when using AI services.
In addition, to show professionalism and friendliness, I can take the initiative to guide the conversation after the introduction and ask if the user needs specific help. This will not only help the user understand me better but also pave the way for subsequent conversations.
Finally, I need to ensure that the answer is concise and clear, with the key points highlighted, so that the user can quickly understand my identity and purpose. Such an answer can both satisfy the user's curiosity and demonstrate professionalism and a service-oriented attitude.
====================Complete response====================
I am a GLM large language model developed by Zhipu AI, designed to provide information and help to users through natural language processing technology. I am trained on a massive amount of text data and can understand and generate human language, answer questions, provide knowledge support, and participate in conversations.
My design goal is to be a useful AI assistant while ensuring user privacy and data security. I do not store users' personal information and will continue to learn and improve to provide better services.
Is there any question I can answer or any task I can assist you with?
====================Token usage====================
{"input_tokens": 8, "output_tokens": 269, "total_tokens": 277}
Java
Código de exemplo
ImportanteA versão do SDK Java do DashScope deve ser 2.19.4 ou posterior.
// The version of the DashScope SDK must be 2.19.4 or later.
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.reactivex.Flowable;
import java.lang.System;
import java.util.Arrays;
public class Main {
private static StringBuilder reasoningContent = new StringBuilder();
private static StringBuilder finalContent = new StringBuilder();
private static boolean isFirstPrint = true;
private static void handleGenerationResult(GenerationResult message) {
String reasoning = message.getOutput().getChoices().get(0).getMessage().getReasoningContent();
String content = message.getOutput().getChoices().get(0).getMessage().getContent();
if (reasoning != null && !reasoning.isEmpty()) {
reasoningContent.append(reasoning);
if (isFirstPrint) {
System.out.println("====================Thinking process====================");
isFirstPrint = false;
}
System.out.print(reasoning);
}
if (content != null && !content.isEmpty()) {
finalContent.append(content);
if (!isFirstPrint) {
System.out.println("\n====================Complete response====================");
isFirstPrint = true;
}
System.out.print(content);
}
}
private static GenerationParam buildGenerationParam(Message userMsg) {
return GenerationParam.builder()
// If the environment variable is not configured, replace the following line with: .apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("glm-5.2")
.incrementalOutput(true)
.resultFormat("message")
.messages(Arrays.asList(userMsg))
.build();
}
public static void streamCallWithMessage(Generation gen, Message userMsg)
throws NoApiKeyException, ApiException, InputRequiredException {
GenerationParam param = buildGenerationParam(userMsg);
Flowable<GenerationResult> result = gen.streamCall(param);
result.blockingForEach(message -> handleGenerationResult(message));
}
public static void main(String[] args) {
try {
Generation gen = new Generation();
Message userMsg = Message.builder().role(Role.USER.getValue()).content("Who are you?").build();
streamCallWithMessage(gen, userMsg);
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("An exception occurred: " + e.getMessage());
}
}
}
Resposta
====================Thinking process====================
Let me think about how to answer the user's question. First, this is a simple question about identity that needs a clear and direct answer.
As a large language model, I should accurately state my basic identity information. This includes:
- Name: GLM
- Developer: Zhipu AI
- Main functions: Language understanding and generation
Considering that the user's question may stem from a first-time interaction, I need to introduce myself in an easy-to-understand way, avoiding overly technical terms. At the same time, I should also briefly explain my main capabilities to help the user better understand how to interact with me.
I should also express a friendly and open attitude, welcoming users to ask various questions, which can lay a good foundation for subsequent conversations. However, the introduction should be concise and not overly detailed, to avoid burdening the user with too much information.
Finally, to promote further communication, I can proactively ask if the user needs specific help, which can better serve the user's actual needs.
====================Complete response====================
I am GLM, a large language model developed by Zhipu AI. I am trained on a massive amount of text data and can understand and generate human language, answer questions, provide information, and engage in conversations.
My design purpose is to help users solve problems, provide knowledge, and support various language tasks. I will continuously learn and update to provide more accurate and useful answers.
Is there any question I can answer or discuss with you?
HTTP
Código de exemplo
curl
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
"model": "glm-5.2",
"input":{
"messages":[
{
"role": "user",
"content": "Who are you?"
}
]
},
"parameters":{
"enable_thinking": true,
"incremental_output": true,
"result_format": "message"
}
}'
Anthropic compatible
Python
Código de exemplo
import anthropic
import os
client = anthropic.Anthropic(
# If the environment variable is not configured, replace the value with your Model Studio API key: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
# When you make a call, replace {WorkspaceId} with your actual Workspace ID.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic",
)
message = client.messages.create(
model="glm-5.2",
max_tokens=1024,
messages=[
{"role": "user", "content": "Who are you?"}
],
stream=True,
)
for event in message:
if event.type == "content_block_delta":
if hasattr(event.delta, "thinking"):
print(event.delta.thinking, end="", flush=True)
if hasattr(event.delta, "text"):
print(event.delta.text, end="", flush=True)
HTTP
Código de exemplo
curl
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/apps/anthropic/v1/messages \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "glm-5.2",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Who are you?"
}
]
}'
Chamada de ferramentas em streaming
Os modelos glm-5,2, glm-5,2-fast-preview, glm-5,1, glm-5, glm-4,7 e glm-4,6 suportam o parâmetro tool_stream. Este parâmetro é booleano, tem como valor padrão false e só entra em vigor quando stream está definido como true. Quando ativado, os argumentos do parâmetro tool_call do Function Calling são retornados incrementalmente em fluxo, em vez de serem entregues todos de uma vez após a conclusão da geração.
O comportamento combinado de stream e tool_stream é o seguinte:
stream | tool_stream | Método de retorno de tool_call |
|---|---|---|
true | true | Os argumentos são retornados incrementalmente em múltiplos chunks. |
true | false (padrão) | Os argumentos são retornados completamente em um único chunk. |
false | true/false | tool_stream não tem efeito. Os argumentos são retornados de uma vez na resposta completa. |
OpenAI compatible
Python
Código de exemplo
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather information for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "The name of the city"}
},
"required": ["city"]
}
}
}
]
messages = [{"role": "user", "content": "What's the weather like in Beijing?"}]
completion = client.chat.completions.create(
model="glm-5.2",
tools=tools,
messages=messages,
extra_body={
"tool_stream": True,
},
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
delta = chunk.choices[0].delta
if hasattr(delta, 'content') and delta.content:
print(f"[content] {delta.content}")
if hasattr(delta, 'tool_calls') and delta.tool_calls:
for tc in delta.tool_calls:
print(f"[tool_call] id={tc.id}, name={tc.function.name}, args={tc.function.arguments}")
if chunk.choices[0].finish_reason:
print(f"[finish_reason] {chunk.choices[0].finish_reason}")
if not chunk.choices and chunk.usage:
print(f"[usage] {chunk.usage}")
Node.js
Código de exemplo
import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
// Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
baseURL: 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1'
});
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Get the weather information for a specified city",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "The name of the city" }
},
required: ["city"]
}
}
}
];
async function main() {
try {
const stream = await openai.chat.completions.create({
model: 'glm-5.2',
messages: [{ role: 'user', content: "What's the weather like in Beijing?" }],
tools: tools,
tool_stream: true,
stream: true,
stream_options: {
include_usage: true
},
});
for await (const chunk of stream) {
if (!chunk.choices?.length) {
if (chunk.usage) {
console.log(`[usage] ${JSON.stringify(chunk.usage)}`);
}
continue;
}
const delta = chunk.choices[0].delta;
if (delta.content) {
console.log(`[content] ${delta.content}`);
}
if (delta.tool_calls) {
for (const tc of delta.tool_calls) {
console.log(`[tool_call] id=${tc.id}, name=${tc.function.name}, args=${tc.function.arguments}`);
}
}
if (chunk.choices[0].finish_reason) {
console.log(`[finish_reason] ${chunk.choices[0].finish_reason}`);
}
}
} catch (error) {
console.error('Error:', error);
}
}
main();
HTTP
Código de exemplo
curl
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.2",
"messages": [
{
"role": "user",
"content": "What's the weather like in Beijing?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather information for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "The name of the city"}
},
"required": ["city"]
}
}
}
],
"stream": true,
"stream_options": {"include_usage": true},
"tool_stream": true
}'
DashScope
Python
Código de exemplo
import os
from dashscope import Generation
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather information for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "The name of the city"}
},
"required": ["city"]
}
}
}
]
messages = [{"role": "user", "content": "What's the weather like in Beijing?"}]
completion = Generation.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="glm-5.2",
messages=messages,
tools=tools,
result_format="message",
stream=True,
tool_stream=True,
incremental_output=True,
)
for chunk in completion:
msg = chunk.output.choices[0].message
if msg.content:
print(f"[content] {msg.content}")
if "tool_calls" in msg and msg.tool_calls:
for tc in msg.tool_calls:
fn = tc.get("function", {})
print(f"[tool_call] id={tc.get('id','')}, name={fn.get('name','')}, args={fn.get('arguments','')}")
finish = chunk.output.choices[0].get("finish_reason", "")
if finish and finish != "null":
print(f"[finish_reason] {finish}")
HTTP
Código de exemplo
curl
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-H "X-DashScope-SSE: enable" \
-d '{
"model": "glm-5.2",
"input": {
"messages": [
{
"role": "user",
"content": "What's the weather like in Beijing?"
}
]
},
"parameters": {
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather information for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "The name of the city"}
},
"required": ["city"]
}
}
}
],
"tool_stream": true,
"incremental_output": true,
"result_format": "message"
}
}'
Esforço de raciocínio (reasoning_effort)
Os modelos glm-5,2, glm-5,2-fast-preview e glm-5,1 têm o modo de raciocínio ativado por padrão. O modelo primeiro gera o processo de raciocínio (reasoning_content) e depois fornece a resposta final. Use o parâmetro reasoning_effort para ajustar o esforço de raciocínio. Um valor mais alto indica um raciocínio mais aprofundado. Os valores suportados variam conforme o modelo. Se você passar um valor não suportado, o sistema retornará um erro invalid_parameter_error. Selecione um valor na tabela a seguir.
Modelo | Valores disponíveis para reasoning_effort |
|---|---|
glm-5.2 |
|
glm-5.2-fast-preview |
|
glm-5.1 |
|
ObservaçãoPara desativar o modo de raciocínio, defina o parâmetro enable_thinking como false no modo compatível com OpenAI ou DashScope. Este parâmetro tem prioridade sobre reasoning_effort.
ObservaçãoO modo compatível com Anthropic não suporta o parâmetro reasoning_effort. Para obter o conteúdo de raciocínio, utilize o parâmetro nativo thinking da Anthropic: {"thinking":{"type":"enabled","budget_tokens":1024}}. Quando ativado, o content da resposta incluirá um bloco de raciocínio com type definido como thinking.
OpenAI compatible
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="glm-5.2",
messages=[{"role": "user", "content": "Which is larger, 9.9 or 9.11?"}],
reasoning_effort="high",
)
print(completion.choices[0].message.content)
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.DASHSCOPE_API_KEY,
// Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
});
const completion = await openai.chat.completions.create({
model: "glm-5.2",
messages: [{ role: "user", content: "Which is larger, 9.9 or 9.11?" }],
reasoning_effort: "high",
});
console.log(completion.choices[0].message.content);
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.2",
"messages": [{"role": "user", "content": "Which is larger, 9.9 or 9.11?"}],
"reasoning_effort": "high"
}'
DashScope
import os
from dashscope import Generation
response = Generation.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="glm-5.2",
messages=[{"role": "user", "content": "Which is larger, 9.9 or 9.11?"}],
reasoning_effort="high",
result_format="message",
)
print(response.output.choices[0].message.content)
Limpar histórico de raciocínio (clear_thinking)
O parâmetro clear_thinking controla se o reasoning_content (processo de raciocínio) de turnos anteriores em uma conversa de múltiplos turnos é passado ao modelo como contexto. Apenas os modelos da série GLM suportam este parâmetro.
true: Ignora oreasoning_contentde turnos anteriores. Somente conteúdos não relacionados ao raciocínio, como texto visível, chamadas de ferramentas e resultados, são usados como entrada de contexto. Isso reduz o comprimento do contexto e o custo.false(padrão): Mantém oreasoning_contentde turnos anteriores e o fornece ao modelo junto com o contexto. Para ative o Raciocínio Preservado, passe oreasoning_contenthistórico de forma completa, sem modificações e na ordem original dentro das mensagens. A ausência, corte, reescrita ou reordenação do conteúdo pode degradar o desempenho ou causar falha no recurso.
ObservaçãoEste parâmetro afeta apenas o conteúdo histórico de raciocínio entre turnos e não altera se o modelo gera ou produz conteúdo de raciocínio no turno atual.
O exemplo a seguir utiliza o mesmo conjunto de mensagens de múltiplos turnos, onde a mensagem do assistant contém reasoning_content. Quando clear_thinking está definido como true, o conteúdo histórico de raciocínio não é incluído no contexto. Consequentemente, a contagem de prompt_tokens é menor do que quando definido como false (padrão). O valor real depende do comprimento do reasoning_content histórico.
OpenAI compatible
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# When you make a call, replace {WorkspaceId} with your actual Workspace ID.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# Multi-turn conversation where the assistant message carries reasoning_content (historical thinking process)
messages = [
{"role": "user", "content": "Please calculate 15 * 23."},
{"role": "assistant", "content": "15 multiplied by 23 equals 345.", "reasoning_content": "15 * 23 = 345"},
{"role": "user", "content": "What if you add 55 to that?"},
{"role": "assistant", "content": "345 plus 55 equals 400.", "reasoning_content": "345 + 55 = 400"},
{"role": "user", "content": "What was the intermediate result?"},
]
completion = client.chat.completions.create(
model="glm-5.2",
messages=messages,
extra_body={
"enable_thinking": True,
# true: Ignores historical reasoning_content to reduce context length and cost
# false (default): Retains historical reasoning_content (Preserved Thinking)
"clear_thinking": True,
},
)
print(completion.usage.prompt_tokens) # The value is smaller when set to true than when set to false
# Singapore region. Replace {WorkspaceId} with your Bailian workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.2",
"messages": [
{"role": "user", "content": "Please calculate 15 * 23."},
{"role": "assistant", "content": "15 multiplied by 23 equals 345.", "reasoning_content": "15 * 23 = 345"},
{"role": "user", "content": "What if you add 55 to that?"},
{"role": "assistant", "content": "345 plus 55 equals 400.", "reasoning_content": "345 + 55 = 400"},
{"role": "user", "content": "What was the intermediate result?"}
],
"enable_thinking": true,
"clear_thinking": true
}'
DashScope
import os
from dashscope import Generation
# Multi-turn conversation where the assistant message carries reasoning_content (historical thinking process)
messages = [
{"role": "user", "content": "Please calculate 15 * 23."},
{"role": "assistant", "content": "15 multiplied by 23 equals 345.", "reasoning_content": "15 * 23 = 345"},
{"role": "user", "content": "What if you add 55 to that?"},
{"role": "assistant", "content": "345 plus 55 equals 400.", "reasoning_content": "345 + 55 = 400"},
{"role": "user", "content": "What was the intermediate result?"},
]
response = Generation.call(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model="glm-5.2",
messages=messages,
result_format="message",
enable_thinking=True,
# true: Ignores historical reasoning_content to reduce context length and cost
# false (default): Retains historical reasoning_content (Preserved Thinking)
clear_thinking=True,
)
print(response.usage.input_tokens)
Outros recursos
| Modelo | ||||||
|---|---|---|---|---|---|---|
glm-5,2 | Suportado | Suportado | Suportado
| Não suportado | Não suportado | Suportado
|
glm-5,2-fast-preview | Suportado | Suportado | Suportado
| Não suportado | Não suportado | Suportado
|
glm-5,1 | Suportado | Suportado | Suportado
| Não suportado | Não suportado | Suportado
|
glm-5 | Suportado | Suportado | Suportado
| Não suportado | Não suportado | Suportado
|
glm-4,7 | Suportado | Suportado | Suportado
| Não suportado | Não suportado | Suportado
|
glm-4,6 | Suportado | Suportado | Suportado
| Não suportado | Não suportado | Suportado
|
Valores padrão dos parâmetros
Modelo | enable_thinking | temperature | top_p | top_k | repetition_penalty |
|---|---|---|---|---|---|
glm-5.2 | true | 1,0 | 0,95 | 20 | 1,0 |
glm-5.1 | true | 1,0 | 0,95 | 20 | 1,0 |
glm-5 | true | 1,0 | 0,95 | 20 | 1,0 |
glm-4.7 | true | 1,0 | 0,95 | 20 | 1,0 |
glm-4.6 | true | 1,0 | 0,95 | 20 | 1,0 |
Para mais informações sobre os parâmetros, consulte OpenAI compatible - Chat.
Precauções
AvisoModelos open-source de terceiros implantados na cloud (como o glm-5,2) lidam com hiperparâmetros de maneira diferente da versão oficial do modelo: a versão oficial valida limiares nos hiperparâmetros e reverte para valores padrão quando os limites são excedidos; já a versão implantada na cloud repassa diretamente os valores fornecidos pelo usuário sem validação de limiar. Portanto, configurações inadequadas de hiperparâmetros (como definir repetition_penalty como 0.1) podem causar saídas inesperadas (como impressão repetida). Recomendamos utilizar os valores padrão de hiperparâmetros (consulte a tabela de valores padrão acima) para modelos open-source de terceiros e evitar parâmetros personalizados.
Modelos e faturamento
Os modelos da série GLM são modelos de raciocínio híbrido desenvolvidos pela Zhipu AI para agentes. Eles oferecem modos de raciocínio e sem raciocínio.
- glm-5,2: O modelo GLM mais recente, com comprimento de contexto de 1M. Suporta Function Calling, saída estruturada e cache implícito. É possível chamá-lo através das interfaces compatíveis com OpenAI, DashScope e Anthropic.
Para informações sobre o comprimento de contexto do modelo e preços, consulte o console do Model Studio.
O faturamento é baseado no número de tokens de entrada e saída.
No modo de raciocínio, a cadeia de pensamento é faturada como tokens de saída.
Códigos de erro
Se ocorrer um erro, consulte Error codes para obter informações de solução de problemas.