-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample.py
More file actions
246 lines (200 loc) · 7.76 KB
/
example.py
File metadata and controls
246 lines (200 loc) · 7.76 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
234
235
236
237
238
239
240
241
242
243
244
245
246
import requests
import json
import time
import os
import sys
# Configuration
ENDPOINT_ID = os.getenv("ENDPOINT_ID", "your-endpoint-id")
API_KEY = os.getenv("RUNPOD_API_KEY")
ENDPOINT_URL = f"https://{ENDPOINT_ID}.api.runpod.ai"
if not API_KEY:
print("Error: Please set RUNPOD_API_KEY environment variable")
sys.exit(1)
def test_streaming():
"""Test streaming endpoint with real-time output"""
print("🔄 Testing Streaming Endpoint")
print("=" * 50)
payload = {
"prompt": "Write a long story about a brave knight who discovers a magical forest",
"max_tokens": 200,
"temperature": 0.7,
"stream": True
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
try:
print(f"📡 Making streaming request to: {ENDPOINT_URL}/v1/completions")
print(f"📝 Prompt: {payload['prompt'][:50]}...")
print("\n🎬 Streaming output:")
print("-" * 30)
response = requests.post(
f"{ENDPOINT_URL}/v1/completions",
json=payload,
headers=headers,
stream=True, # Enable streaming
timeout=300
)
if response.status_code != 200:
print(f"❌ Error: {response.status_code} - {response.text}")
return
# Process streaming response
generated_text = ""
chunk_count = 0
start_time = time.time()
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_part = line[6:] # Remove 'data: ' prefix
if data_part == '[DONE]':
print("\n\n✅ Stream completed!")
break
try:
chunk_data = json.loads(data_part)
if 'text' in chunk_data:
new_text = chunk_data['text']
print(new_text, end='', flush=True)
generated_text += new_text
chunk_count += 1
except json.JSONDecodeError:
# Skip malformed JSON chunks
continue
end_time = time.time()
print(f"\n\n📊 Streaming Statistics:")
print(f" • Total chunks: {chunk_count}")
print(f" • Total characters: {len(generated_text)}")
print(f" • Time taken: {end_time - start_time:.2f} seconds")
print(f" • Average chars/second: {len(generated_text) / (end_time - start_time):.1f}")
except requests.exceptions.RequestException as e:
print(f"❌ Request failed: {e}")
except Exception as e:
print(f"❌ Unexpected error: {e}")
def test_non_streaming():
"""Test non-streaming endpoint for comparison"""
print("\n🔄 Testing Non-Streaming Endpoint")
print("=" * 50)
payload = {
"prompt": "Write a short story about a robot",
"max_tokens": 100,
"temperature": 0.7,
"stream": False
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
try:
print(f"📡 Making non-streaming request...")
start_time = time.time()
response = requests.post(
f"{ENDPOINT_URL}/v1/completions",
json=payload,
headers=headers,
timeout=300
)
end_time = time.time()
if response.status_code == 200:
result = response.json()
print(f"✅ Response received!")
print(f"📝 Generated text: {result.get('text', 'No text found')}")
print(f"⏱️ Time taken: {end_time - start_time:.2f} seconds")
else:
print(f"❌ Error: {response.status_code} - {response.text}")
except Exception as e:
print(f"❌ Error: {e}")
def compare_streaming_vs_non_streaming():
"""Compare streaming vs non-streaming with same prompt"""
print("\n🔄 Comparing Streaming vs Non-Streaming")
print("=" * 50)
prompt = "Tell me about the history of artificial intelligence"
# Test streaming
print("1️⃣ Streaming version:")
start_time = time.time()
payload = {
"prompt": prompt,
"max_tokens": 150,
"temperature": 0.7,
"stream": True
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
f"{ENDPOINT_URL}/v1/completions",
json=payload,
headers=headers,
stream=True,
timeout=300
)
if response.status_code == 200:
first_chunk_time = None
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_part = line[6:]
if data_part != '[DONE]':
try:
chunk_data = json.loads(data_part)
if 'text' in chunk_data and first_chunk_time is None:
first_chunk_time = time.time()
print(f" ⚡ First chunk received in: {first_chunk_time - start_time:.2f}s")
break
except:
continue
streaming_time = time.time() - start_time
except Exception as e:
print(f" ❌ Streaming failed: {e}")
return
# Test non-streaming
print("\n2️⃣ Non-streaming version:")
start_time = time.time()
payload["stream"] = False
try:
response = requests.post(
f"{ENDPOINT_URL}/v1/completions",
json=payload,
headers=headers,
timeout=300
)
non_streaming_time = time.time() - start_time
if response.status_code == 200:
print(f" ⚡ Complete response in: {non_streaming_time:.2f}s")
print(f"\n📊 Comparison:")
print(f" • Streaming first chunk: {first_chunk_time - start_time:.2f}s" if first_chunk_time else " • Streaming: Failed")
print(f" • Non-streaming total: {non_streaming_time:.2f}s")
if first_chunk_time:
improvement = non_streaming_time - (first_chunk_time - start_time)
print(f" • Time to first response improved by: {improvement:.2f}s")
except Exception as e:
print(f" ❌ Non-streaming failed: {e}")
def main():
print("🧪 vLLM Streaming Test Suite")
print("=" * 50)
print(f"🎯 Endpoint: {ENDPOINT_URL}")
print(f"🔑 API Key: {API_KEY[:10]}...")
while True:
print("\n" + "="*50)
print("Choose a test:")
print("1. Test Streaming (real-time output)")
print("2. Test Non-Streaming")
print("3. Compare Streaming vs Non-Streaming")
print("4. Exit")
choice = input("\nEnter your choice (1-4): ").strip()
if choice == '1':
test_streaming()
elif choice == '2':
test_non_streaming()
elif choice == '3':
compare_streaming_vs_non_streaming()
elif choice == '4':
print("👋 Goodbye!")
break
else:
print("❌ Invalid choice. Please enter 1, 2, 3, or 4.")
if __name__ == "__main__":
main()