Skip to content

Commit 78aeccf

Browse files
authored
fix openai api (#1367)
1 parent d7f023a commit 78aeccf

File tree

13 files changed

+367
-140
lines changed

13 files changed

+367
-140
lines changed

docs/source/LLM/VLLM推理加速与部署.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ print(f'model_type: {model_type}')
294294

295295
query = '浙江的省会在哪里?'
296296
request_config = XRequestConfig(seed=42)
297-
resp = asyncio.run(inference_client_async(model_type, query, request_config=request_config))
297+
tasks = [inference_client_async(model_type, query, request_config=request_config) for _ in range(5)]
298+
async def _batch_run(tasks):
299+
return await asyncio.gather(*tasks)
300+
301+
resp_list = asyncio.run(_batch_run(tasks))
302+
resp = resp_list[0]
298303
response = resp.choices[0].message.content
299304
print(f'query: {query}')
300305
print(f'response: {response}')
@@ -317,7 +322,7 @@ model_type: qwen-7b-chat
317322
query: 浙江的省会在哪里?
318323
response: 浙江省的省会是杭州市。
319324
query: 这有什么好吃的?
320-
response: 杭州有许多美食,例如西湖醋鱼、东坡肉、龙井虾仁、叫化童子鸡等。此外,杭州还有许多特色小吃,如西湖藕粉、杭州小笼包、杭州油条等
325+
response: 浙江省有很多美食,比如杭州菜、宁波菜、绍兴菜、温州菜等。其中,杭州菜以清淡、鲜美、精致著称,而宁波菜则以鲜美、醇厚、香辣著称。此外,浙江省还有许多特色小吃,比如杭州的西湖醋鱼、宁波的汤圆、绍兴的酒酿圆子等
321326
"""
322327
```
323328

docs/source/Multi-Modal/MLLM部署文档.md

Lines changed: 214 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ response: <ref>花</ref><box>(34,449),(368,981)</box><box>(342,456),(670,917)</b
9797
"""
9898
```
9999

100-
使用openai:
100+
使用openai(方案1):
101101
```python
102102
from openai import OpenAI
103103
client = OpenAI(
@@ -162,6 +162,81 @@ response: <ref>花</ref><box>(34,449),(368,981)</box><box>(342,456),(670,917)</b
162162
"""
163163
```
164164

165+
使用openai(方案2):
166+
```python
167+
from openai import OpenAI
168+
client = OpenAI(
169+
api_key='EMPTY',
170+
base_url='http://localhost:8000/v1',
171+
)
172+
model_type = client.models.list().data[0].id
173+
print(f'model_type: {model_type}')
174+
175+
# use base64
176+
# import base64
177+
# with open('rose.jpg', 'rb') as f:
178+
# img_base64 = base64.b64encode(f.read()).decode('utf-8')
179+
# image_url = f'data:image/jpeg;base64,{img_base64}'
180+
181+
# use local_path
182+
# from swift.llm import convert_to_base64
183+
# image_url = convert_to_base64(images=['rose.jpg'])['images'][0]
184+
# image_url = f'data:image/jpeg;base64,{image_url}'
185+
186+
# use url
187+
image_url = 'https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/rose.jpg'
188+
189+
query = '图中是什么花,有几只?'
190+
messages = [{
191+
'role': 'user',
192+
'content': [
193+
{'type': 'text', 'text': 'Picture 1:'},
194+
{'type': 'image_url', 'image_url': {'url': image_url}},
195+
{'type': 'text', 'text': f'\n{query}'},
196+
]
197+
}]
198+
resp = client.chat.completions.create(
199+
model=model_type,
200+
messages=messages,
201+
seed=42)
202+
response = resp.choices[0].message.content
203+
print(f'query: {query}')
204+
print(f'response: {response}')
205+
206+
# 流式
207+
messages.append({'role': 'assistant', 'content': response})
208+
query = '框出图中的花'
209+
messages.append({'role': 'user', 'content': query})
210+
stream_resp = client.chat.completions.create(
211+
model=model_type,
212+
messages=messages,
213+
stream=True,
214+
seed=42)
215+
216+
print(f'query: {query}')
217+
print('response: ', end='')
218+
for chunk in stream_resp:
219+
print(chunk.choices[0].delta.content, end='', flush=True)
220+
print()
221+
"""Out[0]
222+
model_type: qwen-vl-chat
223+
query: Picture 1:<img>https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/rose.jpg</img>
224+
图中是什么花,有几只?
225+
response: 图中是三朵红玫瑰花。
226+
query: 框出图中的花
227+
response: <ref>花</ref><box>(34,449),(368,981)</box><box>(342,456),(670,917)</box><box>(585,508),(859,977)</box>
228+
"""
229+
230+
"""Out[0]
231+
model_type: qwen-vl-chat
232+
query: Picture 1:<img>https://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/rose.jpg</img>
233+
图中是什么花,有几只?
234+
response: 图中是三朵红玫瑰花。
235+
query: 框出图中的花
236+
response: <ref>花</ref><box>(34,449),(368,981)</box><box>(342,456),(670,917)</box><box>(585,508),(859,977)</box>
237+
"""
238+
```
239+
165240
## yi-vl-6b-chat
166241

167242
**服务端:**
@@ -233,13 +308,13 @@ print()
233308
"""
234309
model_type: yi-vl-6b-chat
235310
query: 描述这张图片
236-
response: 图片显示一只小猫坐在地板上,眼睛睁开,凝视着前方。小猫有灰色和白色的毛皮,有黑色和白色的条纹。它的毛皮在图片中有些混乱,给它一个可爱和迷人的外观。小猫似乎正在注意周围环境,可能正在寻找一些东西或只是观察它的环境
311+
response: 图片显示一只小猫坐在地板上,眼睛睁开,凝视着摄像机。小猫看起来很可爱,有灰色和白色的毛皮,以及蓝色的眼睛。小猫似乎正在看摄像机,可能被吸引到它正在被拍摄
237312
query: 图中有几只羊
238-
response: 图中有四只羊.
313+
response: 图中有四只羊
239314
"""
240315
```
241316

242-
使用openai:
317+
使用openai(方案1):
243318
```python
244319
from openai import OpenAI
245320
client = OpenAI(
@@ -298,9 +373,76 @@ print()
298373
"""
299374
model_type: yi-vl-6b-chat
300375
query: 描述这张图片
301-
response: 图片显示一只小猫坐在地板上,眼睛睁开,凝视着前方。小猫有灰色和白色的毛皮,有黑色和白色的条纹。它的毛皮在图片中有些混乱,给它一个可爱和迷人的外观。小猫似乎正在注意周围环境,可能正在寻找一些东西或只是观察它的环境
376+
response: 图片显示一只小猫坐在地板上,眼睛睁开,凝视着摄像机。小猫看起来很可爱,有灰色和白色的毛皮,以及蓝色的眼睛。小猫似乎正在看摄像机,可能被吸引到它正在被拍摄
302377
query: 图中有几只羊
303-
response: 图中有四只羊.
378+
response: 图中有四只羊。
379+
"""
380+
```
381+
382+
使用openai(方案2):
383+
```python
384+
from openai import OpenAI
385+
client = OpenAI(
386+
api_key='EMPTY',
387+
base_url='http://localhost:8000/v1',
388+
)
389+
model_type = client.models.list().data[0].id
390+
print(f'model_type: {model_type}')
391+
392+
# use base64
393+
# import base64
394+
# with open('cat.png', 'rb') as f:
395+
# img_base64 = base64.b64encode(f.read()).decode('utf-8')
396+
# image_url = f'data:image/jpeg;base64,{img_base64}'
397+
398+
# use local_path
399+
# from swift.llm import convert_to_base64
400+
# image_url = convert_to_base64(images=['cat.png'])['images'][0]
401+
# image_url = f'data:image/jpeg;base64,{image_url}'
402+
403+
# use url
404+
image_url = 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png'
405+
406+
query = '描述这张图片'
407+
messages = [{
408+
'role': 'user',
409+
'content': [
410+
{'type': 'text', 'text': query},
411+
{'type': 'image_url', 'image_url': {'url': image_url}},
412+
]
413+
}]
414+
resp = client.chat.completions.create(
415+
model=model_type,
416+
messages=messages,
417+
temperature=0)
418+
response = resp.choices[0].message.content
419+
print(f'query: {query}')
420+
print(f'response: {response}')
421+
422+
# 流式
423+
messages.append({'role': 'assistant', 'content': response})
424+
query = '图中有几只羊'
425+
messages.append({'role': 'user', 'content': [
426+
{'type': 'text', 'text': query},
427+
{'type': 'image_url', 'image_url': {'url': 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png'}}
428+
]})
429+
stream_resp = client.chat.completions.create(
430+
model=model_type,
431+
messages=messages,
432+
stream=True,
433+
temperature=0)
434+
435+
print(f'query: {query}')
436+
print('response: ', end='')
437+
for chunk in stream_resp:
438+
print(chunk.choices[0].delta.content, end='', flush=True)
439+
print()
440+
"""
441+
model_type: yi-vl-6b-chat
442+
query: 描述这张图片
443+
response: 图片显示一只小猫坐在地板上,眼睛睁开,凝视着摄像机。小猫看起来很可爱,有灰色和白色的毛皮,以及蓝色的眼睛。小猫似乎正在看摄像机,可能被吸引到它正在被拍摄。
444+
query: 图中有几只羊
445+
response: 图中有四只羊。
304446
"""
305447
```
306448

@@ -380,7 +522,7 @@ response: 这张图片看起来是用数字绘画技术创作的。艺术家使
380522
"""
381523
```
382524

383-
使用openai:
525+
使用openai(方案1):
384526
```python
385527
from openai import OpenAI
386528
client = OpenAI(
@@ -444,6 +586,71 @@ response: 这张图片看起来是用数字绘画技术创作的。艺术家使
444586
"""
445587
```
446588

589+
使用openai(方案2):
590+
```python
591+
from openai import OpenAI
592+
client = OpenAI(
593+
api_key='EMPTY',
594+
base_url='http://localhost:8000/v1',
595+
)
596+
model_type = client.models.list().data[0].id
597+
print(f'model_type: {model_type}')
598+
599+
# use base64
600+
# import base64
601+
# with open('cat.png', 'rb') as f:
602+
# img_base64 = base64.b64encode(f.read()).decode('utf-8')
603+
# image_url = f'data:image/jpeg;base64,{img_base64}'
604+
605+
# use local_path
606+
# from swift.llm import convert_to_base64
607+
# image_url = convert_to_base64(images=['cat.png'])['images'][0]
608+
# image_url = f'data:image/jpeg;base64,{image_url}'
609+
610+
# use url
611+
image_url = 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png'
612+
613+
query = '描述这张图片'
614+
messages = [{
615+
'role': 'user',
616+
'content': [
617+
{'type': 'text', 'text': query},
618+
{'type': 'image_url', 'image_url': {'url': image_url}},
619+
]
620+
}]
621+
resp = client.chat.completions.create(
622+
model=model_type,
623+
messages=messages,
624+
temperature=0)
625+
response = resp.choices[0].message.content
626+
print(f'query: {query}')
627+
print(f'response: {response}')
628+
629+
# 流式
630+
messages.append({'role': 'assistant', 'content': response})
631+
query = '这张图是如何产生的?'
632+
messages.append({'role': 'user', 'content': query})
633+
stream_resp = client.chat.completions.create(
634+
model=model_type,
635+
messages=messages,
636+
stream=True,
637+
temperature=0)
638+
639+
print(f'query: {query}')
640+
print('response: ', end='')
641+
for chunk in stream_resp:
642+
print(chunk.choices[0].delta.content, end='', flush=True)
643+
print()
644+
645+
"""
646+
model_type: minicpm-v-v2_5-chat
647+
query: 描述这张图片
648+
response: 这张图片展示了一只年轻的猫咪的特写,可能是一只小猫,具有明显的特征。它的毛皮主要是白色的,带有灰色和黑色的条纹,尤其是在脸部周围。小猫的眼睛很大,呈蓝色,给人一种好奇和迷人的表情。耳朵尖尖,竖立着,显示出警觉。背景模糊不清,突出了小猫的特征。整体的色调柔和,猫咪的毛皮与背景的柔和色调形成对比。
649+
query: 这张图是如何产生的?
650+
response: 这张图片看起来是用数字绘画技术创作的。艺术家使用数字绘图工具来模仿毛皮的纹理和颜色,眼睛的反射,以及整体的柔和感。这种技术使艺术家能够精确地控制细节和色彩,创造出逼真的猫咪形象。
651+
"""
652+
```
653+
447654
## qwen-vl
448655

449656
**服务端:**

docs/source/Multi-Modal/vLLM推理加速文档.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,43 +142,47 @@ print(f'model_type: {model_type}')
142142
# import base64
143143
# with open('cat.png', 'rb') as f:
144144
# img_base64 = base64.b64encode(f.read()).decode('utf-8')
145-
# images = [img_base64]
145+
# image_url = f'data:image/jpeg;base64,{img_base64}'
146146

147147
# use local_path
148148
# from swift.llm import convert_to_base64
149-
# images = ['cat.png']
150-
# images = convert_to_base64(images=images)['images']
149+
# image_url = convert_to_base64(images=['cat.png'])['images'][0]
150+
# image_url = f'data:image/jpeg;base64,{image_url}'
151151

152152
# use url
153-
images = ['http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png']
153+
image_url = 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png'
154154

155155
query = 'Describe this image.'
156156
messages = [{
157157
'role': 'user',
158-
'content': query
158+
'content': [
159+
{'type': 'text', 'text': query},
160+
{'type': 'image_url', 'image_url': {'url': image_url}},
161+
]
159162
}]
163+
160164
resp = client.chat.completions.create(
161165
model=model_type,
162166
messages=messages,
163-
temperature=0,
164-
extra_body={'images': images})
167+
temperature=0)
165168
response = resp.choices[0].message.content
166169
print(f'query: {query}')
167170
print(f'response: {response}')
168171

169172
# 流式
170-
images = ['http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png']
171173
query = 'How many sheep are in the picture?'
172174
messages = [{
173175
'role': 'user',
174-
'content': query
176+
'content': [
177+
{'type': 'text', 'text': query},
178+
{'type': 'image_url', 'image_url': {'url': 'http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png'}}
179+
]
175180
}]
176181
stream_resp = client.chat.completions.create(
177182
model=model_type,
178183
messages=messages,
179184
stream=True,
180-
temperature=0,
181-
extra_body={'images': images})
185+
temperature=0)
182186

183187
print(f'query: {query}')
184188
print('response: ', end='')
@@ -188,9 +192,9 @@ print()
188192
"""
189193
model_type: llava1_6-vicuna-13b-instruct
190194
query: Describe this image.
191-
response: The image shows a close-up of a person's hands playing a guitar. The hands are positioned on the neck of the guitar, with the fingers pressing down on the strings to produce music. The guitar has a wooden body and a glossy finish, and the strings are clearly visible. The background is blurred, but it appears to be an indoor setting with warm lighting. The focus of the image is on the hands and the guitar, with the background serving to highlight the subject. There are no visible texts or distinctive brands in the image. The style of the image is a realistic photograph with a shallow depth of field, which is a common technique in portrait photography to draw attention to the subject.
195+
response: In the image, a kitten with striking blue eyes is the main subject. The kitten, with its fur in shades of gray and white, is sitting on a white surface. Its head is slightly tilted to the left, giving it a curious and endearing expression. The kitten's eyes are wide open, and its mouth is slightly open, as if it's in the middle of a meow or perhaps just finished one. The background is blurred, drawing focus to the kitten, but it appears to be a room with a window, suggesting an indoor setting. The overall image gives a sense of warmth and cuteness.
192196
query: How many sheep are in the picture?
193-
response: There are two sheep in the picture.
197+
response: There are four sheep in the picture.
194198
"""
195199
```
196200

docs/source_en/LLM/VLLM-inference-acceleration-and-deployment.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,12 @@ print(f'model_type: {model_type}')
235235
236236
query = 'Where is the capital of Zhejiang?'
237237
request_config = XRequestConfig(seed=42)
238-
resp = asyncio.run(inference_client_async(model_type, query, request_config=request_config))
238+
tasks = [inference_client_async(model_type, query, request_config=request_config) for _ in range(5)]
239+
async def _batch_run(tasks):
240+
return await asyncio.gather(*tasks)
241+
242+
resp_list = asyncio.run(_batch_run(tasks))
243+
resp = resp_list[0]
239244
response = resp.choices[0].message.content
240245
print(f'query: {query}')
241246
print(f'response: {response}')
@@ -256,9 +261,9 @@ asyncio.run(_stream())
256261
"""Out[0]
257262
model_type: qwen-7b-chat
258263
query: Where is the capital of Zhejiang?
259-
response: The capital of Zhejiang Province is Hangzhou.
264+
response: The capital of Zhejiang is Hangzhou.
260265
query: What delicious food is there?
261-
response: Hangzhou has many delicious foods, such as West Lake Vinegar Fish, Dongpo Pork, Longjing Shrimp, Beggar's Chicken, etc. In addition, Hangzhou also has many specialty snacks, such as West Lake Lotus Root Powder, Hangzhou Xiao Long Bao, Hangzhou You Tiao, etc.
266+
response: Hangzhou is famous for its delicious food, such as West Lake Fish in Vinegar Gravy, Dongpo Pork, and Longjing Tea.
262267
"""
263268
```
264269

0 commit comments

Comments
 (0)