Skip to content
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

Issue #315: Display full completion in CLI #1068

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions src/openai/cli/_api/chat/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def register(subparser: _SubParsersAction[ArgumentParser]) -> None:
"-t",
"--temperature",
help="""What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.

Mutually exclusive with `top_p`.""",
Mutually exclusive with `top_p`.""",
type=float,
)
opt.add_argument(
Expand All @@ -73,6 +72,12 @@ def register(subparser: _SubParsersAction[ArgumentParser]) -> None:
help="A stop sequence at which to stop generating tokens for the message.",
)
opt.add_argument("--stream", help="Stream messages as they're ready.", action="store_true")
opt.add_argument(
"-v",
"--verbose",
help="Display the entire completion response instead of just the content of the completion.",
action="store_true",
)
sub.set_defaults(func=CLIChatCompletion.create, args_model=CLIChatCompletionCreateArgs)


Expand All @@ -90,6 +95,7 @@ class CLIChatCompletionCreateArgs(BaseModel):
top_p: Optional[float] = None
stop: Optional[str] = None
stream: bool = False
verbose: bool = False


class CLIChatCompletion:
Expand All @@ -115,11 +121,17 @@ def create(args: CLIChatCompletionCreateArgs) -> None:
if args.stream:
return CLIChatCompletion._stream_create(cast(CompletionCreateParamsStreaming, params))

return CLIChatCompletion._create(cast(CompletionCreateParamsNonStreaming, params))
return CLIChatCompletion._create(cast(CompletionCreateParamsNonStreaming, params), verbose_output=args.verbose)

@staticmethod
def _create(params: CompletionCreateParamsNonStreaming) -> None:
def _create(params: CompletionCreateParamsNonStreaming, verbose_output: bool = False) -> None:
completion = get_client().chat.completions.create(**params)

# If verbose output requested, print the entire completion
if verbose_output:
sys.stdout.write(completion.model_dump_json(indent=2))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
sys.stdout.write(completion.model_dump_json(indent=2))
sys.stdout.write(completion.to_json())

return

should_print_header = len(completion.choices) > 1
for choice in completion.choices:
if should_print_header:
Expand Down