send_request.ipynb 9.18 KB
Newer Older
Chayenne's avatar
Chayenne committed
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
7
    "# Quick Start: Sending Requests\n",
Chayenne's avatar
Chayenne committed
8
9
10
11
    "This notebook provides a quick-start guide to use SGLang in chat completions after installation.\n",
    "\n",
    "- For Vision Language Models, see [OpenAI APIs - Vision](../backend/openai_api_vision.ipynb).\n",
    "- For Embedding Models, see [OpenAI APIs - Embedding](../backend/openai_api_embeddings.ipynb) and [Encode (embedding model)](../backend/native_api.html#Encode-(embedding-model)).\n",
12
    "- For Reward Models, see [Classify (reward model)](../backend/native_api.html#Classify-(reward-model))."
Chayenne's avatar
Chayenne committed
13
14
15
16
17
18
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
19
20
    "## Launch A Server\n",
    "\n",
Chayenne's avatar
Chayenne committed
21
22
23
24
25
26
27
    "This code block is equivalent to executing \n",
    "\n",
    "```bash\n",
    "python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\\n",
    "--port 30000 --host 0.0.0.0\n",
    "```\n",
    "\n",
Chayenne's avatar
Chayenne committed
28
    "in your terminal and wait for the server to be ready. Once the server is running, you can send test requests using curl or requests. The server implements the [OpenAI-compatible APIs](https://platform.openai.com/docs/api-reference/chat)."
Chayenne's avatar
Chayenne committed
29
30
31
32
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
33
   "execution_count": null,
Chayenne's avatar
Chayenne committed
34
35
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
36
37
38
39
     "iopub.execute_input": "2024-11-07T18:48:52.032229Z",
     "iopub.status.busy": "2024-11-07T18:48:52.032105Z",
     "iopub.status.idle": "2024-11-07T18:49:20.226042Z",
     "shell.execute_reply": "2024-11-07T18:49:20.225562Z"
Chayenne's avatar
Chayenne committed
40
41
    }
   },
Chayenne's avatar
Chayenne committed
42
   "outputs": [],
Chayenne's avatar
Chayenne committed
43
44
45
46
47
48
49
50
51
   "source": [
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
    "\n",
    "server_process = execute_shell_command(\n",
Chayenne's avatar
Chayenne committed
52
    "    \"\"\"\n",
Chayenne's avatar
Chayenne committed
53
54
55
56
57
58
59
60
61
62
63
64
    "python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\\n",
    "--port 30000 --host 0.0.0.0\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "wait_for_server(\"http://localhost:30000\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
65
66
67
68
69
70
    "## Using cURL\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
71
72
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
73
74
75
76
     "iopub.execute_input": "2024-11-07T18:49:20.228006Z",
     "iopub.status.busy": "2024-11-07T18:49:20.227572Z",
     "iopub.status.idle": "2024-11-07T18:49:20.469885Z",
     "shell.execute_reply": "2024-11-07T18:49:20.469518Z"
77
78
    }
   },
Chayenne's avatar
Chayenne committed
79
80
81
   "outputs": [],
   "source": [
    "import subprocess, json\n",
Chayenne's avatar
Chayenne committed
82
    "\n",
Chayenne's avatar
Chayenne committed
83
84
    "curl_command = \"\"\"\n",
    "curl -s http://localhost:30000/v1/chat/completions \\\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
85
    "  -d '{\"model\": \"meta-llama/Meta-Llama-3.1-8B-Instruct\", \"messages\": [{\"role\": \"user\", \"content\": \"What is the capital of France?\"}]}'\n",
Chayenne's avatar
Chayenne committed
86
87
88
89
90
91
92
93
94
95
    "\"\"\"\n",
    "\n",
    "response = json.loads(subprocess.check_output(curl_command, shell=True))\n",
    "print_highlight(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
96
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
97
98
99
100
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
101
   "execution_count": null,
Chayenne's avatar
Chayenne committed
102
103
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
104
105
106
107
     "iopub.execute_input": "2024-11-07T18:49:20.471956Z",
     "iopub.status.busy": "2024-11-07T18:49:20.471811Z",
     "iopub.status.idle": "2024-11-07T18:49:20.667997Z",
     "shell.execute_reply": "2024-11-07T18:49:20.667630Z"
Chayenne's avatar
Chayenne committed
108
109
    }
   },
Chayenne's avatar
Chayenne committed
110
   "outputs": [],
Chayenne's avatar
Chayenne committed
111
   "source": [
Chayenne's avatar
Chayenne committed
112
    "import requests\n",
113
    "\n",
Chayenne's avatar
Chayenne committed
114
115
116
    "url = \"http://localhost:30000/v1/chat/completions\"\n",
    "\n",
    "data = {\n",
117
    "    \"model\": \"meta-llama/Meta-Llama-3.1-8B-Instruct\",\n",
Chayenne's avatar
Chayenne committed
118
    "    \"messages\": [{\"role\": \"user\", \"content\": \"What is the capital of France?\"}],\n",
Chayenne's avatar
Chayenne committed
119
    "}\n",
120
    "\n",
Chayenne's avatar
Chayenne committed
121
122
    "response = requests.post(url, json=data)\n",
    "print_highlight(response.json())"
Chayenne's avatar
Chayenne committed
123
124
125
126
127
128
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
129
    "## Using OpenAI Python Client"
Chayenne's avatar
Chayenne committed
130
131
132
133
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
134
   "execution_count": null,
Chayenne's avatar
Chayenne committed
135
136
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
137
138
139
140
     "iopub.execute_input": "2024-11-07T18:49:20.669977Z",
     "iopub.status.busy": "2024-11-07T18:49:20.669826Z",
     "iopub.status.idle": "2024-11-07T18:49:22.004855Z",
     "shell.execute_reply": "2024-11-07T18:49:22.004472Z"
Chayenne's avatar
Chayenne committed
141
142
    }
   },
Chayenne's avatar
Chayenne committed
143
   "outputs": [],
Chayenne's avatar
Chayenne committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
   "source": [
    "import openai\n",
    "\n",
    "client = openai.Client(base_url=\"http://127.0.0.1:30000/v1\", api_key=\"None\")\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"List 3 countries and their capitals.\"},\n",
    "    ],\n",
    "    temperature=0,\n",
    "    max_tokens=64,\n",
    ")\n",
    "print_highlight(response)"
   ]
  },
Lianmin Zheng's avatar
Lianmin Zheng committed
160
161
162
163
164
165
166
167
168
169
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Streaming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
170
171
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
172
173
174
175
     "iopub.execute_input": "2024-11-07T18:49:22.006983Z",
     "iopub.status.busy": "2024-11-07T18:49:22.006858Z",
     "iopub.status.idle": "2024-11-07T18:49:23.029098Z",
     "shell.execute_reply": "2024-11-07T18:49:23.028697Z"
176
177
    }
   },
Lianmin Zheng's avatar
Lianmin Zheng committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "client = openai.Client(base_url=\"http://127.0.0.1:30000/v1\", api_key=\"None\")\n",
    "\n",
    "# Use stream=True for streaming responses\n",
    "response = client.chat.completions.create(\n",
    "    model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"List 3 countries and their capitals.\"},\n",
    "    ],\n",
    "    temperature=0,\n",
    "    max_tokens=64,\n",
    "    stream=True,\n",
    ")\n",
    "\n",
    "# Handle the streaming output\n",
    "for chunk in response:\n",
    "    if chunk.choices[0].delta.content:\n",
Chayenne's avatar
Chayenne committed
198
    "        print(chunk.choices[0].delta.content, end=\"\", flush=True)"
Lianmin Zheng's avatar
Lianmin Zheng committed
199
200
   ]
  },
201
202
203
204
205
206
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Native Generation APIs\n",
    "\n",
207
    "You can also use the native `/generate` endpoint with requests, which provides more flexiblity. An API reference is available at [Sampling Parameters](../references/sampling_params.md)."
208
209
210
211
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
212
   "execution_count": null,
213
214
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
215
216
217
218
     "iopub.execute_input": "2024-11-07T18:49:23.031712Z",
     "iopub.status.busy": "2024-11-07T18:49:23.031571Z",
     "iopub.status.idle": "2024-11-07T18:49:23.787752Z",
     "shell.execute_reply": "2024-11-07T18:49:23.787368Z"
219
220
    }
   },
Chayenne's avatar
Chayenne committed
221
   "outputs": [],
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
   "source": [
    "import requests\n",
    "\n",
    "response = requests.post(\n",
    "    \"http://localhost:30000/generate\",\n",
    "    json={\n",
    "        \"text\": \"The capital of France is\",\n",
    "        \"sampling_params\": {\n",
    "            \"temperature\": 0,\n",
    "            \"max_new_tokens\": 32,\n",
    "        },\n",
    "    },\n",
    ")\n",
    "\n",
    "print_highlight(response.json())"
   ]
  },
Lianmin Zheng's avatar
Lianmin Zheng committed
239
240
241
242
243
244
245
246
247
248
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Streaming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
249
250
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
251
252
253
254
     "iopub.execute_input": "2024-11-07T18:49:23.789840Z",
     "iopub.status.busy": "2024-11-07T18:49:23.789702Z",
     "iopub.status.idle": "2024-11-07T18:49:24.545631Z",
     "shell.execute_reply": "2024-11-07T18:49:24.545241Z"
255
256
    }
   },
Lianmin Zheng's avatar
Lianmin Zheng committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "response = requests.post(\n",
    "    \"http://localhost:30000/generate\",\n",
    "    json={\n",
    "        \"text\": \"The capital of France is\",\n",
    "        \"sampling_params\": {\n",
    "            \"temperature\": 0,\n",
    "            \"max_new_tokens\": 32,\n",
    "        },\n",
    "        \"stream\": True,\n",
    "    },\n",
    "    stream=True,\n",
    ")\n",
    "\n",
    "prev = 0\n",
    "for chunk in response.iter_lines(decode_unicode=False):\n",
    "    chunk = chunk.decode(\"utf-8\")\n",
    "    if chunk and chunk.startswith(\"data:\"):\n",
    "        if chunk == \"data: [DONE]\":\n",
    "            break\n",
    "        data = json.loads(chunk[5:].strip(\"\\n\"))\n",
    "        output = data[\"text\"]\n",
    "        print(output[prev:], end=\"\", flush=True)\n",
    "        prev = len(output)"
   ]
  },
Chayenne's avatar
Chayenne committed
286
287
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
288
   "execution_count": 8,
Chayenne's avatar
Chayenne committed
289
290
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
291
292
293
294
     "iopub.execute_input": "2024-11-07T18:49:24.547641Z",
     "iopub.status.busy": "2024-11-07T18:49:24.547497Z",
     "iopub.status.idle": "2024-11-07T18:49:25.888864Z",
     "shell.execute_reply": "2024-11-07T18:49:25.888114Z"
Chayenne's avatar
Chayenne committed
295
296
297
298
299
300
301
302
303
304
305
306
307
    }
   },
   "outputs": [],
   "source": [
    "terminate_process(server_process)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
Chayenne's avatar
Chayenne committed
308
309
310
311
312
313
314
315
316
317
318
319
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
Chayenne's avatar
Chayenne committed
320
321
322
323
324
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}