实现打字机效果的实时AI对话
流式输出(Streaming/SSE)是AI对话的核心体验之一。它让AI一边生成一边输出,用户不用等全部生成完才能看到结果,体验就像真人打字一样。
stream=True 开启流式输出,逐块获取生成结果:
response = client.chat.completions.create(model="deepseek-chat", messages=[...], stream=True)\nfor chunk in response:\n print(chunk.choices[0].delta.content or "", end="")
前端用EventSource或fetch + ReadableStream接收SSE数据流。
对搜科技的API中转 https://ai.duisou.top/v1 完全支持流式输出,和官方API一致,国内直连,速度更快。
对搜科技 · 国内直连 · 微信支付宝付款