openai_pooling_client.py 1.67 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
"""
Example online usage of Pooling API.

6
Run `vllm serve <model> --runner pooling`
7
8
9
to start up the server in vLLM. e.g.

vllm serve internlm/internlm2-1_8b-reward --trust-remote-code
10
"""
11

12
13
14
15
16
17
18
19
20
21
22
23
import argparse
import pprint

import requests


def post_http_request(prompt: dict, api_url: str) -> requests.Response:
    headers = {"User-Agent": "Test Client"}
    response = requests.post(api_url, headers=headers, json=prompt)
    return response


24
def parse_args():
25
26
27
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default="localhost")
    parser.add_argument("--port", type=int, default=8000)
28
    parser.add_argument("--model", type=str, default="internlm/internlm2-1_8b-reward")
29

30
31
32
33
    return parser.parse_args()


def main(args):
34
35
36
37
38
39
    api_url = f"http://{args.host}:{args.port}/pooling"
    model_name = args.model

    # Input like Completions API
    prompt = {"model": model_name, "input": "vLLM is great!"}
    pooling_response = post_http_request(prompt=prompt, api_url=api_url)
40
    print("-" * 50)
41
42
    print("Pooling Response:")
    pprint.pprint(pooling_response.json())
43
    print("-" * 50)
44
45
46

    # Input like Chat API
    prompt = {
47
48
49
50
51
52
53
        "model": model_name,
        "messages": [
            {
                "role": "user",
                "content": [{"type": "text", "text": "vLLM is great!"}],
            }
        ],
54
55
56
57
    }
    pooling_response = post_http_request(prompt=prompt, api_url=api_url)
    print("Pooling Response:")
    pprint.pprint(pooling_response.json())
58
59
60
61
62
63
    print("-" * 50)


if __name__ == "__main__":
    args = parse_args()
    main(args)