| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from argparse import Namespace |
| 5 | |
| 6 | from vllm import LLM, EngineArgs |
| 7 | from vllm.utils.argparse_utils import FlexibleArgumentParser |
| 8 | |
| 9 | |
| 10 | def parse_args(): |
| 11 | parser = FlexibleArgumentParser() |
| 12 | parser = EngineArgs.add_cli_args(parser) |
| 13 | # Set example specific arguments |
| 14 | parser.set_defaults( |
| 15 | model="jason9693/Qwen2.5-1.5B-apeach", |
| 16 | runner="pooling", |
| 17 | enforce_eager=True, |
| 18 | ) |
| 19 | return parser.parse_args() |
| 20 | |
| 21 | |
| 22 | def main(args: Namespace): |
| 23 | # Sample prompts. |
| 24 | prompts = [ |
| 25 | "Hello, my name is", |
| 26 | "The president of the United States is", |
| 27 | "The capital of France is", |
| 28 | "The future of AI is", |
| 29 | ] |
| 30 | |
| 31 | # Create an LLM. |
| 32 | # You should pass runner="pooling" for classification models |
| 33 | llm = LLM(**vars(args)) |
| 34 | |
| 35 | # Generate logits. The output is a list of ClassificationRequestOutputs. |
| 36 | outputs = llm.classify(prompts) |
| 37 | |
| 38 | # Print the outputs. |
| 39 | print("\nGenerated Outputs:\n" + "-" * 60) |
| 40 | for prompt, output in zip(prompts, outputs): |
| 41 | probs = output.outputs.probs |
| 42 | probs_trimmed = (str(probs[:16])[:-1] + ", ...]") if len(probs) > 16 else probs |
| 43 | print( |
| 44 | f"Prompt: {prompt!r} \n" |
| 45 | f"Class Probabilities: {probs_trimmed} (size={len(probs)})" |
| 46 | ) |
| 47 | print("-" * 60) |
| 48 | |
| 49 | |
| 50 | if __name__ == "__main__": |
| 51 | args = parse_args() |
| 52 | main(args) |