OpenAI Vision API Client
Source vllm-project/vllm.
1"""An example showing how to use vLLM to serve VLMs.23Launch the vLLM server with the following command:4vllm serve llava-hf/llava-1.5-7b-hf --chat-template template_llava.jinja5"""6import base6478import requests9from openai import OpenAI1011# Modify OpenAI's API key and API base to use vLLM's API server.12openai_api_key = "EMPTY"13openai_api_base = "http://localhost:8000/v1"1415client = OpenAI(16 # defaults to os.environ.get("OPENAI_API_KEY")17 api_key=openai_api_key,18 base_url=openai_api_base,19)2021models = client.models.list()22model = models.data[0].id2324image_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"2526# Use image url in the payload27chat_completion_from_url = client.chat.completions.create(28 messages=[{29 "role":30 "user",31 "content": [32 {33 "type": "text",34 "text": "What’s in this image?"35 },36 {37 "type": "image_url",38 "image_url": {39 "url": image_url40 },41 },42 ],43 }],44 model=model,45)4647result = chat_completion_from_url.choices[0].message.content48print(f"Chat completion output:{result}")495051# Use base64 encoded image in the payload52def encode_image_base64_from_url(image_url: str) -> str:53 """Encode an image retrieved from a remote url to base64 format."""5455 with requests.get(image_url) as response:56 response.raise_for_status()57 result = base64.b64encode(response.content).decode('utf-8')5859 return result606162image_base64 = encode_image_base64_from_url(image_url=image_url)63chat_completion_from_base64 = client.chat.completions.create(64 messages=[{65 "role":66 "user",67 "content": [68 {69 "type": "text",70 "text": "What’s in this image?"71 },72 {73 "type": "image_url",74 "image_url": {75 "url": f"data:image/jpeg;base64,{image_base64}"76 },77 },78 ],79 }],80 model=model,81)8283result = chat_completion_from_base64.choices[0].message.content84print(f"Chat completion output:{result}")