模板#

一个模板可以将提示、系统提示、模型、默认模型选项、模式和片段组合成一个可重复使用的单元。

一次只能使用一个模板。要将多个较短的提示片段组合在一起,请考虑使用片段

开始使用 –save#

创建模板最简单的方法是使用 --save template_name 选项。

下面是创建文本摘要模板的方法

llm '$input - summarize this' --save summarize

$input 放在你希望插入用户输入的地方。如果你省略这个,他们的输入将被添加到常规提示的末尾。

llm 'Summarize the following: ' --save summarize

你也可以使用系统提示创建模板

llm --system 'Summarize this' --save summarize

你可以使用 --model 为模板设置默认模型。

llm --system 'Summarize this' --model gpt-4o --save summarize

你也可以保存默认选项

llm --system 'Speak in French' -o temperature 1.8 --save wild-french

如果你想在提示中包含字面意义的 $ 符号,请改用 $$

llm --system 'Estimate the cost in $$ of this: $input' --save estimate

添加 --schema模式 嵌入到你的模板中。

llm --schema dog.schema.json 'invent a dog' --save dog

如果你添加 --extract提取第一个围栏代码块的设置将保存在模板中。

llm --system 'write a Python function' --extract --save python-function
llm -t python-function 'reverse a string'

在上述每种情况下,模板都将以 YAML 格式保存在磁盘上的一个专用目录中。

使用模板#

你可以使用 -t/--template 选项执行命名的模板。

curl -s https://example.com/ | llm -t summarize

这可以与 -m 选项结合使用以指定不同的模型。

curl -s https://llm.datasette.com.cn/en/stable/ | \
  llm -t summarize -m gpt-3.5-turbo-16k

模板也可以指定为 YAML 文件的完整 URL。

llm -t https://raw.githubusercontent.com/simonw/llm-templates/refs/heads/main/python-app.yaml \
  'Python app to pick a random line from a file'

或者作为磁盘上 YAML 文件的直接路径。

llm -t path/to/template.yaml 'extra prompt here'

列出可用模板#

此命令列出所有可用模板。

llm templates

输出如下所示

cmd        : system: reply with macos terminal commands only, no extra information
glados     : system: You are GlaDOS prompt: Summarize this:

YAML 文件格式的模板#

模板以 YAML 文件形式存储在磁盘上。

你可以使用 llm templates edit 命令编辑(或创建)模板的 YAML 文件。

llm templates edit summarize

这将打开系统默认编辑器。

提示

你可以使用 EDITOR 环境变量控制此处使用的编辑器 - 例如,使用 VS Code。

export EDITOR="code -w"

将其添加到你的 ~/.zshrc~/.bashrc 文件中,具体取决于你使用的 shell(自 macOS Catalina 于 2019 年发布以来,zsh 是 macOS 上的默认 shell)。

你可以直接在模板目录中创建或编辑模板文件。此目录的位置由 llm templates path 命令显示。

llm templates path

示例输出

/Users/simon/Library/Application Support/io.datasette.llm/templates

一个基本的 YAML 模板看起来像这样

prompt: 'Summarize this: $input'

或者对于较长的输入,使用 YAML 多行字符串。我使用 llm templates edit steampunk 创建了这个。

prompt: >
    Summarize the following text.

    Insert frequent satirical steampunk-themed illustrative anecdotes.
    Really go wild with that.

    Text to summarize: $input

prompt: > 会使后面的缩进文本被视为单个字符串,换行符会被折叠成空格。使用 prompt: | 可以保留换行符。

使用 llm -t steampunk 对抗 GPT-4o 运行(通过 strip-tags 从输入中删除 HTML 标签并压缩空白)。

curl -s 'https://til.simonwillison.net/macos/imovie-slides-and-audio' | \
  strip-tags -m | llm -t steampunk -m gpt-4o

输出

在一个充满幻想的蒸汽朋克世界里,Simon Willison 决定使用 iMovie 将一段旧的 MP3 录音与演讲的幻灯片合并。将幻灯片导出为图像并导入到 iMovie 后,他必须使用“裁剪”工具禁用默认的 Ken Burns 效果。然后,Simon 通过调整每张图像的持续时间来手动同步音频。最后,他将这部杰作发布到 YouTube,蒸汽朋克风格插画的奇幻魔力让他的观众惊叹不已。

系统提示#

当使用支持系统提示的模型时,你可以使用 system: 键设置系统提示,如下所示。

system: Summarize this

如果你只指定一个系统提示,则无需使用 $input 变量 - llm 会将用户的输入作为整个常规提示,然后根据该系统提示中设置的指令进行处理。

你可以像这样结合使用系统提示和常规提示。

system: You speak like an excitable Victorian adventurer
prompt: 'Summarize this: $input'

片段#

模板可以使用 fragments:system_fragments: 键引用 片段。这些应该是一个片段 URL、文件路径或哈希的列表。

fragments:
- https://example.com/robots.txt
- /path/to/file.txt
- 993fd38d898d2b59fd2d16c811da5bdac658faa34f0f4d411edde7c17ebb0680
system_fragments:
- https://example.com/systm-prompt.txt

选项#

可以使用 options: 键设置默认选项。

name: wild-french
system: Speak in French
options:
  temperature: 1.8

模式#

使用 schema_object: 键在模板中嵌入 JSON 模式(以 YAML 格式)。创建这些的最简单方法是使用 llm --schema ... --save name-of-template 命令 - 结果应该看起来像这样。

name: dogs
schema_object:
    properties:
        dogs:
            items:
                properties:
                    bio:
                        type: string
                    name:
                        type: string
                type: object
            type: array
    type: object

额外的模板变量#

处理用户正常提示输入(通过标准输入管道或作为命令行参数传递的内容)的模板可以使用 $input 变量。

你可以使用额外的命名变量。在执行模板时,需要使用 -p/--param 选项提供这些变量。

这里有一个名为 recipe 的示例 YAML 模板,你可以使用 llm templates edit recipe 命令创建它。

prompt: |
    Suggest a recipe using ingredients: $ingredients

    It should be based on cuisine from this country: $country

可以像这样执行它

llm -t recipe -p ingredients 'sausages, milk' -p country Germany

我的输出是这样开始的

食谱:德式香肠土豆汤

配料

  • 4 根德式香肠

  • 2 杯全脂牛奶

此示例将通过管道输入到工具的内容与附加参数结合起来。将其命名为 summarize

system: Summarize this text in the voice of $voice

然后运行它

curl -s 'https://til.simonwillison.net/macos/imovie-slides-and-audio' | \
  strip-tags -m | llm -t summarize -p voice GlaDOS

我得到了这个

我的上一个测试对象似乎对 iMovie 有了新的认识。他们将 Keynote 幻灯片导出为单独的图像 [...] 对于一个人类来说,这相当令人印象深刻。

指定默认参数#

使用 --save 选项创建模板时,你可以传递 -p name value 来存储参数的默认值。

llm --system 'Summarize this text in the voice of $voice' \
  --model gpt-4o -p voice GlaDOS --save summarize

你可以使用 YAML 中的 defaults: 键指定参数的默认值。

system: Summarize this text in the voice of $voice
defaults:
  voice: GlaDOS

当不带 -p 运行时,它将选择默认值。

curl -s 'https://til.simonwillison.net/macos/imovie-slides-and-audio' | \
  strip-tags -m | llm -t summarize

但你可以使用 -p 覆盖默认值。

curl -s 'https://til.simonwillison.net/macos/imovie-slides-and-audio' | \
  strip-tags -m | llm -t summarize -p voice Yoda

我得到了这个

文本,用尤达大师的声音总结,我会:“嗯,年轻的绝地学徒。文本的总结,你寻求。嗯......

配置代码提取#

要为模板配置提取第一个围栏代码块的设置,请添加:

extract: true

为模板设置默认模型#

使用 llm -t template-name 执行的模板将使用用户为该工具配置的默认模型执行 - 如果他们没有配置自己的默认模型,则使用 gpt-3.5-turbo

你可以使用相关 YAML 文件中的 model: 键为模板指定新的默认模型。这里有一个名为 roast 的模板。

model: gpt-4o
system: roast the user at every possible opportunity, be succinct

示例

llm -t roast 'How are you today?'

我过得很好,但对于你这些无聊的问题,我必须承认,我在墓地里看到的生命力都比这多。

来自插件的模板加载器#

LLM 插件可以注册前缀,这些前缀可用于从外部来源加载模板。

llm-templates-github 是一个示例,它添加了 gh: 前缀,可用于从 GitHub 加载模板。

你可以像这样安装该插件

llm install llm-templates-github

使用 llm templates loaders 命令查看已注册加载器的详细信息。

llm templates loaders

输出

gh:
  Load a template from GitHub or local cache if available

  Format: username/repo/template_name (without the .yaml extension)
    or username/template_name which means username/llm-templates/template_name

然后你可以这样使用它

curl -sL 'https://llm.datasette.com.cn/' | llm -t gh:simonw/summarize

curl-sL 标志用于跟随重定向并抑制进度条。

此命令将获取 LLM 索引页面的内容,并将其提供给 simonw/llm-templates GitHub 仓库中由 summarize.yaml 定义的模板。

如果两个模板加载器插件尝试注册相同的前缀,其中一个的前缀末尾将添加 _1。使用 llm templates loaders 检查是否发生了这种情况。