SSE接口一次请求会分多次响应体(chunk)返回, 这就可以表现为流式响应.
chatgpt对话窗口几个字几个字蹦出来的效果,就使用的这种协议
python httpx-sse 客户端 demo
import httpx
from httpx_sse import connect_sse
import json
url = "http://127.0.0.1:8080/streamecho"
param = {
"text": "你好啊~",
"num": 100,
"interval": 0.5
}
with httpx.Client() as client:
with connect_sse(client, "GET", url, params=param) as event_source:
# 把 iter_sse() 迭代完, 就相当于处理完了一次流式调用
for sse in event_source.iter_sse():
# 流式响应中,每次响应体的处理逻辑
chunk_result = json.loads(sse.data)
# sse.event 获取 event 类型; sse.data 获取每次返回的数据
print(sse.event, json.dumps(chunk_result, ensure_ascii=False))
print()
# message {"part": 0, "text": "你好啊~", "datetime": "2024-09-03 17:53:35.684078"}
# message {"part": 1, "text": "你好啊~", "datetime": "2024-09-03 17:53:36.185339"}
# message {"part": 2, "text": "你好啊~", "datetime": "2024-09-03 17:53:36.686913"}
参考
Server-Sent Events 教程参考: https://www.ruanyifeng.com/blog/2017/05/server-sent_events.html