embed.py 1.38 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
8
9

from argparse import Namespace

from vllm import LLM, EngineArgs
from vllm.utils import FlexibleArgumentParser


10
11
12
13
def parse_args():
    parser = FlexibleArgumentParser()
    parser = EngineArgs.add_cli_args(parser)
    # Set example specific arguments
14
    parser.set_defaults(
15
        model="intfloat/e5-small",
16
        runner="pooling",
17
        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 embedding models
33
    llm = LLM(**vars(args))
34
35

    # Generate embedding. The output is a list of EmbeddingRequestOutputs.
36
    outputs = llm.embed(prompts)
37
38

    # Print the outputs.
39
    print("\nGenerated Outputs:\n" + "-" * 60)
40
41
    for prompt, output in zip(prompts, outputs):
        embeds = output.outputs.embedding
42
43
44
45
        embeds_trimmed = (
            (str(embeds[:16])[:-1] + ", ...]") if len(embeds) > 16 else embeds
        )
        print(f"Prompt: {prompt!r} \nEmbeddings: {embeds_trimmed} (size={len(embeds)})")
46
        print("-" * 60)
47
48
49


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