Accept prompts sent to standard input, closes #11

This commit is contained in:
Simon Willison 2023-05-17 14:08:53 -07:00
parent 45b230b297
commit abc1e96d1b
2 changed files with 13 additions and 1 deletions

View file

@ -33,6 +33,10 @@ To switch from ChatGPT 3.5 (the default) to GPT-4 if you have access:
Pass `--model <model name>` to use a different model.
You can also send a prompt to standard input, for example:
echo 'Ten names for cheesecakes' | llm
### Using with a shell
To generate a description of changes made to a Git repository since the last commit:
@ -48,6 +52,11 @@ You can use `--system '...'` to set a system prompt.
llm 'SQL to calculate total sales by month' -s \
--system 'You are an exaggerated sentient cheesecake that knows SQL and talks about cheesecake a lot'
This is useful for piping content to standard input, for example:
curl -s 'https://simonwillison.net/2023/May/15/per-interpreter-gils/' | \
llm --system 'Suggest topics for this post as a JSON array' --stream
The `--code` option will set a system prompt for you that attempts to output just code without explanation, and will strip off any leading or trailing markdown code block syntax. You can use this to generate code and write it straight to a file:
llm 'Python CLI tool: reverse string passed to stdin' --code > fetch.py

View file

@ -27,7 +27,7 @@ def cli():
@cli.command()
@click.argument("prompt")
@click.argument("prompt", required=False)
@click.option("--system", help="System prompt to use")
@click.option("-4", "--gpt4", is_flag=True, help="Use GPT-4")
@click.option("-m", "--model", help="Model to use")
@ -36,6 +36,9 @@ def cli():
@click.option("--code", is_flag=True, help="System prompt to optimize for code output")
def chatgpt(prompt, system, gpt4, model, stream, no_log, code):
"Execute prompt against ChatGPT"
if prompt is None:
# Read from stdin instead
prompt = sys.stdin.read()
openai.api_key = get_openai_api_key()
if gpt4:
model = "gpt-4"