Item 2: Use Streaming for Real-Time Response¶
当用户请求长文本或需要实时反馈时,不要让用户等待完整结果。使用流式输出,让 AI 的思考过程可见。
问题¶
非流式调用的问题:
如果生成需要 30 秒,用户会以为程序卡死了。
解决方案¶
用 stream=True 启用流式输出:
stream = await agent.run(prompt, stream=True)
print("Agent: ", end="", flush=True)
async for update in stream:
if update.contents:
for content in update.contents:
if content.text:
print(content.text, end="", flush=True)
await stream.get_final_response() # 必须调用
关键细节¶
- flush=True:立即输出,不等缓冲区
- get_final_response():必须调用,否则资源泄漏
- update.contents:内容在列表里,需要遍历取 text
Things to Remember¶
stream=True返回 ResponseStream,不是普通字符串async for update in stream遍历每个 chunkupdate.contents[i].text取文本内容- 必须调用
get_final_response()获取完整响应