| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | # ruff: noqa: E501 |
| 4 | |
| 5 | """Example online usage of the Jina Reranker v3 score and rerank APIs with a task |
| 6 | instruction. |
| 7 | |
| 8 | Run `vllm serve jinaai/jina-reranker-v3 --runner pooling` to start up the |
| 9 | server in vLLM. |
| 10 | """ |
| 11 | |
| 12 | import argparse |
| 13 | import json |
| 14 | |
| 15 | import requests |
| 16 | |
| 17 | |
| 18 | def post_http_request(prompt: dict, api_url: str) -> requests.Response: |
| 19 | headers = {"User-Agent": "Test Client"} |
| 20 | response = requests.post(api_url, headers=headers, json=prompt) |
| 21 | return response |
| 22 | |
| 23 | |
| 24 | def print_response(name: str, prompt: dict, response: requests.Response) -> None: |
| 25 | print(f"\n{name} request:") |
| 26 | print(json.dumps(prompt, indent=2)) |
| 27 | print(f"\n{name} response:") |
| 28 | print(json.dumps(response.json(), indent=2)) |
| 29 | |
| 30 | |
| 31 | def parse_args(): |
| 32 | parser = argparse.ArgumentParser() |
| 33 | parser.add_argument("--host", type=str, default="localhost") |
| 34 | parser.add_argument("--port", type=int, default=8000) |
| 35 | parser.add_argument("--model", type=str, default="jinaai/jina-reranker-v3") |
| 36 | return parser.parse_args() |
| 37 | |
| 38 | |
| 39 | def main(args): |
| 40 | score_url = f"http://{args.host}:{args.port}/score" |
| 41 | rerank_url = f"http://{args.host}:{args.port}/rerank" |
| 42 | model_name = args.model |
| 43 | |
| 44 | query = "Which passage is about sports?" |
| 45 | documents = [ |
| 46 | "Basketball is played by two teams on a court.", |
| 47 | "Green tea contains antioxidants and may support metabolism.", |
| 48 | ] |
| 49 | instruction = "Rank passages about sports higher than passages about nutrition." |
| 50 | |
| 51 | score_prompt = { |
| 52 | "model": model_name, |
| 53 | "queries": query, |
| 54 | "documents": documents, |
| 55 | "instruction": instruction, |
| 56 | } |
| 57 | score_response = post_http_request(prompt=score_prompt, api_url=score_url) |
| 58 | print_response("Score", score_prompt, score_response) |
| 59 | |
| 60 | rerank_prompt = { |
| 61 | "model": model_name, |
| 62 | "query": query, |
| 63 | "documents": documents, |
| 64 | "instruction": instruction, |
| 65 | } |
| 66 | rerank_response = post_http_request(prompt=rerank_prompt, api_url=rerank_url) |
| 67 | print_response("Rerank", rerank_prompt, rerank_response) |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | args = parse_args() |
| 72 | main(args) |