openai_api_vision.ipynb 10.1 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
    "\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",
13
    "SGLang supports various vision language models such as Llama 3.2, LLaVA-OneVision, Qwen2.5-VL, Gemma3 and [more](https://docs.sglang.ai/supported_models/vision_language_models): \n",
14
15
    "- [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",
16
17
18
19
    "- [Qwen/Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct)\n",
    "- [google/gemma-3-4b-it](https://huggingface.co/google/gemma-3-4b-it)\n",
    "- [openbmb/MiniCPM-V](https://huggingface.co/openbmb/MiniCPM-V)\n",
    "- [deepseek-ai/deepseek-vl2](https://huggingface.co/deepseek-ai/deepseek-vl2)\n",
simveit's avatar
simveit committed
20
21
    "\n",
    "As an alternative to the OpenAI API, you can also use the [SGLang offline engine](https://github.com/sgl-project/sglang/blob/main/examples/runtime/engine/offline_batch_inference_vlm.py)."
22
23
24
25
26
27
28
29
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Launch A Server\n",
    "\n",
30
    "Launch the server in your terminal and wait for it to initialize.\n",
31
    "\n",
32
    "**Remember to add** `--chat-template llama_3_vision` **to specify the [vision chat template](https://docs.sglang.ai/backend/openai_api_vision.html#Chat-Template), otherwise, the server will only support text (images won’t be passed in), which can lead to degraded performance.**\n",
33
    "\n",
34
35
36
37
38
    "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
   "metadata": {},
Chayenne's avatar
Chayenne committed
41
   "outputs": [],
42
   "source": [
43
44
45
46
47
48
49
50
    "from sglang.test.test_utils import is_in_ci\n",
    "\n",
    "if is_in_ci():\n",
    "    from patch import launch_server_cmd\n",
    "else:\n",
    "    from sglang.utils import launch_server_cmd\n",
    "\n",
    "from sglang.utils import wait_for_server, print_highlight, terminate_process\n",
51
    "\n",
simveit's avatar
simveit committed
52
    "vision_process, port = launch_server_cmd(\n",
Chayenne's avatar
Chayenne committed
53
    "    \"\"\"\n",
54
    "python3 -m sglang.launch_server --model-path meta-llama/Llama-3.2-11B-Vision-Instruct \\\n",
55
    "    --chat-template=llama_3_vision\n",
56
57
58
    "\"\"\"\n",
    ")\n",
    "\n",
59
    "wait_for_server(f\"http://localhost:{port}\")"
60
61
62
63
64
65
66
67
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using cURL\n",
    "\n",
Chayenne's avatar
Chayenne committed
68
    "Once the server is up, you can send test requests using curl or requests."
69
70
71
72
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
73
   "execution_count": null,
74
   "metadata": {},
Chayenne's avatar
Chayenne committed
75
   "outputs": [],
76
77
78
   "source": [
    "import subprocess\n",
    "\n",
79
80
81
    "curl_command = f\"\"\"\n",
    "curl -s http://localhost:{port}/v1/chat/completions \\\\\n",
    "  -d '{{\n",
82
83
    "    \"model\": \"meta-llama/Llama-3.2-11B-Vision-Instruct\",\n",
    "    \"messages\": [\n",
84
    "      {{\n",
85
86
    "        \"role\": \"user\",\n",
    "        \"content\": [\n",
87
    "          {{\n",
88
89
    "            \"type\": \"text\",\n",
    "            \"text\": \"What’s in this image?\"\n",
90
91
    "          }},\n",
    "          {{\n",
92
    "            \"type\": \"image_url\",\n",
93
    "            \"image_url\": {{\n",
94
    "              \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
95
96
    "            }}\n",
    "          }}\n",
97
    "        ]\n",
98
    "      }}\n",
99
100
    "    ],\n",
    "    \"max_tokens\": 300\n",
101
    "  }}'\n",
102
103
104
    "\"\"\"\n",
    "\n",
    "response = subprocess.check_output(curl_command, shell=True).decode()\n",
105
106
107
108
    "print_highlight(response)\n",
    "\n",
    "\n",
    "response = subprocess.check_output(curl_command, shell=True).decode()\n",
109
110
111
    "print_highlight(response)"
   ]
  },
Chayenne's avatar
Chayenne committed
112
113
114
115
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
116
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
117
118
119
120
121
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
122
   "metadata": {},
Chayenne's avatar
Chayenne committed
123
124
125
126
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
127
    "url = f\"http://localhost:{port}/v1/chat/completions\"\n",
Chayenne's avatar
Chayenne committed
128
129
130
131
132
133
134
    "\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
135
    "                {\"type\": \"text\", \"text\": \"What’s in this image?\"},\n",
Chayenne's avatar
Chayenne committed
136
137
138
139
    "                {\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
140
141
142
    "                    },\n",
    "                },\n",
    "            ],\n",
Chayenne's avatar
Chayenne committed
143
144
    "        }\n",
    "    ],\n",
Chayenne's avatar
Chayenne committed
145
    "    \"max_tokens\": 300,\n",
Chayenne's avatar
Chayenne committed
146
147
148
149
150
151
    "}\n",
    "\n",
    "response = requests.post(url, json=data)\n",
    "print_highlight(response.text)"
   ]
  },
152
153
154
155
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
156
    "## Using OpenAI Python Client"
157
158
159
160
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
161
   "execution_count": null,
162
   "metadata": {},
Chayenne's avatar
Chayenne committed
163
   "outputs": [],
164
165
166
   "source": [
    "from openai import OpenAI\n",
    "\n",
167
    "client = OpenAI(base_url=f\"http://localhost:{port}/v1\", api_key=\"None\")\n",
168
169
170
171
172
173
174
175
176
177
178
179
180
    "\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
181
182
183
    "                    \"image_url\": {\n",
    "                        \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
    "                    },\n",
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    "                },\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
205
   "execution_count": null,
206
   "metadata": {},
Chayenne's avatar
Chayenne committed
207
   "outputs": [],
208
209
210
   "source": [
    "from openai import OpenAI\n",
    "\n",
211
    "client = OpenAI(base_url=f\"http://localhost:{port}/v1\", api_key=\"None\")\n",
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    "\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
234
    "                    \"Please describe the first image in one sentence, and then describe the second image in another sentence.\",\n",
235
236
237
238
239
240
241
242
243
244
245
246
    "                },\n",
    "            ],\n",
    "        }\n",
    "    ],\n",
    "    temperature=0,\n",
    ")\n",
    "\n",
    "print_highlight(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
247
248
   "execution_count": null,
   "metadata": {},
249
250
   "outputs": [],
   "source": [
simveit's avatar
simveit committed
251
    "terminate_process(vision_process)"
252
253
254
255
256
257
258
259
260
261
262
263
264
   ]
  },
  {
   "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",
265
266
267
268
    "- [Qwen/Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) uses `qwen2-vl`.\n",
    "- [google/gemma-3-4b-it](https://huggingface.co/google/gemma-3-4b-it) uses `gemma-it`.\n",
    "- [openbmb/MiniCPM-V](https://huggingface.co/openbmb/MiniCPM-V) uses `minicpmv`.\n",
    "- [deepseek-ai/deepseek-vl2](https://huggingface.co/deepseek-ai/deepseek-vl2) uses `deepseek-vl2`.\n",
269
    "- [LlaVA-OneVision](https://huggingface.co/lmms-lab/llava-onevision-qwen2-7b-ov) uses `chatml-llava`.\n",
Lianmin Zheng's avatar
Lianmin Zheng committed
270
    "- [LLaVA-NeXT](https://huggingface.co/collections/lmms-lab/llava-next-6623288e2d61edba3ddbf5ff) uses `chatml-llava`.\n",
271
272
273
274
275
276
    "- [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": {
Chayenne's avatar
Chayenne committed
277
278
279
280
281
282
283
284
285
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
286
   "pygments_lexer": "ipython3"
287
288
289
290
291
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}