Solutions for realizing streaming output
The key to improving the interaction experience is to realize real-time response effects. ZipAgent does this through therun_streammethod natively supports streaming output, here are the steps to do so:
- Step1 Import the necessary components: Import from framework
StreamEventTypeenumeration andRunnerresemble - Step2 Create a streaming request: Use
Runner.run_stream()Replacement of conventionalrun()methodologies - Step3 Handling the Event Stream: The two core events can be obtained by traversing the event stream object:
ANSWER_DELTA: Incremental content generated by the modelTOOL_CALL: System prompts for tool invocations
Example of typical implementation code:
stream = Runner.run_stream(agent, "问题内容")
for event in stream:
if event.type == StreamEventType.ANSWER_DELTA:
print(event.content, end="", flush=True)
elif event.type == StreamEventType.TOOL_CALL:
print(f"n正在调用工具: {event.tool_name}...")
Optimization Recommendations::
1. Using WebSocket protocol to transfer streaming data in Web applications
2. Add cursor animation to enhance the user perception during the waiting process.
3. Syntax highlighting for special content (e.g., code blocks)
This answer comes from the articleZipAgent: a lightweight Python framework for building exclusive AI assistants in 5 minutesThe































