2024-11-24 23:56:20 -03:00
|
|
|
"""
|
2024-12-14 00:22:22 +08:00
|
|
|
Example online usage of Score API.
|
2024-11-24 23:56:20 -03:00
|
|
|
|
2024-12-14 00:22:22 +08:00
|
|
|
Run `vllm serve <model> --task score` to start up the server in vLLM.
|
|
|
|
"""
|
2024-11-24 23:56:20 -03:00
|
|
|
import argparse
|
|
|
|
import pprint
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2024-12-14 00:22:22 +08:00
|
|
|
def post_http_request(prompt: dict, api_url: str) -> requests.Response:
|
2024-11-24 23:56:20 -03:00
|
|
|
headers = {"User-Agent": "Test Client"}
|
|
|
|
response = requests.post(api_url, headers=headers, json=prompt)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--host", type=str, default="localhost")
|
|
|
|
parser.add_argument("--port", type=int, default=8000)
|
|
|
|
parser.add_argument("--model", type=str, default="BAAI/bge-reranker-v2-m3")
|
|
|
|
args = parser.parse_args()
|
2024-12-14 00:22:22 +08:00
|
|
|
api_url = f"http://{args.host}:{args.port}/score"
|
2024-11-24 23:56:20 -03:00
|
|
|
|
|
|
|
model_name = args.model
|
|
|
|
|
2024-12-14 00:22:22 +08:00
|
|
|
text_1 = "What is the capital of Brazil?"
|
|
|
|
text_2 = "The capital of Brazil is Brasilia."
|
|
|
|
prompt = {"model": model_name, "text_1": text_1, "text_2": text_2}
|
|
|
|
score_response = post_http_request(prompt=prompt, api_url=api_url)
|
|
|
|
print("Prompt when text_1 and text_2 are both strings:")
|
|
|
|
pprint.pprint(prompt)
|
|
|
|
print("Score Response:")
|
|
|
|
pprint.pprint(score_response.json())
|
|
|
|
|
2024-11-24 23:56:20 -03:00
|
|
|
text_1 = "What is the capital of France?"
|
|
|
|
text_2 = [
|
|
|
|
"The capital of Brazil is Brasilia.", "The capital of France is Paris."
|
|
|
|
]
|
|
|
|
prompt = {"model": model_name, "text_1": text_1, "text_2": text_2}
|
|
|
|
score_response = post_http_request(prompt=prompt, api_url=api_url)
|
2024-12-14 00:22:22 +08:00
|
|
|
print("Prompt when text_1 is string and text_2 is a list:")
|
2024-11-24 23:56:20 -03:00
|
|
|
pprint.pprint(prompt)
|
|
|
|
print("Score Response:")
|
2024-12-14 00:22:22 +08:00
|
|
|
pprint.pprint(score_response.json())
|
2024-11-24 23:56:20 -03:00
|
|
|
|
|
|
|
text_1 = [
|
|
|
|
"What is the capital of Brazil?", "What is the capital of France?"
|
|
|
|
]
|
|
|
|
text_2 = [
|
|
|
|
"The capital of Brazil is Brasilia.", "The capital of France is Paris."
|
|
|
|
]
|
|
|
|
prompt = {"model": model_name, "text_1": text_1, "text_2": text_2}
|
|
|
|
score_response = post_http_request(prompt=prompt, api_url=api_url)
|
2024-12-14 00:22:22 +08:00
|
|
|
print("Prompt when text_1 and text_2 are both lists:")
|
2024-11-24 23:56:20 -03:00
|
|
|
pprint.pprint(prompt)
|
|
|
|
print("Score Response:")
|
2024-12-14 00:22:22 +08:00
|
|
|
pprint.pprint(score_response.json())
|