-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path04_server_http.py
More file actions
43 lines (30 loc) · 1.32 KB
/
Copy path04_server_http.py
File metadata and controls
43 lines (30 loc) · 1.32 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
"""Example 4 — talk to the server over plain HTTP (OpenAI-compatible).
Use this when the caller is NOT this Python process: another language, a tool,
or a different machine. The server speaks the OpenAI Chat Completions shape.
Install requests first:
pip install requests
Start the server in another terminal:
python app.py
Then run this from the project root:
python examples/04_server_http.py
Each response includes a `conversation_id`; send it back in the next request's
body to continue the same thread.
"""
import requests
URL = "http://localhost:8000/v1/chat/completions"
# Turn 1 — new conversation.
first = requests.post(URL, json={
"model": "deepseek-chat",
"messages": [{"role": "user", "content": "My name is Ada. Remember it."}],
}).json()
print("DeepSeek:", first["choices"][0]["message"]["content"])
cid = first["conversation_id"]
print("conversation_id:", cid)
# Turn 2 — continue by sending the conversation_id back in the body. No `model`
# here: a thread's model is fixed when it's created, so on resume the server
# ignores `model` and keeps the original.
second = requests.post(URL, json={
"conversation_id": cid,
"messages": [{"role": "user", "content": "What's my name? Just the name."}],
}).json()
print("DeepSeek:", second["choices"][0]["message"]["content"]) # -> recalls "Ada"