forked from mohammad-mousavi005/deepseek-api-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.py
More file actions
62 lines (50 loc) · 1.48 KB
/
Copy pathexample.py
File metadata and controls
62 lines (50 loc) · 1.48 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
import requests
import json
BASE_URL = "http://127.0.0.1:8000/v1/chat/completions"
HEADERS = {
"Content-Type": "application/json",
"Authorization": "Bearer dummy-key"
}
MODELS = {
"thinking_not_search": True,
"thinking_search": True,
"not_thinking_not_search": True,
"not_thinking_search": True
}
TEST_STREAMING = {
"stream": False,
"non_stream": True,
}
PAYLOAD_TEMPLATE = {
"messages": [
{"role": "user", "content": "Explain quantum computing in simple terms."}
],
"temperature": 0.7
}
def run_request(model_id, stream: bool):
payload = dict(PAYLOAD_TEMPLATE)
payload["model"] = model_id
payload["stream"] = stream
print(f"\n=== Running model={model_id} stream={stream} ===")
if stream:
with requests.post(BASE_URL, headers=HEADERS, json=payload, stream=True) as r:
for chunk in r.iter_lines():
if chunk:
decoded = chunk.decode("utf-8")
print(decoded)
else:
r = requests.post(BASE_URL, headers=HEADERS, json=payload)
try:
print(json.dumps(r.json(), indent=2))
except Exception:
print(r.text)
def main():
for model_id, enabled in MODELS.items():
if not enabled:
continue
if TEST_STREAMING["non_stream"]:
run_request(model_id, stream=False)
if TEST_STREAMING["stream"]:
run_request(model_id, stream=True)
if __name__ == "__main__":
main()