openai_api_vision.ipynb 8.24 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/multimodal_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."
31
32
33
34
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
35
   "execution_count": null,
36
   "metadata": {},
Chayenne's avatar
Chayenne committed
37
   "outputs": [],
38
   "source": [
39
40
41
42
43
44
45
46
    "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",
47
    "\n",
simveit's avatar
simveit committed
48
    "vision_process, port = launch_server_cmd(\n",
Chayenne's avatar
Chayenne committed
49
    "    \"\"\"\n",
50
    "python3 -m sglang.launch_server --model-path Qwen/Qwen2.5-VL-7B-Instruct\n",
51
52
53
    "\"\"\"\n",
    ")\n",
    "\n",
54
    "wait_for_server(f\"http://localhost:{port}\")"
55
56
57
58
59
60
61
62
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using cURL\n",
    "\n",
Chayenne's avatar
Chayenne committed
63
    "Once the server is up, you can send test requests using curl or requests."
64
65
66
67
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
68
   "execution_count": null,
69
   "metadata": {},
Chayenne's avatar
Chayenne committed
70
   "outputs": [],
71
72
73
   "source": [
    "import subprocess\n",
    "\n",
74
75
76
    "curl_command = f\"\"\"\n",
    "curl -s http://localhost:{port}/v1/chat/completions \\\\\n",
    "  -d '{{\n",
77
    "    \"model\": \"Qwen/Qwen2.5-VL-7B-Instruct\",\n",
78
    "    \"messages\": [\n",
79
    "      {{\n",
80
81
    "        \"role\": \"user\",\n",
    "        \"content\": [\n",
82
    "          {{\n",
83
84
    "            \"type\": \"text\",\n",
    "            \"text\": \"What’s in this image?\"\n",
85
86
    "          }},\n",
    "          {{\n",
87
    "            \"type\": \"image_url\",\n",
88
    "            \"image_url\": {{\n",
89
    "              \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
90
91
    "            }}\n",
    "          }}\n",
92
    "        ]\n",
93
    "      }}\n",
94
95
    "    ],\n",
    "    \"max_tokens\": 300\n",
96
    "  }}'\n",
97
98
99
    "\"\"\"\n",
    "\n",
    "response = subprocess.check_output(curl_command, shell=True).decode()\n",
100
101
102
103
    "print_highlight(response)\n",
    "\n",
    "\n",
    "response = subprocess.check_output(curl_command, shell=True).decode()\n",
104
105
106
    "print_highlight(response)"
   ]
  },
Chayenne's avatar
Chayenne committed
107
108
109
110
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
111
    "## Using Python Requests"
Chayenne's avatar
Chayenne committed
112
113
114
115
116
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
117
   "metadata": {},
Chayenne's avatar
Chayenne committed
118
119
120
121
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
122
    "url = f\"http://localhost:{port}/v1/chat/completions\"\n",
Chayenne's avatar
Chayenne committed
123
124
    "\n",
    "data = {\n",
125
    "    \"model\": \"Qwen/Qwen2.5-VL-7B-Instruct\",\n",
Chayenne's avatar
Chayenne committed
126
127
128
129
    "    \"messages\": [\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": [\n",
Chayenne's avatar
Chayenne committed
130
    "                {\"type\": \"text\", \"text\": \"What’s in this image?\"},\n",
Chayenne's avatar
Chayenne committed
131
132
133
134
    "                {\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
135
136
137
    "                    },\n",
    "                },\n",
    "            ],\n",
Chayenne's avatar
Chayenne committed
138
139
    "        }\n",
    "    ],\n",
Chayenne's avatar
Chayenne committed
140
    "    \"max_tokens\": 300,\n",
Chayenne's avatar
Chayenne committed
141
142
143
144
145
146
    "}\n",
    "\n",
    "response = requests.post(url, json=data)\n",
    "print_highlight(response.text)"
   ]
  },
147
148
149
150
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Lianmin Zheng's avatar
Lianmin Zheng committed
151
    "## Using OpenAI Python Client"
152
153
154
155
   ]
  },
  {
   "cell_type": "code",
Chayenne's avatar
Chayenne committed
156
   "execution_count": null,
157
   "metadata": {},
Chayenne's avatar
Chayenne committed
158
   "outputs": [],
159
160
161
   "source": [
    "from openai import OpenAI\n",
    "\n",
162
    "client = OpenAI(base_url=f\"http://localhost:{port}/v1\", api_key=\"None\")\n",
163
164
    "\n",
    "response = client.chat.completions.create(\n",
165
    "    model=\"Qwen/Qwen2.5-VL-7B-Instruct\",\n",
166
167
168
169
170
171
172
173
174
175
    "    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
176
177
178
    "                    \"image_url\": {\n",
    "                        \"url\": \"https://github.com/sgl-project/sglang/blob/main/test/lang/example_image.png?raw=true\"\n",
    "                    },\n",
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    "                },\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
200
   "execution_count": null,
201
   "metadata": {},
Chayenne's avatar
Chayenne committed
202
   "outputs": [],
203
204
205
   "source": [
    "from openai import OpenAI\n",
    "\n",
206
    "client = OpenAI(base_url=f\"http://localhost:{port}/v1\", api_key=\"None\")\n",
207
208
    "\n",
    "response = client.chat.completions.create(\n",
209
    "    model=\"Qwen/Qwen2.5-VL-7B-Instruct\",\n",
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    "    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
229
    "                    \"Please describe the first image in one sentence, and then describe the second image in another sentence.\",\n",
230
231
232
233
234
235
236
237
238
239
240
241
    "                },\n",
    "            ],\n",
    "        }\n",
    "    ],\n",
    "    temperature=0,\n",
    ")\n",
    "\n",
    "print_highlight(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
242
243
   "execution_count": null,
   "metadata": {},
244
245
   "outputs": [],
   "source": [
simveit's avatar
simveit committed
246
    "terminate_process(vision_process)"
247
248
249
250
   ]
  }
 ],
 "metadata": {
Chayenne's avatar
Chayenne committed
251
252
253
254
255
256
257
258
259
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
260
   "pygments_lexer": "ipython3"
261
262
263
264
265
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}