
# 流式输出
**流式输出（Streaming Output）** 是指大模型在生成回复时，每产生一个片段就立即推送给调用方，而非等待全部内容生成完毕后一次性返回。降低了首字延迟，用户更快看到响应，提升交互体验。
工作原理如下：
1. 请求发起：调用方发起请求，标识启用流式模式，"stream"字段取值为"True"。
2. 逐步生成：模型逐个 Token 生成内容。
3. 逐块推送：每生成一个片段，通过 SSE（Server-Sent Events）等方式立即推送。
4. 结束标识：全部生成完成后，发送结束标识信号。
#### 应用场景
- **智能对话：**逐字显示回复，用户无需等待完整生成，体验更自然流畅。
- **长文本生成：**文章、报告等长内容产出时，边生成边展示，减少用户感知等待。
- **代码补全：**逐行输出代码建议，开发者可即时查看并决定是否采纳。
- **实时翻译：**输入源文本后逐段返回翻译结果，适用于会议同传等实时场景。
- **内容摘要：**长文档摘要生成时，逐步呈现要点，用户可提前判断相关性。
- **数据报表解读：**大量数据分析结果逐段输出，便于用户逐步理解。
 
#### 计费说明
流式输出与非流式输出计费规则一样，均是根据输入和输出消耗的Tokens数量计费。流式输出场景下，假如请求中断，输出的Token仅计算成功生成的部分，未生成的Token不计费。
#### API说明
模型调用的完整参数列表请见[对话Chat/Post](https://support.huaweicloud.com/model-call-maas/model-call-018.html)。
#### 前提条件
- 已在"模型推理 \> 在线推理 \> 预置服务"页签开通预置服务。详情请见[开通MaaS预置服务](https://support.huaweicloud.com/model-call-maas/model-call-052.html)。
- （可选）如果需要控制服务调用流量，可提前创建自定义接入点，详情请参见[创建自定义接入点](https://support.huaweicloud.com/model-call-maas/model-call-048.html#ZH-CN_TOPIC_0000002549717747__zh-cn_topic_0000002397381901_section446813817237)。

- 已获取API Key。详情请见[在MaaS管理API Key](https://support.huaweicloud.com/model-call-maas/model-call-049.html)。
- 已获取模型服务的model参数值。支持的模型信息和接口详情请见[对话Chat/Post](https://support.huaweicloud.com/model-call-maas/model-call-018.html)。
 
#### 快速入门
下面以GLM-5.2模型为例，展示了非思考模式下的流式输出效果。
![](https://support.huaweicloud.com/model-call-maas/public_sys-resources/note_3.0-zh-cn.png)
思考模式下，思考过程也是按照流式输出的。在思考阶段，"reasoning_content"字段是模型思考的内容，"content"字段为空（回复内容为空）；当思考结束后，"content"字段才会有值。
- [Python]
  ```
  import requests
  import json
  if __name__ == '__main__':
      url = "https://api.modelarts-maas.com/v2/chat/completions"  # API地址
      api_key = "MAAS_API_KEY"  # 把MAAS_API_KEY替换成已获取的API Key
      # Send request.
      headers = {
          'Content-Type': 'application/json',
          'Authorization': f'Bearer {api_key}'
      }
      data = {
           "model": "glm-5.2",  # model参数
           "messages": [
               {"role": "system", "content": "You are a helpful assistant."},
               {"role": "user", "content": "你好"}
           ],
          "thinking": {
              "type": "disabled"
           },
           "stream": True
       }
      response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)
      # Print result.
      print(response.status_code)
      print(response.text)
  ```
- [Curl]
  ```
  curl -X POST "https://api.modelarts-maas.com/v2/chat/completions" \   
  -H "Content-Type: application/json" \   
  -H "Authorization: Bearer $MAAS_API_KEY" \   
  -d '{     
      "model": "glm-5.2",     
      "messages": [       
          {"role": "system", "content": "You are a helpful assistant."},     
          {"role": "user", "content": "你好"}    
      ],
      "thinking": {
          "type": "disabled"
       },
      "stream": true
  }'
  ```
- [OpenAI SDK]
  ```
  from openai import OpenAI
  import httpx
  base_url = "https://api.modelarts-maas.com/openai/v1"  # API地址
  api_key = "MAAS_API_KEY"  # 把MAAS_API_KEY替换成已获取的API Key
  client = OpenAI(api_key=api_key, base_url=base_url, http_client=httpx.Client(verify=False))
  response = client.chat.completions.create(
      model="glm-5.2",
      messages=[
          {"role": "system", "content": "You are a helpful assistant"},
          {"role": "user", "content": "你好"},
      ],
      chat_template_kwargs = {
          "thinking": False
      },
      stream=True
  )
  for chunk in response:
      if not chunk.choices:
          continue
      print(chunk.choices[0].delta.content, end="")
  ```
流式输出的响应：
```
data: {"id":"4dc9cd5407fa4fcf92e5d547984819fd","object":"chat.completion.chunk","created":1784770930,"model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":"你好"}}],"service_tier":"default","first_token_return_time":1784770931.0727186}
data: {"id":"4dc9cd5407fa4fcf92e5d547984819fd","object":"chat.completion.chunk","created":1784770930,"model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":"！有什么"}}],"service_tier":"default","first_token_return_time":1784770931.0729105}
data: {"id":"4dc9cd5407fa4fcf92e5d547984819fd","object":"chat.completion.chunk","created":1784770930,"model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":"我可以帮你的"}}],"service_tier":"default","first_token_return_time":1784770931.1312485}
data: {"id":"4dc9cd5407fa4fcf92e5d547984819fd","object":"chat.completion.chunk","created":1784770930,"model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":"吗？"}}],"service_tier":"default","first_token_return_time":1784770931.1887114}
data: {"id":"4dc9cd5407fa4fcf92e5d547984819fd","object":"chat.completion.chunk","created":1784770930,"model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop"}],"service_tier":"default","first_token_return_time":1784770931.1887114}
data: {"id":"4dc9cd5407fa4fcf92e5d547984819fd","object":"chat.completion.chunk","created":1784770930,"model":"glm-5.2","choices":[],"usage":{"prompt_tokens":14,"total_tokens":23,"completion_tokens":9,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":0}},"service_tier":"default","first_token_return_time":1784770931.188748}
data: [DONE]
```
#### 错误码
模型调用过程中，如有报错，请参考[错误码](https://support.huaweicloud.com/model-call-maas/model-call-035.html)排查并处理。
