OpenAI Cross Encoder Score

OpenAI Cross Encoder Score#

Source vllm-project/vllm.

 1"""
 2Example online usage of Score API.
 3
 4Run `vllm serve <model> --task score` to start up the server in vLLM.
 5"""
 6import argparse
 7import pprint
 8
 9import requests
10
11
12def post_http_request(prompt: dict, api_url: str) -> requests.Response:
13    headers = {"User-Agent": "Test Client"}
14    response = requests.post(api_url, headers=headers, json=prompt)
15    return response
16
17
18if __name__ == "__main__":
19    parser = argparse.ArgumentParser()
20    parser.add_argument("--host", type=str, default="localhost")
21    parser.add_argument("--port", type=int, default=8000)
22    parser.add_argument("--model", type=str, default="BAAI/bge-reranker-v2-m3")
23    args = parser.parse_args()
24    api_url = f"http://{args.host}:{args.port}/score"
25
26    model_name = args.model
27
28    text_1 = "What is the capital of Brazil?"
29    text_2 = "The capital of Brazil is Brasilia."
30    prompt = {"model": model_name, "text_1": text_1, "text_2": text_2}
31    score_response = post_http_request(prompt=prompt, api_url=api_url)
32    print("Prompt when text_1 and text_2 are both strings:")
33    pprint.pprint(prompt)
34    print("Score Response:")
35    pprint.pprint(score_response.json())
36
37    text_1 = "What is the capital of France?"
38    text_2 = [
39        "The capital of Brazil is Brasilia.", "The capital of France is Paris."
40    ]
41    prompt = {"model": model_name, "text_1": text_1, "text_2": text_2}
42    score_response = post_http_request(prompt=prompt, api_url=api_url)
43    print("Prompt when text_1 is string and text_2 is a list:")
44    pprint.pprint(prompt)
45    print("Score Response:")
46    pprint.pprint(score_response.json())
47
48    text_1 = [
49        "What is the capital of Brazil?", "What is the capital of France?"
50    ]
51    text_2 = [
52        "The capital of Brazil is Brasilia.", "The capital of France is Paris."
53    ]
54    prompt = {"model": model_name, "text_1": text_1, "text_2": text_2}
55    score_response = post_http_request(prompt=prompt, api_url=api_url)
56    print("Prompt when text_1 and text_2 are both lists:")
57    pprint.pprint(prompt)
58    print("Score Response:")
59    pprint.pprint(score_response.json())