"tests/others/test_config.py" did not exist on "95f4256fc905b6e29e5ea0f245dcf88f72a9ddd1"
openai_api_vision.ipynb 10.7 KB
Newer Older
1
2
3
4
5
6
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
7
    "# OpenAI APIs - Vision\n",
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    "\n",
    "SGLang provides OpenAI-compatible APIs to enable a smooth transition from OpenAI services to self-hosted local models.\n",
    "A complete reference for the API is available in the [OpenAI API Reference](https://platform.openai.com/docs/guides/vision).\n",
    "This tutorial covers the vision APIs for vision language models.\n",
    "\n",
    "SGLang supports vision language models such as Llama 3.2, LLaVA-OneVision, and QWen-VL2  \n",
    "- [meta-llama/Llama-3.2-11B-Vision-Instruct](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct)  \n",
    "- [lmms-lab/llava-onevision-qwen2-72b-ov-chat](https://huggingface.co/lmms-lab/llava-onevision-qwen2-72b-ov-chat)  \n",
    "- [Qwen/Qwen2-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Launch A Server\n",
    "\n",
    "This code block is equivalent to executing \n",
    "\n",
    "```bash\n",
    "python3 -m sglang.launch_server --model-path meta-llama/Llama-3.2-11B-Vision-Instruct \\\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
29
    "  --port 30000 --chat-template llama_3_vision\n",
30
31
32
33
34
35
36
37
38
    "```\n",
    "in your terminal and wait for the server to be ready.\n",
    "\n",
    "Remember to add `--chat-template llama_3_vision` to specify the vision chat template, otherwise the server only supports text.\n",
    "We need to specify `--chat-template` for vision language models because the chat template provided in Hugging Face tokenizer only supports text."
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
39
   "execution_count": null,
40
41
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
42
43
44
45
     "iopub.execute_input": "2024-11-07T18:43:47.311708Z",
     "iopub.status.busy": "2024-11-07T18:43:47.311517Z",
     "iopub.status.idle": "2024-11-07T18:44:18.512576Z",
     "shell.execute_reply": "2024-11-07T18:44:18.511909Z"
46
47
    }
   },
Chayenne's avatar
Chayenne committed
48
   "outputs": [],
49
50
51
52
53
54
55
56
57
   "source": [
    "from sglang.utils import (\n",
    "    execute_shell_command,\n",
    "    wait_for_server,\n",
    "    terminate_process,\n",
    "    print_highlight,\n",
    ")\n",
    "\n",
    "embedding_process = execute_shell_command(\n",
Chayenne's avatar
Chayenne committed
58
    "    \"\"\"\n",
59
    "python3 -m sglang.launch_server --model-path meta-llama/Llama-3.2-11B-Vision-Instruct \\\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
60
    "    --port=30000 --chat-template=llama_3_vision\n",
61
62
63
    "\"\"\"\n",
    ")\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
64
    "wait_for_server(\"http://localhost:30000\")"
65
66
67
68
69
70
71
72
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using cURL\n",
    "\n",
Chayenne's avatar
Chayenne committed
73
    "Once the server is up, you can send test requests using curl or requests."
74
75
76
77
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
78
   "execution_count": null,
79
80
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
81
82
83
84
     "iopub.execute_input": "2024-11-07T18:44:18.515678Z",
     "iopub.status.busy": "2024-11-07T18:44:18.515314Z",
     "iopub.status.idle": "2024-11-07T18:44:22.880793Z",
     "shell.execute_reply": "2024-11-07T18:44:22.880303Z"
85
86
    }
   },
Chayenne's avatar
Chayenne committed
87
   "outputs": [],
88
89
90
91
   "source": [
    "import subprocess\n",
    "\n",
    "curl_command = \"\"\"\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
92
    "curl -s http://localhost:30000/v1/chat/completions \\\n",
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    "  -d '{\n",
    "    \"model\": \"meta-llama/Llama-3.2-11B-Vision-Instruct\",\n",
    "    \"messages\": [\n",
    "      {\n",
    "        \"role\": \"user\",\n",
    "        \"content\": [\n",
    "          {\n",
    "            \"type\": \"text\",\n",
    "            \"text\": \"What’s in this image?\"\n",
    "          },\n",
    "          {\n",
    "            \"type\": \"image_url\",\n",
    "            \"image_url\": {\n",
    "              \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
    "            }\n",
    "          }\n",
    "        ]\n",
    "      }\n",
    "    ],\n",
    "    \"max_tokens\": 300\n",
    "  }'\n",
    "\"\"\"\n",
    "\n",
    "response = subprocess.check_output(curl_command, shell=True).decode()\n",
    "print_highlight(response)"
   ]
  },
Chayenne's avatar
Chayenne committed
120
121
122
123
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
124
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
125
126
127
128
129
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
130
131
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
132
133
134
135
     "iopub.execute_input": "2024-11-07T18:44:22.883309Z",
     "iopub.status.busy": "2024-11-07T18:44:22.883160Z",
     "iopub.status.idle": "2024-11-07T18:44:27.048810Z",
     "shell.execute_reply": "2024-11-07T18:44:27.048074Z"
136
137
    }
   },
Chayenne's avatar
Chayenne committed
138
139
140
141
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
142
    "url = \"http://localhost:30000/v1/chat/completions\"\n",
Chayenne's avatar
Chayenne committed
143
144
145
146
147
148
149
    "\n",
    "data = {\n",
    "    \"model\": \"meta-llama/Llama-3.2-11B-Vision-Instruct\",\n",
    "    \"messages\": [\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
Chayenne's avatar
Chayenne committed
150
    "                {\"type\": \"text\", \"text\": \"What’s in this image?\"},\n",
Chayenne's avatar
Chayenne committed
151
152
153
154
    "                {\n",
    "                    \"type\": \"image_url\",\n",
    "                    \"image_url\": {\n",
    "                        \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
Chayenne's avatar
Chayenne committed
155
156
157
    "                    },\n",
    "                },\n",
    "            ],\n",
Chayenne's avatar
Chayenne committed
158
159
    "        }\n",
    "    ],\n",
Chayenne's avatar
Chayenne committed
160
    "    \"max_tokens\": 300,\n",
Chayenne's avatar
Chayenne committed
161
162
163
164
165
166
    "}\n",
    "\n",
    "response = requests.post(url, json=data)\n",
    "print_highlight(response.text)"
   ]
  },
167
168
169
170
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
171
    "## Using OpenAI Python Client"
172
173
174
175
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
176
   "execution_count": null,
177
178
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
179
180
181
182
     "iopub.execute_input": "2024-11-07T18:44:27.051312Z",
     "iopub.status.busy": "2024-11-07T18:44:27.051190Z",
     "iopub.status.idle": "2024-11-07T18:44:32.358097Z",
     "shell.execute_reply": "2024-11-07T18:44:32.357628Z"
183
184
    }
   },
Chayenne's avatar
Chayenne committed
185
   "outputs": [],
186
187
188
   "source": [
    "from openai import OpenAI\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
189
    "client = OpenAI(base_url=\"http://localhost:30000/v1\", api_key=\"None\")\n",
190
191
192
193
194
195
196
197
198
199
200
201
202
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=\"meta-llama/Llama-3.2-11B-Vision-Instruct\",\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
    "                {\n",
    "                    \"type\": \"text\",\n",
    "                    \"text\": \"What is in this image?\",\n",
    "                },\n",
    "                {\n",
    "                    \"type\": \"image_url\",\n",
Chayenne's avatar
Chayenne committed
203
204
205
    "                    \"image_url\": {\n",
    "                        \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
    "                    },\n",
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
    "                },\n",
    "            ],\n",
    "        }\n",
    "    ],\n",
    "    max_tokens=300,\n",
    ")\n",
    "\n",
    "print_highlight(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple-Image Inputs\n",
    "\n",
    "The server also supports multiple images and interleaved text and images if the model supports it."
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
227
   "execution_count": null,
228
229
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
230
231
232
233
     "iopub.execute_input": "2024-11-07T18:44:32.359532Z",
     "iopub.status.busy": "2024-11-07T18:44:32.359413Z",
     "iopub.status.idle": "2024-11-07T18:44:36.164664Z",
     "shell.execute_reply": "2024-11-07T18:44:36.164005Z"
234
235
    }
   },
Chayenne's avatar
Chayenne committed
236
   "outputs": [],
237
238
239
   "source": [
    "from openai import OpenAI\n",
    "\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
240
    "client = OpenAI(base_url=\"http://localhost:30000/v1\", api_key=\"None\")\n",
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=\"meta-llama/Llama-3.2-11B-Vision-Instruct\",\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
    "                {\n",
    "                    \"type\": \"image_url\",\n",
    "                    \"image_url\": {\n",
    "                        \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\",\n",
    "                    },\n",
    "                },\n",
    "                {\n",
    "                    \"type\": \"image_url\",\n",
    "                    \"image_url\": {\n",
    "                        \"url\": \"https://raw.githubusercontent.com/sgl-project/sglang/main/assets/logo.png\",\n",
    "                    },\n",
    "                },\n",
    "                {\n",
    "                    \"type\": \"text\",\n",
    "                    \"text\": \"I have two very different images. They are not related at all. \"\n",
Chayenne's avatar
Chayenne committed
263
    "                    \"Please describe the first image in one sentence, and then describe the second image in another sentence.\",\n",
264
265
266
267
268
269
270
271
272
273
274
275
    "                },\n",
    "            ],\n",
    "        }\n",
    "    ],\n",
    "    temperature=0,\n",
    ")\n",
    "\n",
    "print_highlight(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
276
277
278
   "execution_count": 6,
   "metadata": {
    "execution": {
Chayenne's avatar
Chayenne committed
279
280
281
282
     "iopub.execute_input": "2024-11-07T18:44:36.167123Z",
     "iopub.status.busy": "2024-11-07T18:44:36.166535Z",
     "iopub.status.idle": "2024-11-07T18:44:37.743761Z",
     "shell.execute_reply": "2024-11-07T18:44:37.742510Z"
283
284
    }
   },
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
   "outputs": [],
   "source": [
    "terminate_process(embedding_process)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat Template\n",
    "\n",
    "As mentioned before, if you do not specify a vision model's `--chat-template`, the server uses Hugging Face's default template, which only supports text.\n",
    "\n",
    "We list popular vision models with their chat templates:\n",
    "\n",
    "- [meta-llama/Llama-3.2-Vision](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct) uses `llama_3_vision`.\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
301
    "- [Qwen/Qwen2-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct) uses `qwen2-vl`.\n",
302
    "- [LlaVA-OneVision](https://huggingface.co/lmms-lab/llava-onevision-qwen2-7b-ov) uses `chatml-llava`.\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
303
    "- [LLaVA-NeXT](https://huggingface.co/collections/lmms-lab/llava-next-6623288e2d61edba3ddbf5ff) uses `chatml-llava`.\n",
304
305
306
307
308
309
310
311
312
313
    "- [Llama3-LLaVA-NeXT](https://huggingface.co/lmms-lab/llama3-llava-next-8b) uses `llava_llama_3`.\n",
    "- [LLaVA-v1.5 / 1.6](https://huggingface.co/liuhaotian/llava-v1.6-34b) uses `vicuna_v1.1`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
Chayenne's avatar
Chayenne committed
314
315
316
317
318
319
320
321
322
323
324
325
  },
  "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"
326
327
328
329
330
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}