| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | import requests |
| 7 | |
| 8 | from tests.utils import RemoteOpenAIServer |
| 9 | from vllm.assets.base import VLLM_S3_BUCKET_URL |
| 10 | from vllm.entrypoints.pooling.classify.protocol import ClassificationResponse |
| 11 | from vllm.multimodal.utils import encode_image_url, fetch_image |
| 12 | |
| 13 | MODEL_NAME = "muziyongshixin/Qwen2.5-VL-7B-for-VideoCls" |
| 14 | MAXIMUM_VIDEOS = 1 |
| 15 | |
| 16 | HF_OVERRIDES = {"architectures": ["Qwen2_5_VLForSequenceClassification"]} |
| 17 | input_text = "This product was excellent and exceeded my expectations" |
| 18 | image_url = f"{VLLM_S3_BUCKET_URL}/multimodal_asset/cat_snow.jpg" |
| 19 | image_base64 = {"url": encode_image_url(fetch_image(image_url))} |
| 20 | video_url = f"{VLLM_S3_BUCKET_URL}/multimodal_asset/slow_traffic_small.mp4" |
| 21 | |
| 22 | |
| 23 | @pytest.fixture(scope="module") |
| 24 | def server(): |
| 25 | args = [ |
| 26 | "--runner", |
| 27 | "pooling", |
| 28 | "--max-model-len", |
| 29 | "16384", |
| 30 | "--enforce-eager", |
| 31 | "--limit-mm-per-prompt", |
| 32 | json.dumps({"video": MAXIMUM_VIDEOS}), |
| 33 | ] |
| 34 | |
| 35 | with RemoteOpenAIServer( |
| 36 | MODEL_NAME, args, override_hf_configs=HF_OVERRIDES |
| 37 | ) as remote_server: |
| 38 | yield remote_server |
| 39 | |
| 40 | |
| 41 | @pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 42 | def test_chat_text_request(server: RemoteOpenAIServer, model_name: str): |
| 43 | messages = [ |
| 44 | { |
| 45 | "role": "assistant", |
| 46 | "content": "Please classify this text request.", |
| 47 | }, |
| 48 | { |
| 49 | "role": "user", |
| 50 | "content": input_text, |
| 51 | }, |
| 52 | ] |
| 53 | |
| 54 | response = requests.post( |
| 55 | server.url_for("classify"), |
| 56 | json={"model": model_name, "messages": messages}, |
| 57 | ) |
| 58 | response.raise_for_status() |
| 59 | |
| 60 | output = ClassificationResponse.model_validate(response.json()) |
| 61 | |
| 62 | assert output.object == "list" |
| 63 | assert output.model == model_name |
| 64 | assert len(output.data) == 1 |
| 65 | assert len(output.data[0].probs) == 2 |
| 66 | assert output.usage.prompt_tokens == 35 |
| 67 | |
| 68 | |
| 69 | @pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 70 | def test_chat_image_url_request(server: RemoteOpenAIServer, model_name: str): |
| 71 | messages = [ |
| 72 | { |
| 73 | "role": "user", |
| 74 | "content": [ |
| 75 | {"type": "text", "text": "Please classify this image."}, |
| 76 | {"type": "image_url", "image_url": {"url": image_url}}, |
| 77 | ], |
| 78 | } |
| 79 | ] |
| 80 | |
| 81 | response = requests.post( |
| 82 | server.url_for("classify"), |
| 83 | json={"model": model_name, "messages": messages}, |
| 84 | ) |
| 85 | response.raise_for_status() |
| 86 | |
| 87 | output = ClassificationResponse.model_validate(response.json()) |
| 88 | |
| 89 | assert output.object == "list" |
| 90 | assert output.model == model_name |
| 91 | assert len(output.data) == 1 |
| 92 | assert len(output.data[0].probs) == 2 |
| 93 | assert output.usage.prompt_tokens == 47 |
| 94 | |
| 95 | |
| 96 | @pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 97 | def test_chat_image_base64_request(server: RemoteOpenAIServer, model_name: str): |
| 98 | messages = [ |
| 99 | { |
| 100 | "role": "user", |
| 101 | "content": [ |
| 102 | {"type": "text", "text": "Please classify this image."}, |
| 103 | {"type": "image_url", "image_url": image_base64}, |
| 104 | ], |
| 105 | } |
| 106 | ] |
| 107 | |
| 108 | response = requests.post( |
| 109 | server.url_for("classify"), |
| 110 | json={"model": model_name, "messages": messages}, |
| 111 | ) |
| 112 | response.raise_for_status() |
| 113 | |
| 114 | output = ClassificationResponse.model_validate(response.json()) |
| 115 | |
| 116 | assert output.object == "list" |
| 117 | assert output.model == model_name |
| 118 | assert len(output.data) == 1 |
| 119 | assert len(output.data[0].probs) == 2 |
| 120 | assert output.usage.prompt_tokens == 47 |
| 121 | |
| 122 | |
| 123 | @pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 124 | def test_chat_video_url_request(server: RemoteOpenAIServer, model_name: str): |
| 125 | messages = [ |
| 126 | { |
| 127 | "role": "user", |
| 128 | "content": [ |
| 129 | {"type": "text", "text": "Please classify this video."}, |
| 130 | {"type": "video_url", "video_url": {"url": video_url}}, |
| 131 | ], |
| 132 | } |
| 133 | ] |
| 134 | |
| 135 | response = requests.post( |
| 136 | server.url_for("classify"), |
| 137 | json={"model": model_name, "messages": messages}, |
| 138 | ) |
| 139 | response.raise_for_status() |
| 140 | |
| 141 | output = ClassificationResponse.model_validate(response.json()) |
| 142 | |
| 143 | assert output.object == "list" |
| 144 | assert output.model == model_name |
| 145 | assert len(output.data) == 1 |
| 146 | assert len(output.data[0].probs) == 2 |
| 147 | assert output.usage.prompt_tokens == 8993 |