Skip to content

Commit

Permalink
Read prompt after validating options
Browse files Browse the repository at this point in the history
This means that if you do this:

    llm -m markov -o length -1

You will see an error message rather than have the command hang
waiting for a prompt to be entered on stdin.
  • Loading branch information
simonw committed Jul 7, 2023
1 parent 3548ff0 commit 100758a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,20 @@ def prompt(
Documentation: https://llm.datasette.io/en/stable/usage.html
"""
if prompt is None:
if template:
# If running a template only consume from stdin if it has data
if not sys.stdin.isatty():
prompt = sys.stdin.read()
elif not save:
# Hang waiting for input to stdin (unless --save)
prompt = sys.stdin.read()

model_aliases = get_model_aliases()

def read_prompt():
nonlocal prompt
if prompt is None:
if template:
# If running a template only consume from stdin if it has data
if not sys.stdin.isatty():
prompt = sys.stdin.read()
elif not save:
# Hang waiting for input to stdin (unless --save)
prompt = sys.stdin.read()
return prompt

if save:
# We are saving their prompt/system/etc to a new template
# Fields to save: prompt, system, model - and more in the future
Expand All @@ -141,6 +144,7 @@ def prompt(
to_save["model"] = model_aliases[model_id].model_id
except KeyError:
raise click.ClickException("'{}' is not a known model".format(model_id))
prompt = read_prompt()
if prompt:
to_save["prompt"] = prompt
if system:
Expand All @@ -163,6 +167,7 @@ def prompt(
if system:
raise click.ClickException("Cannot use -t/--template and --system together")
template_obj = load_template(template)
prompt = read_prompt()
try:
prompt, system = template_obj.execute(prompt, params)
except Template.MissingVariables as ex:
Expand Down Expand Up @@ -220,6 +225,8 @@ def prompt(
if not should_stream:
validated_options["stream"] = False

prompt = read_prompt()

try:
response = model.prompt(prompt, system, **validated_options)
if should_stream:
Expand Down

0 comments on commit 100758a

Please sign in to comment.