From abc1e96d1bc3614302d54eaeeddb2e9f3051f8bc Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 17 May 2023 14:08:53 -0700 Subject: [PATCH] Accept prompts sent to standard input, closes #11 --- README.md | 9 +++++++++ llm/cli.py | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 39fe667..c458737 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ To switch from ChatGPT 3.5 (the default) to GPT-4 if you have access: Pass `--model ` 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 diff --git a/llm/cli.py b/llm/cli.py index 95b6703..b506c0b 100644 --- a/llm/cli.py +++ b/llm/cli.py @@ -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"