| 
 | 1 | +"""An example showing how to use vLLM to serve VLMs.  | 
 | 2 | +
  | 
 | 3 | +Launch the vLLM server with the following command:  | 
 | 4 | +python -m vllm.entrypoints.openai.api_server \  | 
 | 5 | +    --model llava-hf/llava-1.5-7b-hf \  | 
 | 6 | +    --image-input-type pixel_values \  | 
 | 7 | +    --image-token-id 32000 \  | 
 | 8 | +    --image-input-shape 1,3,336,336 \  | 
 | 9 | +    --image-feature-size 576 \  | 
 | 10 | +    --chat-template template_llava.jinja  | 
 | 11 | +"""  | 
 | 12 | +import base64  | 
 | 13 | + | 
 | 14 | +import requests  | 
 | 15 | +from openai import OpenAI  | 
 | 16 | + | 
 | 17 | +# Modify OpenAI's API key and API base to use vLLM's API server.  | 
 | 18 | +openai_api_key = "EMPTY"  | 
 | 19 | +openai_api_base = "http://localhost:8000/v1"  | 
 | 20 | + | 
 | 21 | +client = OpenAI(  | 
 | 22 | +    # defaults to os.environ.get("OPENAI_API_KEY")  | 
 | 23 | +    api_key=openai_api_key,  | 
 | 24 | +    base_url=openai_api_base,  | 
 | 25 | +)  | 
 | 26 | + | 
 | 27 | +models = client.models.list()  | 
 | 28 | +model = models.data[0].id  | 
 | 29 | + | 
 | 30 | +image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"  | 
 | 31 | + | 
 | 32 | +# Use image url in the payload  | 
 | 33 | +chat_completion_from_url = client.chat.completions.create(  | 
 | 34 | +    messages=[{  | 
 | 35 | +        "role":  | 
 | 36 | +        "user",  | 
 | 37 | +        "content": [  | 
 | 38 | +            {  | 
 | 39 | +                "type": "text",  | 
 | 40 | +                "text": "What’s in this image?"  | 
 | 41 | +            },  | 
 | 42 | +            {  | 
 | 43 | +                "type": "image_url",  | 
 | 44 | +                "image_url": {  | 
 | 45 | +                    "url": image_url  | 
 | 46 | +                },  | 
 | 47 | +            },  | 
 | 48 | +        ],  | 
 | 49 | +    }],  | 
 | 50 | +    model=model,  | 
 | 51 | +)  | 
 | 52 | + | 
 | 53 | +result = chat_completion_from_url.choices[0].message.content  | 
 | 54 | +print(f"Chat completion output:{result}")  | 
 | 55 | + | 
 | 56 | + | 
 | 57 | +# Use base64 encoded image in the payload  | 
 | 58 | +def encode_image_base64_from_url(image_url: str) -> str:  | 
 | 59 | +    """Encode an image retrieved from a remote url to base64 format."""  | 
 | 60 | + | 
 | 61 | +    with requests.get(image_url) as response:  | 
 | 62 | +        response.raise_for_status()  | 
 | 63 | +        result = base64.b64encode(response.content).decode('utf-8')  | 
 | 64 | + | 
 | 65 | +    return result  | 
 | 66 | + | 
 | 67 | + | 
 | 68 | +image_base64 = encode_image_base64_from_url(image_url=image_url)  | 
 | 69 | +chat_completion_from_base64 = client.chat.completions.create(  | 
 | 70 | +    messages=[{  | 
 | 71 | +        "role":  | 
 | 72 | +        "user",  | 
 | 73 | +        "content": [  | 
 | 74 | +            {  | 
 | 75 | +                "type": "text",  | 
 | 76 | +                "text": "What’s in this image?"  | 
 | 77 | +            },  | 
 | 78 | +            {  | 
 | 79 | +                "type": "image_url",  | 
 | 80 | +                "image_url": {  | 
 | 81 | +                    "url": f"data:image/jpeg;base64,{image_base64}"  | 
 | 82 | +                },  | 
 | 83 | +            },  | 
 | 84 | +        ],  | 
 | 85 | +    }],  | 
 | 86 | +    model=model,  | 
 | 87 | +)  | 
 | 88 | + | 
 | 89 | +result = chat_completion_from_base64.choices[0].message.content  | 
 | 90 | +print(f"Chat completion output:{result}")  | 
0 commit comments