openai_completion_client.py 567 Bytes
Newer Older
Zhuohan Li's avatar
Zhuohan Li committed
1
import openai
2

Woosuk Kwon's avatar
Woosuk Kwon committed
3
# Modify OpenAI's API key and API base to use vLLM's API server.
Zhuohan Li's avatar
Zhuohan Li committed
4
5
6
openai.api_key = "EMPTY"
openai.api_base = "http://localhost:8000/v1"

7
# List models API
Zhuohan Li's avatar
Zhuohan Li committed
8
models = openai.Model.list()
9
print("Models:", models)
Zhuohan Li's avatar
Zhuohan Li committed
10

11
12
13
14
model = models["data"][0]["id"]

# Completion API
stream = False
Zhuohan Li's avatar
Zhuohan Li committed
15
completion = openai.Completion.create(
16
17
18
19
20
21
    model=model,
    prompt="A robot may not injure a human being",
    echo=False,
    n=2,
    stream=stream,
    logprobs=3)
Zhuohan Li's avatar
Zhuohan Li committed
22

23
print("Completion results:")
Zhuohan Li's avatar
Zhuohan Li committed
24
25
26
27
if stream:
    for c in completion:
        print(c)
else:
28
    print(completion)