| 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 | from vllm.utils.print_utils import print_embeddings |
| 9 | |
| 10 | |
| 11 | def parse_args(): |
| 12 | parser = FlexibleArgumentParser() |
| 13 | parser = EngineArgs.add_cli_args(parser) |
| 14 | # Set example specific arguments |
| 15 | parser.set_defaults( |
| 16 | model="intfloat/e5-small", |
| 17 | runner="pooling", |
| 18 | enforce_eager=True, |
| 19 | ) |
| 20 | return parser.parse_args() |
| 21 | |
| 22 | |
| 23 | def main(args: Namespace): |
| 24 | # Sample prompts. |
| 25 | prompts = [ |
| 26 | "Hello, my name is", |
| 27 | "The president of the United States is", |
| 28 | "The capital of France is", |
| 29 | "The future of AI is", |
| 30 | ] |
| 31 | |
| 32 | # 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 | for prompt, output in zip(prompts, outputs): |
| 42 | embeds = output.outputs.embedding |
| 43 | print(f"Prompt: {prompt!r}") |
| 44 | print_embeddings(embeds) |
| 45 | print("-" * 60) |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
| 49 | args = parse_args() |
| 50 | main(args) |