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