native_api.ipynb 10.2 KB
Newer Older
Chayenne's avatar
Chayenne committed
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
7
    "# Native APIs\n",
Chayenne's avatar
Chayenne committed
8
    "\n",
Chayenne's avatar
Chayenne committed
9
    "Apart from the OpenAI compatible APIs, the SGLang Runtime also provides its native server APIs. We introduce these following APIs:\n",
Chayenne's avatar
Chayenne committed
10
    "\n",
Chayenne's avatar
Chayenne committed
11
    "- `/generate` (text generation model)\n",
Chayenne's avatar
Chayenne committed
12
    "- `/get_model_info`\n",
13
    "- `/get_server_info`\n",
Chayenne's avatar
Chayenne committed
14
15
16
    "- `/health`\n",
    "- `/health_generate`\n",
    "- `/flush_cache`\n",
Chayenne's avatar
Chayenne committed
17
    "- `/update_weights`\n",
Chayenne's avatar
Chayenne committed
18
    "- `/encode`(embedding model)\n",
19
    "- `/classify`(reward model)\n",
Chayenne's avatar
Chayenne committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    "\n",
    "We mainly use `requests` to test these APIs in the following examples. You can also use `curl`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Launch A Server"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
34
   "metadata": {},
Chayenne's avatar
Chayenne committed
35
36
37
38
39
40
41
42
43
   "outputs": [],
   "source": [
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
    "\n",
Chayenne's avatar
Chayenne committed
44
45
    "import requests\n",
    "\n",
Chayenne's avatar
Chayenne committed
46
    "server_process = execute_shell_command(\n",
Chayenne's avatar
Chayenne committed
47
    "    \"\"\"\n",
Chayenne's avatar
Chayenne committed
48
49
50
51
52
53
54
55
56
57
58
    "python3 -m sglang.launch_server --model-path meta-llama/Llama-3.2-1B-Instruct --port=30010\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "wait_for_server(\"http://localhost:30010\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
59
    "## Generate (text generation model)\n",
60
    "Generate completions. This is similar to the `/v1/completions` in OpenAI API. Detailed parameters can be found in the [sampling parameters](../references/sampling_params.md)."
Chayenne's avatar
Chayenne committed
61
62
63
64
65
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
66
   "metadata": {},
Chayenne's avatar
Chayenne committed
67
68
69
   "outputs": [],
   "source": [
    "url = \"http://localhost:30010/generate\"\n",
Chayenne's avatar
Chayenne committed
70
    "data = {\"text\": \"What is the capital of France?\"}\n",
Chayenne's avatar
Chayenne committed
71
72
    "\n",
    "response = requests.post(url, json=data)\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
73
    "print_highlight(response.json())"
Chayenne's avatar
Chayenne committed
74
75
76
77
78
79
80
81
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get Model Info\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
82
    "Get the information of the model.\n",
Chayenne's avatar
Chayenne committed
83
84
85
86
87
88
89
90
    "\n",
    "- `model_path`: The path/name of the model.\n",
    "- `is_generation`: Whether the model is used as generation model or embedding model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
91
   "metadata": {},
Chayenne's avatar
Chayenne committed
92
93
94
95
96
97
98
99
   "outputs": [],
   "source": [
    "url = \"http://localhost:30010/get_model_info\"\n",
    "\n",
    "response = requests.get(url)\n",
    "response_json = response.json()\n",
    "print_highlight(response_json)\n",
    "assert response_json[\"model_path\"] == \"meta-llama/Llama-3.2-1B-Instruct\"\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
100
    "assert response_json[\"is_generation\"] is True\n",
Chayenne's avatar
Chayenne committed
101
102
103
104
105
106
107
    "assert response_json.keys() == {\"model_path\", \"is_generation\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
108
109
110
111
112
113
    "## Get Server Info\n",
    "Gets the server information including CLI arguments, token limits, and memory pool sizes.\n",
    "- Note: `get_server_info` merges the following deprecated endpoints:\n",
    "  - `get_server_args`\n",
    "  - `get_memory_pool_size` \n",
    "  - `get_max_total_num_tokens`"
Chayenne's avatar
Chayenne committed
114
115
116
117
118
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
119
   "metadata": {},
Chayenne's avatar
Chayenne committed
120
121
   "outputs": [],
   "source": [
122
    "# get_server_info\n",
Chayenne's avatar
Chayenne committed
123
    "\n",
124
    "url = \"http://localhost:30010/get_server_info\"\n",
Chayenne's avatar
Chayenne committed
125
126
127
128
129
130
131
132
133
    "\n",
    "response = requests.get(url)\n",
    "print_highlight(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
134
135
136
    "## Health Check\n",
    "- `/health`: Check the health of the server.\n",
    "- `/health_generate`: Check the health of the server by generating one token."
Chayenne's avatar
Chayenne committed
137
138
139
140
141
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
142
   "metadata": {},
Chayenne's avatar
Chayenne committed
143
144
   "outputs": [],
   "source": [
145
    "url = \"http://localhost:30010/health_generate\"\n",
Chayenne's avatar
Chayenne committed
146
    "\n",
147
    "response = requests.get(url)\n",
Chayenne's avatar
Chayenne committed
148
149
150
151
152
153
    "print_highlight(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
154
   "metadata": {},
Chayenne's avatar
Chayenne committed
155
156
   "outputs": [],
   "source": [
157
    "url = \"http://localhost:30010/health\"\n",
Chayenne's avatar
Chayenne committed
158
159
160
161
162
    "\n",
    "response = requests.get(url)\n",
    "print_highlight(response.text)"
   ]
  },
163
164
165
166
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
167
    "## Flush Cache\n",
168
    "\n",
169
    "Flush the radix cache. It will be automatically triggered when the model weights are updated by the `/update_weights` API."
170
171
172
173
174
175
176
177
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
178
    "# flush cache\n",
179
    "\n",
180
    "url = \"http://localhost:30010/flush_cache\"\n",
181
    "\n",
182
    "response = requests.post(url)\n",
183
184
185
    "print_highlight(response.text)"
   ]
  },
Chayenne's avatar
Chayenne committed
186
187
188
189
190
191
192
193
194
195
196
197
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Update Weights\n",
    "\n",
    "Update model weights without restarting the server. Use for continuous evaluation during training. Only applicable for models with the same architecture and parameter size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
198
   "metadata": {},
Chayenne's avatar
Chayenne committed
199
200
201
202
203
204
205
206
207
   "outputs": [],
   "source": [
    "# successful update with same architecture and size\n",
    "\n",
    "url = \"http://localhost:30010/update_weights\"\n",
    "data = {\"model_path\": \"meta-llama/Llama-3.2-1B\"}\n",
    "\n",
    "response = requests.post(url, json=data)\n",
    "print_highlight(response.text)\n",
208
    "assert response.json()[\"success\"] is True\n",
Chayenne's avatar
Chayenne committed
209
210
211
212
213
214
215
    "assert response.json()[\"message\"] == \"Succeeded to update model weights.\"\n",
    "assert response.json().keys() == {\"success\", \"message\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
216
   "metadata": {},
Chayenne's avatar
Chayenne committed
217
218
219
220
221
222
223
224
225
226
   "outputs": [],
   "source": [
    "# failed update with different parameter size\n",
    "\n",
    "url = \"http://localhost:30010/update_weights\"\n",
    "data = {\"model_path\": \"meta-llama/Llama-3.2-3B\"}\n",
    "\n",
    "response = requests.post(url, json=data)\n",
    "response_json = response.json()\n",
    "print_highlight(response_json)\n",
227
    "assert response_json[\"success\"] is False\n",
Chayenne's avatar
Chayenne committed
228
229
230
231
232
233
234
    "assert response_json[\"message\"] == (\n",
    "    \"Failed to update weights: The size of tensor a (2048) must match \"\n",
    "    \"the size of tensor b (3072) at non-singleton dimension 1.\\n\"\n",
    "    \"Rolling back to original weights.\"\n",
    ")"
   ]
  },
Chayenne's avatar
Chayenne committed
235
236
237
238
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
239
    "## Encode (embedding model)\n",
Chayenne's avatar
Chayenne committed
240
    "\n",
Chayenne's avatar
Chayenne committed
241
242
    "Encode text into embeddings. Note that this API is only available for [embedding models](openai_api_embeddings.html#openai-apis-embedding) and will raise an error for generation models.\n",
    "Therefore, we launch a new server to server an embedding model."
Chayenne's avatar
Chayenne committed
243
244
245
246
247
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
248
   "metadata": {},
Chayenne's avatar
Chayenne committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
   "outputs": [],
   "source": [
    "terminate_process(server_process)\n",
    "\n",
    "embedding_process = execute_shell_command(\n",
    "    \"\"\"\n",
    "python -m sglang.launch_server --model-path Alibaba-NLP/gte-Qwen2-7B-instruct \\\n",
    "    --port 30020 --host 0.0.0.0 --is-embedding\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "wait_for_server(\"http://localhost:30020\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
266
   "metadata": {},
Chayenne's avatar
Chayenne committed
267
268
269
270
271
272
273
274
275
276
277
278
   "outputs": [],
   "source": [
    "# successful encode for embedding model\n",
    "\n",
    "url = \"http://localhost:30020/encode\"\n",
    "data = {\"model\": \"Alibaba-NLP/gte-Qwen2-7B-instruct\", \"text\": \"Once upon a time\"}\n",
    "\n",
    "response = requests.post(url, json=data)\n",
    "response_json = response.json()\n",
    "print_highlight(f\"Text embedding (first 10): {response_json['embedding'][:10]}\")"
   ]
  },
Chayenne's avatar
Chayenne committed
279
280
281
282
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
283
    "## Classify (reward model)\n",
Chayenne's avatar
Chayenne committed
284
    "\n",
285
    "SGLang Runtime also supports reward models. Here we use a reward model to classify the quality of pairwise generations."
Chayenne's avatar
Chayenne committed
286
287
288
289
290
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
291
   "metadata": {},
Chayenne's avatar
Chayenne committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
   "outputs": [],
   "source": [
    "terminate_process(embedding_process)\n",
    "\n",
    "# Note that SGLang now treats embedding models and reward models as the same type of models.\n",
    "# This will be updated in the future.\n",
    "\n",
    "reward_process = execute_shell_command(\n",
    "    \"\"\"\n",
    "python -m sglang.launch_server --model-path Skywork/Skywork-Reward-Llama-3.1-8B-v0.2 --port 30030 --host 0.0.0.0 --is-embedding\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "wait_for_server(\"http://localhost:30030\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
311
   "metadata": {},
Chayenne's avatar
Chayenne committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer\n",
    "\n",
    "PROMPT = (\n",
    "    \"What is the range of the numeric output of a sigmoid node in a neural network?\"\n",
    ")\n",
    "\n",
    "RESPONSE1 = \"The output of a sigmoid node is bounded between -1 and 1.\"\n",
    "RESPONSE2 = \"The output of a sigmoid node is bounded between 0 and 1.\"\n",
    "\n",
    "CONVS = [\n",
    "    [{\"role\": \"user\", \"content\": PROMPT}, {\"role\": \"assistant\", \"content\": RESPONSE1}],\n",
    "    [{\"role\": \"user\", \"content\": PROMPT}, {\"role\": \"assistant\", \"content\": RESPONSE2}],\n",
    "]\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"Skywork/Skywork-Reward-Llama-3.1-8B-v0.2\")\n",
    "prompts = tokenizer.apply_chat_template(CONVS, tokenize=False)\n",
    "\n",
331
    "url = \"http://localhost:30030/classify\"\n",
Chayenne's avatar
Chayenne committed
332
    "data = {\"model\": \"Skywork/Skywork-Reward-Llama-3.1-8B-v0.2\", \"text\": prompts}\n",
Chayenne's avatar
Chayenne committed
333
334
335
336
337
338
    "\n",
    "responses = requests.post(url, json=data).json()\n",
    "for response in responses:\n",
    "    print_highlight(f\"reward: {response['embedding'][0]}\")"
   ]
  },
Chayenne's avatar
Chayenne committed
339
340
  {
   "cell_type": "code",
341
342
   "execution_count": null,
   "metadata": {},
Chayenne's avatar
Chayenne committed
343
344
   "outputs": [],
   "source": [
Chayenne's avatar
Chayenne committed
345
    "terminate_process(reward_process)"
Chayenne's avatar
Chayenne committed
346
347
348
349
350
351
352
353
354
355
356
357
358
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
359
   "pygments_lexer": "ipython3"
Chayenne's avatar
Chayenne committed
360
361
362
363
364
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}