Skip to content

Commit 91a63f2

Browse files
MikeAmyatty-openai
andauthored
Fixed CLI streamed chat completions. (openai#319)
* 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 <atty@openai.com>
1 parent 03f848e commit 91a63f2

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

openai/cli.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ def create(cls, args):
141141
for c_idx, c in enumerate(sorted(choices, key=lambda s: s["index"])):
142142
if len(choices) > 1:
143143
sys.stdout.write("===== Chat Completion {} =====\n".format(c_idx))
144-
sys.stdout.write(c["message"]["content"])
145-
if len(choices) > 1:
146-
sys.stdout.write("\n")
144+
if args.stream:
145+
delta = c["delta"]
146+
if "content" in delta:
147+
sys.stdout.write(delta["content"])
148+
else:
149+
sys.stdout.write(c["message"]["content"])
150+
if len(choices) > 1: # not in streams
151+
sys.stdout.write("\n")
147152
sys.stdout.flush()
148153

149154

@@ -203,7 +208,9 @@ def list(cls, args):
203208

204209
@classmethod
205210
def create(cls, args):
206-
models = openai.Deployment.create(model=args.model, scale_settings={"scale_type": args.scale_type})
211+
models = openai.Deployment.create(
212+
model=args.model, scale_settings={"scale_type": args.scale_type}
213+
)
207214
print(models)
208215

209216

@@ -833,10 +840,15 @@ def help(args):
833840
sub = subparsers.add_parser("deployments.delete")
834841
sub.add_argument("-i", "--id", required=True, help="The deployment ID")
835842
sub.set_defaults(func=Deployment.delete)
836-
843+
837844
sub = subparsers.add_parser("deployments.create")
838845
sub.add_argument("-m", "--model", required=True, help="The model ID")
839-
sub.add_argument("-s", "--scale_type", required=True, help="The scale type. Either 'manual' or 'standard'")
846+
sub.add_argument(
847+
"-s",
848+
"--scale_type",
849+
required=True,
850+
help="The scale type. Either 'manual' or 'standard'",
851+
)
840852
sub.set_defaults(func=Deployment.create)
841853

842854
# Models

0 commit comments

Comments
 (0)