main
py 60 lines 1.74 KB
Raw
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="jinaai/jina-embeddings-v3",
16 runner="pooling",
17 trust_remote_code=True,
18 )
19 return parser.parse_args()
20
21
22 def main(args: Namespace):
23 # Sample prompts.
24 prompts = [
25 "Follow the white rabbit.", # English
26 "Sigue al conejo blanco.", # Spanish
27 "Suis le lapin blanc.", # French
28 "跟着白兔走。", # Chinese
29 "اتبع الأرنب الأبيض.", # Arabic
30 "Folge dem weißen Kaninchen.", # German
31 ]
32
33 # Create an LLM.
34 # You should pass runner="pooling" for embedding models
35 llm = LLM(**vars(args))
36
37 # Generate embedding. The output is a list of EmbeddingRequestOutputs.
38 # Only text matching task is supported for now. See #16120
39 outputs = llm.embed(prompts)
40
41 # Print the outputs.
42 print("\nGenerated Outputs:")
43 print("Only text matching task is supported for now. See #16120")
44 print("-" * 60)
45 for prompt, output in zip(prompts, outputs):
46 embeds = output.outputs.embedding
47 embeds_trimmed = (
48 (str(embeds[:16])[:-1] + ", ...]") if len(embeds) > 16 else embeds
49 )
50 print(
51 f"Prompt: {prompt!r} \n"
52 f"Embeddings for text matching: {embeds_trimmed} "
53 f"(size={len(embeds)})"
54 )
55 print("-" * 60)
56
57
58 if __name__ == "__main__":
59 args = parse_args()
60 main(args)