智谱(Zhipu)大模型的流式使用 response.iter_lines() 逐行解析 SSE 流

复制代码
import requests
import json
url = "https://open.bigmodel.cn/api/paas/v4/chat/completions"

payload = {
    "model": "glm-4.7",
    "messages": [
        {
            "role": "user",
            "content": "写一首关于春天的诗。"
        }
    ],
    "stream": True,
    "temperature": 1
}
headers = {
    "Authorization": "api-key",
    "Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
# 检查状态
if response.status_code != 200:
    print(f"请求失败: {response.status_code} - {response.text}")
    exit()
full_content = []
#流失输出
print("正在生成...\n")
try:
    for line in response.iter_lines():
        if line:
            decoded_line = line.decode('utf-8')
            # SSE 格式: data: {...}
            if decoded_line.startswith("data:"):
                json_str = decoded_line[len("data:"):].strip()
                if json_str == "[DONE]":
                    break
                try:
                    chunk = json.loads(json_str)
                    # 提取 reasoning_content
                    if "choices" in chunk and len(chunk["choices"]) > 0:
                        delta = chunk["choices"][0].get("delta", {})
                        content = delta.get("reasoning_content")
                        if content is not None:
                            full_content.append(content)
                            #print(content, end="", flush=True)  # 实时打印
                except json.JSONDecodeError as e:
                    print(f"\nJSON 解析错误: {e}, 原始数据: {json_str}")
except Exception as e:
    print(f"\n流处理异常: {e}")

print("\n\n✅ 最终完整输出:")
print("".join(full_content))

你提供的代码使用 requests.post 调用智谱(Zhipu)大模型的 流式(stream=True)接口,但当前写法存在两个关键问题:

❌ response.text 无法正确处理 流式响应(SSE) ------ 它会等待整个响应结束才返回,而流式接口是逐块发送的;

❌ 智谱 GLM-4 的流式响应中,实际文本内容在 choices0.delta.reasoning_content 字段(而非标准 OpenAI 的 content)。

✅ 正确做法:使用 response.iter_lines() 逐行解析 SSE 流

以下是 完整可运行的修复代码,能正确提取 reasoning_content 并拼接成完整诗歌:

python

编辑

import requests

import json

配置

url = "https://open.bigmodel.cn/api/paas/v4/chat/completions"

payload = {

"model": "glm-4.7",

"messages": [

{"role": "user", "content": "写一首关于春天的诗。"}

],

"stream": True,

"temperature": 1

}

headers = {

"Authorization": "Bearer your_api_key_here", # ⚠️ 替换为你的有效 API Key

"Content-Type": "application/json"

}

发送流式请求

response = requests.post(url, json=payload, headers=headers, stream=True)

检查状态

if response.status_code != 200:

print(f"请求失败: {response.status_code} - {response.text}")

exit()

full_content = \[\]

print("正在生成...\n")

try:

for line in response.iter_lines():

if line:

decoded_line = line.decode('utf-8')

SSE 格式: data: {...}

if decoded_line.startswith("data:"):

json_str = decoded_linelen("data:"):.strip()

if json_str == "DONE":

break

try:

chunk = json.loads(json_str)

提取 reasoning_content

if "choices" in chunk and len(chunk"choices") > 0:

delta = chunk"choices"0.get("delta", {})

content = delta.get("reasoning_content")

if content is not None:

full_content.append(content)

print(content, end="", flush=True) # 实时打印

except json.JSONDecodeError as e:

print(f"\nJSON 解析错误: {e}, 原始数据: {json_str}")

except Exception as e:

print(f"\n流处理异常: {e}")

print("\n\n✅ 最终完整输出:")

print("".join(full_content))

🔑 关键说明

表格

问题 解决方案

流式响应读取 使用 stream=True + response.iter_lines()

SSE 格式解析 每行以 data: 开头,去掉前缀后解析 JSON

字段位置 智谱 GLM-4 流式响应使用 delta.reasoning_content(不是 content)

结束标志 遇到 data: DONE 时停止

实时输出 print(..., end="", flush=True) 实现逐字打印

📝 输出示例(模拟)

text

编辑

正在生成...

春天的缕东风穿过山谷,

便唤醒了沉睡的田野。

✅ 最终完整输出:

春天的缕东风穿过山谷,

便唤醒了沉睡的田野。

相关推荐
天蓝色的鱼鱼8 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
SelectDB9 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
泯泷9 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花9 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷9 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜9 小时前
Spring Boot 核心知识点总结
前端
lichenyang45310 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端
古夕10 小时前
第三方 SSO 接入实践:redirect_uri 编码、回调一致性与跨项目联调
前端·vue.js
朦胧之10 小时前
页面白屏卡住排查方法
前端·javascript
用户5936087414010 小时前
Playwright 黑魔法:用 ClipboardEvent 绕过 React 富文本编辑器
前端