Simple example of using StreamingContext.
()
| 18 | |
| 19 | |
| 20 | async def simple_streaming_example(): |
| 21 | """Simple example of using StreamingContext.""" |
| 22 | console = Console() |
| 23 | |
| 24 | # Simulate chunks coming from an LLM |
| 25 | text_chunks = [ |
| 26 | "I'll help you ", |
| 27 | "understand how ", |
| 28 | "streaming works.\n\n", |
| 29 | "When content streams ", |
| 30 | "from an LLM, ", |
| 31 | "it arrives in chunks ", |
| 32 | "like this.\n\n", |
| 33 | "The streaming display:\n", |
| 34 | "• Shows a progress indicator\n", |
| 35 | "• Detects content type\n", |
| 36 | "• Provides a preview\n", |
| 37 | "• Shows statistics\n\n", |
| 38 | "This makes the ", |
| 39 | "experience more ", |
| 40 | "responsive!", |
| 41 | ] |
| 42 | |
| 43 | # Use StreamingContext - it's that simple! |
| 44 | with StreamingContext(console=console, title="🤖 Assistant") as ctx: |
| 45 | for chunk in text_chunks: |
| 46 | ctx.update(chunk) |
| 47 | await asyncio.sleep(0.1) # Simulate network delay |
| 48 | |
| 49 | print("\n✅ Streaming complete!") |
| 50 | |
| 51 | |
| 52 | async def streaming_with_code(): |