-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
96 lines (75 loc) · 2.64 KB
/
Copy pathtest_client.py
File metadata and controls
96 lines (75 loc) · 2.64 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
"""
test_client.py — Quick test client for the ChatGPT Scraper API
JSON mode (default):
python test_client.py "What is 2 + 2?"
SSE streaming mode:
python test_client.py --stream "Tell me a short joke."
"""
import sys
import json
import urllib.request
ENDPOINT = "http://127.0.0.1:8000"
def ask_json(prompt: str) -> None:
"""Hit POST /ask — get a single JSON response and exit."""
url = f"{ENDPOINT}/ask"
req = urllib.request.Request(
url,
data=json.dumps({"prompt": prompt}).encode(),
headers={"Content-Type": "application/json"},
method="POST",
)
print(f"→ POST {url}")
print(f"→ Prompt: {prompt!r}\n")
with urllib.request.urlopen(req) as resp:
body = json.loads(resp.read().decode())
print(json.dumps(body, indent=2))
if body.get("status") == "ok":
print(f"\n✅ Response received ({len(body.get('response', ''))} chars)")
else:
print(f"\n❌ Error: {body.get('error')}")
def ask_stream(prompt: str) -> None:
"""Hit POST /ask/stream — consume SSE events and exit when done."""
url = f"{ENDPOINT}/ask/stream"
req = urllib.request.Request(
url,
data=json.dumps({"prompt": prompt}).encode(),
headers={"Content-Type": "application/json"},
method="POST",
)
print(f"→ POST {url}")
print(f"→ Prompt: {prompt!r}\n")
event = ""
with urllib.request.urlopen(req) as resp:
for raw_line in resp:
line = raw_line.decode().strip()
if not line:
continue
if line.startswith("event:"):
event = line[len("event:"):].strip()
elif line.startswith("data:"):
raw_data = line[len("data:"):].strip()
try:
data = json.loads(raw_data)
except json.JSONDecodeError:
data = {"raw": raw_data}
if event == "start":
print("[stream started]")
elif event == "message":
print(data.get("delta", "").replace("\\n", "\n"), end="", flush=True)
elif event == "done":
print("\n\n[stream complete]")
break
elif event == "error":
print(f"\n[ERROR] {data.get('error')}")
break
if __name__ == "__main__":
args = sys.argv[1:]
stream_mode = False
if "--stream" in args:
stream_mode = True
args.remove("--stream")
prompt = " ".join(args) if args else "Tell me a short joke."
if stream_mode:
ask_stream(prompt)
else:
ask_json(prompt)