openai_completion_client.py 1.2 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
import argparse

6
from openai import OpenAI
7

Woosuk Kwon's avatar
Woosuk Kwon committed
8
# Modify OpenAI's API key and API base to use vLLM's API server.
9
10
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"
Zhuohan Li's avatar
Zhuohan Li committed
11

12

13
14
def parse_args():
    parser = argparse.ArgumentParser(description="Client for vLLM API server")
15
16
17
    parser.add_argument(
        "--stream", action="store_true", help="Enable streaming response"
    )
18
19
20
21
    return parser.parse_args()


def main(args):
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    client = OpenAI(
        # defaults to os.environ.get("OPENAI_API_KEY")
        api_key=openai_api_key,
        base_url=openai_api_base,
    )

    models = client.models.list()
    model = models.data[0].id

    # Completion API
    completion = client.completions.create(
        model=model,
        prompt="A robot may not injure a human being",
        echo=False,
        n=2,
37
        stream=args.stream,
38
39
        logprobs=3,
    )
40
41
42

    print("-" * 50)
    print("Completion results:")
43
    if args.stream:
44
45
46
47
48
49
50
51
        for c in completion:
            print(c)
    else:
        print(completion)
    print("-" * 50)


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