[Misc][Doc] Add Example of using OpenAI Server with VLM (#5832)

This commit is contained in:
Roger Wang
2024-06-25 20:34:25 -07:00
committed by GitHub
parent dda4811591
commit 3aa7b6cf66
3 changed files with 101 additions and 3 deletions
+9 -3
View File
@@ -1,6 +1,7 @@
import base64
from io import BytesIO
from typing import Optional, Union
from urllib.parse import urlparse
import aiohttp
from PIL import Image
@@ -28,6 +29,10 @@ class ImageFetchAiohttp:
"""Load PIL image from a url or base64 encoded openai GPT4V format"""
if image_url.startswith('http'):
parsed_url = urlparse(image_url)
if parsed_url.scheme not in ["http", "https"]:
raise ValueError("Invalid 'image_url': A valid 'image_url' "
"must have scheme 'http' or 'https'.")
# Avoid circular import
from vllm import __version__ as VLLM_VERSION
@@ -44,8 +49,9 @@ class ImageFetchAiohttp:
image = load_image_from_base64(image_url.split(',', 1)[1])
else:
raise ValueError("Invalid image url: A valid image url must start "
"with either 'data:image' or 'http'.")
raise ValueError(
"Invalid 'image_url': A valid 'image_url' must start "
"with either 'data:image' or 'http'.")
return image
@@ -56,7 +62,7 @@ async def async_get_and_parse_image(image_url: str) -> ImagePixelData:
def encode_image_base64(image: Image.Image, format: str = 'JPEG') -> str:
"""encode image to base64 format."""
"""Encode a pillow image to base64 format."""
buffered = BytesIO()
if format == 'JPEG':