-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_streaming.py
More file actions
233 lines (177 loc) · 6.83 KB
/
example_streaming.py
File metadata and controls
233 lines (177 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
"""
Example of using md2term as a library for streaming markdown content.
This demonstrates how to integrate md2term into your Python application
to stream markdown content with proper terminal formatting, similar to
how LLM tools like Simon Willison's llm stream their output.
Usage:
python example_streaming.py
"""
import time
import shutil
from rich.console import Console
# Import the streaming functionality from md2term
from md2term import StreamingRenderer
def simulate_llm_streaming():
"""
Simulate an LLM streaming markdown content character by character.
This is similar to how tools like OpenAI's API or other LLM services
stream their responses in real-time.
"""
# Sample markdown content with various features
markdown_content = """# AI Assistant Response
Thank you for your question! Let me provide a comprehensive answer.
## Key Points
Here are the **most important** things to understand:
1. **Streaming** allows for *real-time* display of content
2. **Markdown formatting** is preserved during streaming
3. **Terminal colors** make the output more readable
### Code Example
Here's a simple Python example:
```python
def hello_world():
print("Hello, World!")
return "success"
# Call the function
result = hello_world()
```
### Additional Features
> This is a blockquote that demonstrates
> how multi-line quoted text appears
> in the terminal output.
Some `inline code` and a [link to example](https://example.com) for reference.
---
## Performance Considerations
- Streaming reduces perceived latency
- Users see content as it's generated
- Better user experience for long responses
That's the complete explanation! Hope this helps.
"""
# Get terminal width
width = shutil.get_terminal_size().columns
# Create a Rich console
console = Console(width=width, force_terminal=True)
# Create the streaming renderer
renderer = StreamingRenderer(console)
print("🤖 Simulating LLM streaming output with md2term...\n")
time.sleep(1)
try:
# Stream the content character by character
for char in markdown_content:
renderer.add_text(char)
# Simulate network delay (like real LLM streaming)
# Adjust this value to make it faster/slower
time.sleep(0.02) # 20ms delay per character
except KeyboardInterrupt:
print("\n\n⚠️ Streaming interrupted by user")
finally:
# Always finalize to ensure complete rendering
renderer.finalize()
print("\n✅ Streaming complete!")
def simulate_chunk_streaming():
"""
Simulate streaming in larger chunks (more realistic for real applications).
This is more similar to how actual LLM APIs work - they send chunks
of text rather than individual characters.
"""
# Different markdown content for the second example
chunks = [
"# Product Documentation\n\n",
"Welcome to our **API documentation**. ",
"This guide will help you get started.\n\n",
"## Authentication\n\n",
"To authenticate, you'll need an API key:\n\n",
"```bash\n",
'curl -H "Authorization: Bearer YOUR_API_KEY" \\\n',
" https://api.example.com/v1/data\n",
"```\n\n",
"## Rate Limits\n\n",
"Please note the following limits:\n\n",
"- **Free tier**: 100 requests/hour\n",
"- **Pro tier**: 1,000 requests/hour\n",
"- **Enterprise**: *Unlimited*\n\n",
"> **Important**: Rate limits reset every hour\n",
"> at the top of the hour (e.g., 2:00 PM, 3:00 PM).\n\n",
"For more information, visit our [support page](https://example.com/support).\n",
]
# Get terminal width
width = shutil.get_terminal_size().columns
# Create a Rich console
console = Console(width=width, force_terminal=True)
# Create the streaming renderer
renderer = StreamingRenderer(console)
print("📚 Simulating chunk-based streaming (more realistic)...\n")
time.sleep(1)
try:
# Stream in chunks
for i, chunk in enumerate(chunks):
print(f"📦 Chunk {i+1}/{len(chunks)}", end="\r")
renderer.add_text(chunk)
# Simulate processing time between chunks
time.sleep(0.3) # 300ms between chunks
except KeyboardInterrupt:
print("\n\n⚠️ Streaming interrupted by user")
finally:
# Clear the chunk counter and finalize
print(" " * 20, end="\r") # Clear the chunk counter
renderer.finalize()
print("\n✅ Chunk streaming complete!")
def demonstrate_library_usage():
"""
Show how to integrate md2term into your own application.
This is the pattern you would use in your own code.
"""
print("🔧 Library Integration Example\n")
# Your application's markdown content (could come from anywhere)
content_parts = [
"# My Application Output\n\n",
"Processing your request",
".",
".",
".\n\n",
"**Results found!**\n\n",
"Here's what I discovered:\n\n",
"```json\n",
'{\n "status": "success",\n "data": [\n {"id": 1, "name": "Item 1"},\n {"id": 2, "name": "Item 2"}\n ]\n}\n',
"```\n\n",
"Analysis complete! 🎉",
]
# Set up md2term streaming
console = Console(force_terminal=True)
renderer = StreamingRenderer(console)
try:
for part in content_parts:
# In a real app, this might come from:
# - API responses
# - Database queries
# - File processing
# - LLM streaming
renderer.add_text(part)
time.sleep(0.5) # Simulate processing time
finally:
renderer.finalize()
print("\n💡 This is how you'd integrate md2term into your own Python application!")
if __name__ == "__main__":
print("🚀 md2term Library Streaming Examples\n")
print(
"This demonstrates how to use md2term as a library in your Python applications."
)
print("Press Ctrl+C at any time to interrupt.\n")
# Run the examples
try:
# Example 1: Character-by-character streaming (like LLM output)
simulate_llm_streaming()
print("\n" + "=" * 60 + "\n")
# Example 2: Chunk-based streaming (more realistic)
simulate_chunk_streaming()
print("\n" + "=" * 60 + "\n")
# Example 3: Library integration pattern
demonstrate_library_usage()
except KeyboardInterrupt:
print("\n\n👋 Examples interrupted. Goodbye!")
print("\n🎯 Key takeaways:")
print(" • Import StreamingRenderer from md2term")
print(" • Create a Rich Console")
print(" • Use renderer.add_text() for each chunk")
print(" • Always call renderer.finalize() when done")
print(" • Perfect for LLM streaming, API responses, and real-time content!")