| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | from typing import NamedTuple |
| 4 | |
| 5 | import pytest |
| 6 | from PIL.Image import Image |
| 7 | from transformers import AutoProcessor |
| 8 | |
| 9 | from vllm import LLM, EngineArgs, SamplingParams |
| 10 | from vllm.multimodal.utils import encode_image_url |
| 11 | |
| 12 | MODEL_NAME = "Kwai-Keye/Keye-VL-8B-Preview" |
| 13 | |
| 14 | QUESTION = "What is the content of each image?" |
| 15 | |
| 16 | |
| 17 | class ModelRequestData(NamedTuple): |
| 18 | engine_args: EngineArgs |
| 19 | prompt: str |
| 20 | image_data: list[Image] |
| 21 | stop_token_ids: list[int] | None = None |
| 22 | chat_template: str | None = None |
| 23 | sampling_params: SamplingParams | None = None |
| 24 | |
| 25 | |
| 26 | @pytest.mark.parametrize("question", [QUESTION]) |
| 27 | def test_keye_vl(image_assets, question: str): |
| 28 | images = [asset.pil_image for asset in image_assets] |
| 29 | image_urls = [encode_image_url(image) for image in images] |
| 30 | |
| 31 | placeholders = [{"type": "image", "image": url} for url in image_urls] |
| 32 | messages = [ |
| 33 | { |
| 34 | "role": "user", |
| 35 | "content": [ |
| 36 | *placeholders, |
| 37 | {"type": "text", "text": question}, |
| 38 | ], |
| 39 | }, |
| 40 | ] |
| 41 | |
| 42 | processor = AutoProcessor.from_pretrained(MODEL_NAME, trust_remote_code=True) |
| 43 | |
| 44 | prompt = processor.apply_chat_template( |
| 45 | messages, tokenize=False, add_generation_prompt=True |
| 46 | ) |
| 47 | |
| 48 | llm = LLM( |
| 49 | model=MODEL_NAME, |
| 50 | trust_remote_code=True, |
| 51 | max_model_len=8192, |
| 52 | max_num_seqs=5, |
| 53 | limit_mm_per_prompt={"image": len(image_urls)}, |
| 54 | seed=42, |
| 55 | ) |
| 56 | |
| 57 | sampling_params = SamplingParams( |
| 58 | temperature=0.0, max_tokens=256, stop_token_ids=None |
| 59 | ) |
| 60 | |
| 61 | outputs = llm.generate( |
| 62 | { |
| 63 | "prompt": prompt, |
| 64 | "multi_modal_data": {"image": images}, |
| 65 | }, |
| 66 | sampling_params=sampling_params, |
| 67 | ) |
| 68 | |
| 69 | print("-" * 50) |
| 70 | for o in outputs: |
| 71 | generated_text = o.outputs[0].text |
| 72 | print(generated_text) |
| 73 | assert len(generated_text) > 10, ( |
| 74 | f"Generated text is too short: {generated_text}" |
| 75 | ) |
| 76 | print("-" * 50) |