Todos los productos
Search
Centro de documentos

Alibaba Cloud Model Studio:GLM

Última actualización: Oct 12, 2026

Este tema describe cómo utilizar las API para invocar modelos de la serie GLM en la plataforma Alibaba Cloud Model Studio.

Endpoints de servicio

Los endpoints de servicio varían según la región. Configure la URL base correspondiente a la región seleccionada.

Compatible con OpenAI

China (Beijing)

El base_url para llamadas SDK es: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/chat/completions

EE. UU. (Virginia)

El base_url para llamadas SDK es: https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/compatible-mode/v1

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions

Germany (Frankfurt)

El base_url para llamadas SDK es: https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions

Hong Kong (China)

El base_url para llamadas SDK es: https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1/chat/completions

Singapore

Cuerpo de la pestaña

El base_url para llamadas SDK es: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions

Compatible con OpenAI - Responses API

NotaLa API Responses actualmente solo admite glm-5.2 y glm-5.3, y está disponible únicamente en las regiones de China (Beijing) y Singapore.

Singapore

El base_url para llamadas al SDK: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1

Endpoint HTTP: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses

China (Beijing)

El base_url para llamadas al SDK: https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1

Endpoint HTTP: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/responses

DashScope

China (Beijing)

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation

El base_url para llamadas SDK es:

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";

EE. UU. (Virginia)

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation

El base_url para llamadas SDK es:

Python

dashscope.base_http_api_url = 'https://{WorkspaceId}.us-east-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}.us-east-1.maas.aliyuncs.com/api/v1");
  • Método 2:
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl="https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/api/v1";

Germany (Frankfurt)

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.eu-central-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation

El base_url para llamadas SDK es:

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";

Hong Kong (China)

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation

El base_url para llamadas SDK es:

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

Contenido de la pestaña

Endpoint de solicitud HTTP: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation

El base_url para llamadas SDK es: dashscope.base_http_api_url = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"

Reemplace {WorkspaceId} con su ID de espacio de trabajo real.

Introducción

glm-5.3, glm-5.2 y glm-5.2-fast-preview son los modelos más recientes de la serie GLM. Tienen una longitud de contexto de 1M. glm-5.2 y glm-5.2-fast-preview le permiten configurar el modo de pensamiento y el modo sin pensamiento mediante el parámetro enable_thinking. glm-5.3 admite únicamente el modo de pensamiento, el cual no se puede desactivar. Puede ejecutar el siguiente código para invocar rápidamente el modelo glm-5.2 en modo de pensamiento.

Antes de comenzar, obtenga una clave API y configúrela como variable de entorno. Si utiliza un SDK, también debe instalar el SDK de OpenAI o DashScope.

Compatible con OpenAI

NotaEl parámetro enable_thinking no es un parámetro estándar de OpenAI. En el SDK de Python de OpenAI, se pasa a través de extra_body. En el SDK de Node.js, se pasa como un parámetro de nivel superior.

Python

Código de ejemplo

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

Respuesta

====================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 ejemplo

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();

Respuesta

====================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 ejemplo

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 ejemplo

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)

Respuesta

====================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 ejemplo

ImportanteLa versión del SDK de Java de DashScope debe ser 2.19.4 o 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());
        }
    }
}

Respuesta

====================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 ejemplo

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"
    }
}'

Compatible con Anthropic

Python

Código de ejemplo

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 ejemplo

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?"
        }
    ]
}'

Llamada a herramientas en streaming

glm-5.3, glm-5.2, glm-5.2-fast-preview, glm-5.1 y glm-5 admiten el parámetro tool_stream. Este parámetro booleano tiene como valor predeterminado true para glm-5.3 y false para los demás modelos, y solo surte efecto cuando stream se establece en true. Cuando está habilitado, los argumentos del parámetro tool_call de Function Calling se devuelven de forma incremental en un flujo, en lugar de todos a la vez al finalizar la generación completa.

El comportamiento combinado de stream y tool_stream es el siguiente:

stream

tool_stream

Método de retorno de tool_call

true

true

Los argumentos se devuelven incrementalmente en múltiples fragmentos.

true

false (predeterminado)

Los argumentos se devuelven completamente en un solo fragmento.

false

true/false

tool_stream no tiene efecto. Los argumentos se devuelven de una vez en la respuesta completa.

Compatible con OpenAI

Python

Código de ejemplo

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 ejemplo

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 ejemplo

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 ejemplo

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 ejemplo

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"
    }
}'

Esfuerzo de razonamiento (reasoning_effort)

glm-5.3, glm-5.2, glm-5.2-fast-preview y glm-5.1 tienen el modo de pensamiento habilitado de forma predeterminada. El modelo primero genera el proceso de pensamiento (reasoning_content) y luego proporciona la respuesta final. Puede utilizar el parámetro reasoning_effort para ajustar el esfuerzo de razonamiento. Un valor más alto indica un pensamiento más exhaustivo. Los valores admitidos varían según el modelo. Si pasa un valor no admitido, se devolverá un error invalid_parameter_error. Seleccione un valor de la siguiente tabla.

Modelo

Valores disponibles para reasoning_effort

glm-5.3

low, high, max (máximo)

glm-5.2

none (sin razonamiento, reasoning_tokens=0), minimal, low, medium, high, xhigh, max (máximo)

glm-5.2-fast-preview

none (sin razonamiento, reasoning_tokens=0), minimal, low, medium, high, xhigh, max (máximo)

glm-5.1

none, minimal, low, medium, high, xhigh (máximo, max no es compatible)

NotaPara desactivar el modo de pensamiento, establezca el parámetro enable_thinking en false en el modo compatible con OpenAI o DashScope. Este parámetro tiene mayor prioridad que reasoning_effort. glm-5.3 no admite la desactivación del modo de pensamiento. Establecer enable_thinking en false no tiene efecto.

NotaEl modo compatible con Anthropic no admite el parámetro reasoning_effort. Para obtener el contenido de pensamiento, utilice el parámetro nativo thinking de Anthropic: {"thinking":{"type":"enabled","budget_tokens":1024}}. Cuando está activado, la respuesta content incluirá un bloque de pensamiento con type establecido en thinking.

Compatible con OpenAI

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)

Borrar historial de pensamiento (clear_thinking)

El parámetro clear_thinking controla si el reasoning_content (proceso de pensamiento) de turnos anteriores en una conversación de múltiples turnos se pasa al modelo como contexto. Este parámetro solo es compatible con los modelos de la serie GLM.

  • true: Ignora el reasoning_content de turnos anteriores. Solo se utiliza como entrada de contexto contenido no relacionado con el razonamiento, como texto visible, llamadas a herramientas y resultados. Esto reduce la longitud del contexto y el costo.
  • false: Conserva el reasoning_content de turnos anteriores y lo proporciona al modelo junto con el contexto. Para habilitar el Pensamiento Conservado, debe pasar el reasoning_content histórico de forma completa, sin modificaciones y en el orden original dentro de los mensajes. La omisión, recorte, reescritura o reordenamiento del contenido puede degradar el rendimiento o provocar fallos en la función.

El valor predeterminado varía según el modelo: true para glm-5.3 y false para los demás modelos GLM.

NotaEste parámetro solo afecta al contenido histórico de pensamiento entre turnos y no cambia si el modelo genera o emite contenido de pensamiento en el turno actual.

El siguiente ejemplo utiliza el mismo conjunto de mensajes de múltiples turnos, donde el mensaje assistant contiene reasoning_content. Cuando clear_thinking se establece en true, el contenido histórico de pensamiento no se incluye en el contexto. Por lo tanto, el recuento de prompt_tokens es menor que cuando se establece en false. El valor real depende de la longitud del reasoning_content histórico.

Compatible con OpenAI

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)

API Responses

glm-5.2 admite llamadas a través de la API Responses compatible con OpenAI. Solo se admiten las regiones de China (Beijing) y Singapur. Para ver los endpoints, consulte Endpoints de servicio.

Al llamar a la Responses API, puede agregar las herramientas web_search (Búsqueda web), web_extractor (Extractor web) y code_interpreter (Intérprete de código) al parámetro tools.

from openai import OpenAI
import os

client = OpenAI(
    # If the environment variable is not configured, replace the following line with your Model Studio API key: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # The following URL is for the Singapore region. For the other region, use https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

response = client.responses.create(
    model="glm-5.2",
    input="Hello! Please introduce yourself in one sentence.",
    # Optional: enable the web search, web extractor, and code interpreter tools
    tools=[
        {"type": "web_search"},
        {"type": "web_extractor"},
        {"type": "code_interpreter"},
    ],
)

# Get the model response
print(response.output_text)
import OpenAI from "openai";

const openai = new OpenAI({
    // If the environment variable is not configured, replace the following line with your Model Studio API key: apiKey: "sk-xxx"
    apiKey: process.env.DASHSCOPE_API_KEY,
    // The following URL is for the Singapore region. For the other region, use https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
    baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});

const response = await openai.responses.create({
    model: "glm-5.2",
    input: "Hello! Please introduce yourself in one sentence.",
    // Optional: enable the web search, web extractor, and code interpreter tools
    tools: [
        { type: "web_search" },
        { type: "web_extractor" },
        { type: "code_interpreter" },
    ],
});

// Get the model response
console.log(response.output_text);
# The following URL is for the Singapore region. For the other region, use https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/responses
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "glm-5.2",
    "input": "Hello! Please introduce yourself in one sentence.",
    "tools": [
        {"type": "web_search"},
        {"type": "web_extractor"},
        {"type": "code_interpreter"}
    ]
}'

Otras funciones

Modelo

Conversación multironda

Llamada a funciones

Salida estructurada

Búsqueda web

Modo parcial

Caché de contexto

glm-5.3

Compatible

Compatible

Compatible

Solo se admite response_format configurado como json_object

No compatible

No compatible

Compatible

Solo se admite caché implícita

glm-5.2

Compatible

Compatible

Compatible

Solo en modo sin pensamiento

No compatible

No compatible

Compatible

Solo se admite caché implícita

glm-5.2-fast-preview

Compatible

Compatible

Compatible

Solo en modo sin pensamiento

No compatible

No compatible

Compatible

Solo se admite caché implícita

glm-5.1

Compatible

Compatible

Compatible

Solo en modo sin pensamiento

No compatible

No compatible

Compatible

Se admiten tanto caché explícita como implícita

glm-5

Compatible

Compatible

Compatible

Solo en modo sin pensamiento

No compatible

No compatible

Compatible

Solo se admite caché implícita

Valores predeterminados de parámetros

Modelo

enable_thinking

temperature

top_p

top_k

repetition_penalty

glm-5.3

true (no se puede desactivar)

1.0

0.95

No compatible

No compatible

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

Para más información sobre los parámetros, consulte Compatible con OpenAI - Chat.

Precauciones

AdvertenciaLos modelos de código abierto de terceros implementados en la nube (como glm-5.2) manejan los hiperparámetros de manera diferente a la versión oficial del modelo: la versión oficial realiza una validación de umbrales en los hiperparámetros y recurre a los valores predeterminados cuando se superan dichos umbrales; la versión implementada en la nube transmite directamente los valores de los parámetros proporcionados por el usuario sin validación de umbrales. Por lo tanto, configuraciones inadecuadas de hiperparámetros (como establecer repetition_penalty en 0.1) pueden provocar resultados inesperados (como impresión repetida). Recomendamos utilizar los valores predeterminados de los hiperparámetros (consulte la tabla de valores de parámetros predeterminados anterior) para los modelos de código abierto de terceros y evitar el uso de parámetros personalizados.

Modelos y facturación

Los modelos de la serie GLM son modelos de razonamiento híbrido diseñados por Zhipu AI para agentes. Proporcionan modos de pensamiento y sin pensamiento.

  • glm-5.3: El modelo GLM más reciente de la serie GLM con una longitud de contexto de 1M. Admite Function Calling, salida estructurada y caché implícita. Puede llamarlo mediante interfaces compatibles con OpenAI, DashScope y Anthropic.
  • glm-5.2: Un modelo GLM con una longitud de contexto de 1M. Admite Function Calling, salida estructurada y caché implícita. Puede llamarlo mediante interfaces compatibles con OpenAI, DashScope y Anthropic.
  • glm-5.2-fast-preview: El modo rápido del modelo glm-5.2. Para más información, consulte Modo Prime.

Para obtener información sobre la longitud del contexto del modelo y los precios, consulte la consola de Model Studio.

La facturación se basa en el número de tokens de entrada y salida.

En el modo de pensamiento, la cadena de pensamiento se factura como tokens de salida.

Códigos de error

Si ocurre un error, consulte Códigos de error para obtener información sobre solución de problemas.