Skip to content

[Doc] Add stream flag for chat completion example #18524

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
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
24 changes: 21 additions & 3 deletions examples/online_serving/openai_chat_completion_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
NOTE: start a supported chat completion model server with `vllm serve`, e.g.
vllm serve meta-llama/Llama-2-7b-chat-hf
"""

import argparse

from openai import OpenAI

# Modify OpenAI's API key and API base to use vLLM's API server.
Expand All @@ -24,7 +27,15 @@
}]


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 @@ -34,16 +45,23 @@ def main():
models = client.models.list()
model = models.data[0].id

# Chat Completion API
chat_completion = client.chat.completions.create(
messages=messages,
model=model,
stream=args.stream,
)

print("-" * 50)
print("Chat completion results:")
print(chat_completion)
if args.stream:
for c in chat_completion:
print(c)
else:
print(chat_completion)
print("-" * 50)


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