Skip to content

Commit

Permalink
Fixed CLI streamed chat completions. (#319)
Browse files Browse the repository at this point in the history
* Fixed streamed chat completions.

Streamed chat completions use a different response structure per returned token, also they may have roles and empty tokens at the end. Handle this sensibly.

* Only render content

---------

Co-authored-by: Atty Eleti <[email protected]>
  • Loading branch information
MikeAmy and athyuttamre authored May 8, 2023
1 parent 03f848e commit 91a63f2
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions openai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ def create(cls, args):
for c_idx, c in enumerate(sorted(choices, key=lambda s: s["index"])):
if len(choices) > 1:
sys.stdout.write("===== Chat Completion {} =====\n".format(c_idx))
sys.stdout.write(c["message"]["content"])
if len(choices) > 1:
sys.stdout.write("\n")
if args.stream:
delta = c["delta"]
if "content" in delta:
sys.stdout.write(delta["content"])
else:
sys.stdout.write(c["message"]["content"])
if len(choices) > 1: # not in streams
sys.stdout.write("\n")
sys.stdout.flush()


Expand Down Expand Up @@ -203,7 +208,9 @@ def list(cls, args):

@classmethod
def create(cls, args):
models = openai.Deployment.create(model=args.model, scale_settings={"scale_type": args.scale_type})
models = openai.Deployment.create(
model=args.model, scale_settings={"scale_type": args.scale_type}
)
print(models)


Expand Down Expand Up @@ -833,10 +840,15 @@ def help(args):
sub = subparsers.add_parser("deployments.delete")
sub.add_argument("-i", "--id", required=True, help="The deployment ID")
sub.set_defaults(func=Deployment.delete)

sub = subparsers.add_parser("deployments.create")
sub.add_argument("-m", "--model", required=True, help="The model ID")
sub.add_argument("-s", "--scale_type", required=True, help="The scale type. Either 'manual' or 'standard'")
sub.add_argument(
"-s",
"--scale_type",
required=True,
help="The scale type. Either 'manual' or 'standard'",
)
sub.set_defaults(func=Deployment.create)

# Models
Expand Down

0 comments on commit 91a63f2

Please sign in to comment.