openai_completion_client.py 699 Bytes
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
from openai import OpenAI
4

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

9
10
11
12
13
client = OpenAI(
    # defaults to os.environ.get("OPENAI_API_KEY")
    api_key=openai_api_key,
    base_url=openai_api_base,
)
Zhuohan Li's avatar
Zhuohan Li committed
14

15
16
models = client.models.list()
model = models.data[0].id
17
18
19

# Completion API
stream = False
20
completion = client.completions.create(
21
22
23
24
25
    model=model,
    prompt="A robot may not injure a human being",
    echo=False,
    n=2,
    stream=stream,
26
    logprobs=3)
Zhuohan Li's avatar
Zhuohan Li committed
27

28
print("Completion results:")
Zhuohan Li's avatar
Zhuohan Li committed
29
30
31
32
if stream:
    for c in completion:
        print(c)
else:
33
    print(completion)