api_client.py 2.3 KB
Newer Older
Woosuk Kwon's avatar
Woosuk Kwon committed
1
"""Example Python client for vllm.entrypoints.api_server"""
2

Zhuohan Li's avatar
Zhuohan Li committed
3
4
import argparse
import json
5
from typing import Iterable, List
Zhuohan Li's avatar
Zhuohan Li committed
6

7
8
9
10
import requests


def clear_line(n: int = 1) -> None:
Zhuohan Li's avatar
Zhuohan Li committed
11
12
    LINE_UP = '\033[1A'
    LINE_CLEAR = '\x1b[2K'
13
    for _ in range(n):
Zhuohan Li's avatar
Zhuohan Li committed
14
15
16
        print(LINE_UP, end=LINE_CLEAR, flush=True)


17
18
def post_http_request(prompt: str, api_url: str, n: int = 1,
                      stream: bool = False) -> requests.Response:
Zhuohan Li's avatar
Zhuohan Li committed
19
20
21
22
23
24
25
    headers = {"User-Agent": "Test Client"}
    pload = {
        "prompt": prompt,
        "n": n,
        "use_beam_search": True,
        "temperature": 0.0,
        "max_tokens": 16,
26
        "stream": stream,
Zhuohan Li's avatar
Zhuohan Li committed
27
28
    }
    response = requests.post(api_url, headers=headers, json=pload, stream=True)
29
30
    return response

Zhuohan Li's avatar
Zhuohan Li committed
31

32
33
34
def get_streaming_response(response: requests.Response) -> Iterable[List[str]]:
    for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False,
                                     delimiter=b"\0"):
Zhuohan Li's avatar
Zhuohan Li committed
35
36
37
38
39
40
        if chunk:
            data = json.loads(chunk.decode("utf-8"))
            output = data["text"]
            yield output


41
42
43
44
45
46
def get_response(response: requests.Response) -> List[str]:
    data = json.loads(response.content)
    output = data["text"]
    return output


Zhuohan Li's avatar
Zhuohan Li committed
47
48
49
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--host", type=str, default="localhost")
50
    parser.add_argument("--port", type=int, default=8000)
Zhuohan Li's avatar
Zhuohan Li committed
51
52
    parser.add_argument("--n", type=int, default=4)
    parser.add_argument("--prompt", type=str, default="San Francisco is a")
53
    parser.add_argument("--stream", action="store_true")
Zhuohan Li's avatar
Zhuohan Li committed
54
55
56
57
    args = parser.parse_args()
    prompt = args.prompt
    api_url = f"http://{args.host}:{args.port}/generate"
    n = args.n
58
    stream = args.stream
Zhuohan Li's avatar
Zhuohan Li committed
59

60
    print(f"Prompt: {prompt!r}\n", flush=True)
61
62
63
    response = post_http_request(prompt, api_url, n, stream)

    if stream:
Zhuohan Li's avatar
Zhuohan Li committed
64
        num_printed_lines = 0
65
66
67
68
69
        for h in get_streaming_response(response):
            clear_line(num_printed_lines)
            num_printed_lines = 0
            for i, line in enumerate(h):
                num_printed_lines += 1
70
                print(f"Beam candidate {i}: {line!r}", flush=True)
71
72
73
    else:
        output = get_response(response)
        for i, line in enumerate(output):
74
            print(f"Beam candidate {i}: {line!r}", flush=True)