embed.py 1.31 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
from vllm.utils.print_utils import print_embeddings
9
10


11
12
13
14
def parse_args():
    parser = FlexibleArgumentParser()
    parser = EngineArgs.add_cli_args(parser)
    # Set example specific arguments
15
    parser.set_defaults(
16
        model="intfloat/e5-small",
17
        runner="pooling",
18
        enforce_eager=True,
19
    )
20
21
22
    return parser.parse_args()


23
24
25
26
27
28
29
30
31
32
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.
33
    # You should pass runner="pooling" for embedding models
34
    llm = LLM(**vars(args))
35
36

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

    # Print the outputs.
40
    print("\nGenerated Outputs:\n" + "-" * 60)
41
42
    for prompt, output in zip(prompts, outputs):
        embeds = output.outputs.embedding
43
44
        print(f"Prompt: {prompt!r}")
        print_embeddings(embeds)
45
        print("-" * 60)
46
47
48


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