classify.py 1.42 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6

from argparse import Namespace

from vllm import LLM, EngineArgs
7
from vllm.utils.argparse_utils import FlexibleArgumentParser
8
9


10
11
12
13
def parse_args():
    parser = FlexibleArgumentParser()
    parser = EngineArgs.add_cli_args(parser)
    # Set example specific arguments
14
    parser.set_defaults(
15
16
17
        model="jason9693/Qwen2.5-1.5B-apeach",
        runner="pooling",
        enforce_eager=True,
18
    )
19
20
21
    return parser.parse_args()


22
23
24
25
26
27
28
29
30
31
def main(args: Namespace):
    # Sample prompts.
    prompts = [
        "Hello, my name is",
        "The president of the United States is",
        "The capital of France is",
        "The future of AI is",
    ]

    # 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
41
    for prompt, output in zip(prompts, outputs):
        probs = output.outputs.probs
42
43
44
45
46
        probs_trimmed = (str(probs[:16])[:-1] + ", ...]") if len(probs) > 16 else probs
        print(
            f"Prompt: {prompt!r} \n"
            f"Class Probabilities: {probs_trimmed} (size={len(probs)})"
        )
47
        print("-" * 60)
48
49
50


if __name__ == "__main__":
51
    args = parse_args()
52
    main(args)