Skip to content

Commit 7107502

Browse files
authored
[Doc] Support --stream arg in openai_completion_client.py script (#18388)
Signed-off-by: googs1025 <googs1025@gmail.com>
1 parent ca86a7c commit 7107502

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

examples/online_serving/openai_chat_completion_structured_outputs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from openai import BadRequestError, OpenAI
1313
from pydantic import BaseModel
1414

15+
openai_api_key = "EMPTY"
16+
openai_api_base = "http://localhost:8000/v1"
17+
1518

1619
# Guided decoding by Choice (list of possible options)
1720
def guided_choice_completion(client: OpenAI, model: str):
@@ -134,8 +137,8 @@ def extra_backend_options_completion(client: OpenAI, model: str):
134137

135138
def main():
136139
client: OpenAI = OpenAI(
137-
base_url="http://localhost:8000/v1",
138-
api_key="-",
140+
base_url=openai_api_base,
141+
api_key=openai_api_key,
139142
)
140143

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

examples/online_serving/openai_chat_completion_structured_outputs_structural_tag.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
# to enforce the format of a tool call response, but it could be used for
88
# any structured output within a subset of the response.
99

10+
openai_api_key = "EMPTY"
11+
openai_api_base = "http://localhost:8000/v1"
12+
1013

1114
def main():
1215
client = OpenAI(
13-
base_url="http://localhost:8000/v1",
14-
api_key="-",
16+
base_url=openai_api_base,
17+
api_key=openai_api_key,
1518
)
1619

1720
messages = [{
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
import argparse
4+
35
from openai import OpenAI
46

57
# Modify OpenAI's API key and API base to use vLLM's API server.
68
openai_api_key = "EMPTY"
79
openai_api_base = "http://localhost:8000/v1"
810

911

10-
def main():
12+
def parse_args():
13+
parser = argparse.ArgumentParser(description="Client for vLLM API server")
14+
parser.add_argument("--stream",
15+
action="store_true",
16+
help="Enable streaming response")
17+
return parser.parse_args()
18+
19+
20+
def main(args):
1121
client = OpenAI(
1222
# defaults to os.environ.get("OPENAI_API_KEY")
1323
api_key=openai_api_key,
@@ -18,18 +28,17 @@ def main():
1828
model = models.data[0].id
1929

2030
# Completion API
21-
stream = False
2231
completion = client.completions.create(
2332
model=model,
2433
prompt="A robot may not injure a human being",
2534
echo=False,
2635
n=2,
27-
stream=stream,
36+
stream=args.stream,
2837
logprobs=3)
2938

3039
print("-" * 50)
3140
print("Completion results:")
32-
if stream:
41+
if args.stream:
3342
for c in completion:
3443
print(c)
3544
else:
@@ -38,4 +47,5 @@ def main():
3847

3948

4049
if __name__ == "__main__":
41-
main()
50+
args = parse_args()
51+
main(args)

0 commit comments

Comments
 (0)