Skip to content

[Doc] Support --stream arg in openai_completion_client.py script #18388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from openai import BadRequestError, OpenAI
from pydantic import BaseModel

openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"


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

def main():
client: OpenAI = OpenAI(
base_url="http://localhost:8000/v1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1" to keep it consistent with other scripts

api_key="-",
base_url=openai_api_base,
api_key=openai_api_key,
)

model = client.models.list().data[0].id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
# to enforce the format of a tool call response, but it could be used for
# any structured output within a subset of the response.

openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"


def main():
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="-",
base_url=openai_api_base,
api_key=openai_api_key,
)

messages = [{
Expand Down
20 changes: 15 additions & 5 deletions examples/online_serving/openai_completion_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# SPDX-License-Identifier: Apache-2.0

import argparse

from openai import OpenAI

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


def main():
def parse_args():
parser = argparse.ArgumentParser(description="Client for vLLM API server")
parser.add_argument("--stream",
action="store_true",
help="Enable streaming response")
return parser.parse_args()


def main(args):
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key=openai_api_key,
Expand All @@ -18,18 +28,17 @@ def main():
model = models.data[0].id

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

print("-" * 50)
print("Completion results:")
if stream:
if args.stream:
for c in completion:
print(c)
else:
Expand All @@ -38,4 +47,5 @@ def main():


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