send_request.ipynb 6.12 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
12
13
14
15
16
17
18
19
20
21
22
23
24
    "\n",
    "This notebook provides a quick-start guide for using SGLang after installation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Launch a server\n",
    "\n",
    "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
25
    "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 API](https://platform.openai.com/docs/api-reference/chat)."
Chayenne's avatar
Chayenne committed
26
27
28
29
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
30
   "execution_count": null,
Chayenne's avatar
Chayenne committed
31
32
33
34
35
36
37
38
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:46:13.611212Z",
     "iopub.status.busy": "2024-11-01T02:46:13.611093Z",
     "iopub.status.idle": "2024-11-01T02:46:42.810261Z",
     "shell.execute_reply": "2024-11-01T02:46:42.809147Z"
    }
   },
Chayenne's avatar
Chayenne committed
39
   "outputs": [],
Chayenne's avatar
Chayenne committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
   "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",
    "\"\"\"\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",
    "\n",
    "wait_for_server(\"http://localhost:30000\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Chayenne's avatar
Chayenne committed
62
63
64
65
66
67
68
69
70
71
    "## Using cURL\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess, json\n",
Chayenne's avatar
Chayenne committed
72
    "\n",
Chayenne's avatar
Chayenne committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    "curl_command = \"\"\"\n",
    "curl -s http://localhost:30000/v1/chat/completions \\\n",
    "  -d '{\"model\": \"meta-llama/Meta-Llama-3.1-8B-Instruct\", \"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"What is a LLM?\"}]}'\n",
    "\"\"\"\n",
    "\n",
    "response = json.loads(subprocess.check_output(curl_command, shell=True))\n",
    "print_highlight(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using OpenAI Compatible API w/ Requests"
Chayenne's avatar
Chayenne committed
87
88
89
90
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
91
   "execution_count": null,
Chayenne's avatar
Chayenne committed
92
93
94
95
96
97
98
99
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:46:42.813656Z",
     "iopub.status.busy": "2024-11-01T02:46:42.813354Z",
     "iopub.status.idle": "2024-11-01T02:46:51.436613Z",
     "shell.execute_reply": "2024-11-01T02:46:51.435965Z"
    }
   },
Chayenne's avatar
Chayenne committed
100
   "outputs": [],
Chayenne's avatar
Chayenne committed
101
   "source": [
Chayenne's avatar
Chayenne committed
102
    "import requests\n",
103
    "\n",
Chayenne's avatar
Chayenne committed
104
105
106
    "url = \"http://localhost:30000/v1/chat/completions\"\n",
    "\n",
    "data = {\n",
107
108
    "    \"model\": \"meta-llama/Meta-Llama-3.1-8B-Instruct\",\n",
    "    \"messages\": [\n",
Chayenne's avatar
Chayenne committed
109
110
    "        {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
    "        {\"role\": \"user\", \"content\": \"What is a LLM?\"}\n",
111
    "    ]\n",
Chayenne's avatar
Chayenne committed
112
    "}\n",
113
    "\n",
Chayenne's avatar
Chayenne committed
114
115
    "response = requests.post(url, json=data)\n",
    "print_highlight(response.json())"
Chayenne's avatar
Chayenne committed
116
117
118
119
120
121
122
123
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using OpenAI Python Client\n",
    "\n",
Chayenne's avatar
Chayenne committed
124
    "You can also use the OpenAI Python API library to send requests."
Chayenne's avatar
Chayenne committed
125
126
127
128
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
129
   "execution_count": null,
Chayenne's avatar
Chayenne committed
130
131
132
133
134
135
136
137
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:46:51.439372Z",
     "iopub.status.busy": "2024-11-01T02:46:51.439178Z",
     "iopub.status.idle": "2024-11-01T02:46:52.895776Z",
     "shell.execute_reply": "2024-11-01T02:46:52.895318Z"
    }
   },
Chayenne's avatar
Chayenne committed
138
   "outputs": [],
Chayenne's avatar
Chayenne committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
   "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\": \"system\", \"content\": \"You are a helpful AI assistant\"},\n",
    "        {\"role\": \"user\", \"content\": \"List 3 countries and their capitals.\"},\n",
    "    ],\n",
    "    temperature=0,\n",
    "    max_tokens=64,\n",
    ")\n",
    "print_highlight(response)"
   ]
  },
156
157
158
159
160
161
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Native Generation APIs\n",
    "\n",
Chayenne's avatar
Chayenne committed
162
    "You can also use the native `/generate` endpoint with requests, which provides more flexiblity. An API reference is available at [Sampling Parameters](https://sgl-project.github.io/references/sampling_params.html)."
163
164
165
166
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
167
   "execution_count": null,
168
   "metadata": {},
Chayenne's avatar
Chayenne committed
169
   "outputs": [],
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
   "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())"
   ]
  },
Chayenne's avatar
Chayenne committed
187
188
  {
   "cell_type": "code",
189
   "execution_count": 6,
Chayenne's avatar
Chayenne committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-01T02:46:52.898411Z",
     "iopub.status.busy": "2024-11-01T02:46:52.898149Z",
     "iopub.status.idle": "2024-11-01T02:46:54.398382Z",
     "shell.execute_reply": "2024-11-01T02:46:54.397564Z"
    }
   },
   "outputs": [],
   "source": [
    "terminate_process(server_process)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
Chayenne's avatar
Chayenne committed
209
210
211
212
213
214
215
216
217
218
219
220
  },
  "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
221
222
223
224
225
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}