| 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="BAAI/bge-reranker-v2-m3", |
| 16 | runner="pooling", |
| 17 | enforce_eager=True, |
| 18 | ) |
| 19 | return parser.parse_args() |
| 20 | |
| 21 | |
| 22 | def main(args: Namespace): |
| 23 | # Sample prompts. |
| 24 | query = "What is the capital of France?" |
| 25 | documents = [ |
| 26 | "The capital of Brazil is Brasilia.", |
| 27 | "The capital of France is Paris.", |
| 28 | ] |
| 29 | |
| 30 | # Create an LLM. |
| 31 | # You should pass runner="pooling" for cross-encoder models |
| 32 | llm = LLM(**vars(args)) |
| 33 | |
| 34 | # Generate scores. The output is a list of ScoringRequestOutputs. |
| 35 | outputs = llm.score(query, documents) |
| 36 | |
| 37 | # Print the outputs. |
| 38 | print("\nGenerated Outputs:\n" + "-" * 60) |
| 39 | for document, output in zip(documents, outputs): |
| 40 | score = output.outputs.score |
| 41 | print(f"Pair: {[query, document]!r} \nScore: {score}") |
| 42 | print("-" * 60) |
| 43 | |
| 44 | |
| 45 | if __name__ == "__main__": |
| 46 | args = parse_args() |
| 47 | main(args) |