| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | # ruff: noqa: E501 |
| 4 | |
| 5 | import torch.nn.functional as F |
| 6 | |
| 7 | from vllm import LLM |
| 8 | |
| 9 | query = "What are the health benefits of green tea?" |
| 10 | documents = [ |
| 11 | "Green tea contains antioxidants called catechins that may help reduce inflammation and protect cells from damage.", |
| 12 | "El precio del café ha aumentado un 20% este año debido a problemas en la cadena de suministro.", |
| 13 | "Studies show that drinking green tea regularly can improve brain function and boost metabolism.", |
| 14 | "Basketball is one of the most popular sports in the United States.", |
| 15 | "绿茶富含儿茶素等抗氧化剂,可以降低心脏病风险,还有助于控制体重。", |
| 16 | "Le thé vert est riche en antioxydants et peut améliorer la fonction cérébrale.", |
| 17 | ] |
| 18 | |
| 19 | |
| 20 | def main(): |
| 21 | # Initialize model |
| 22 | llm = LLM( |
| 23 | model="jinaai/jina-reranker-v3", |
| 24 | runner="pooling", |
| 25 | ) |
| 26 | |
| 27 | # Generate scores. |
| 28 | outputs = llm.score(query, documents) |
| 29 | |
| 30 | # Print the outputs. |
| 31 | print("\nGenerated Outputs:\n" + "-" * 60) |
| 32 | for document, output in zip(documents, outputs): |
| 33 | score = output.outputs.score |
| 34 | print(f"Pair: {[query, document]!r} \nScore: {score}") |
| 35 | print("-" * 60) |
| 36 | |
| 37 | # Generate embeddings. |
| 38 | # The JinaForRanking model concatenates docs first, then query. |
| 39 | # Let's stay consistent with this novel design. |
| 40 | outputs = llm.encode(documents + [query], pooling_task="token_embed") |
| 41 | embeds = outputs[0].outputs.data.float() |
| 42 | |
| 43 | doc_embeds = embeds[:-1] |
| 44 | query_embeds = embeds[-1] |
| 45 | |
| 46 | scores = F.cosine_similarity(query_embeds, doc_embeds) |
| 47 | |
| 48 | # Print the outputs. |
| 49 | print("\nGenerated Outputs:\n" + "-" * 60) |
| 50 | for document, score in zip(documents, scores): |
| 51 | print(f"Pair: {[query, document]!r} \nScore: {score}") |
| 52 | print("-" * 60) |
| 53 | |
| 54 | |
| 55 | if __name__ == "__main__": |
| 56 | main() |