一声棒喝,本不立文字
偏要著録,已是二义

simon-willison

在脚本的 shebang 行中使用 LLM

Using LLM in the shebang line of a script

二〇二六年五月十二日 · 英文原文

在脚本shebang行中使用LLM(大语言模型)的模式被提出。通过`llm -f`选项可直接生成SVG图像;使用`-T`选项可整合工具调用,如`llm_time`;YAML模板支持将额外工具定义为Python函数,例如`add`和`multiply`。运行`./calc.sh`并配合`--td`(tools debug)选项可输出工具调用过程及计算结果。该模式由Kim_Bruning在Hacker News上启发,完整示例使用Datasette SQL API回答博客内容相关问题。

TIL:在脚本的 shebang 行中使用 LLM Kim_Bruning 在 Hacker News 上:但说真的,现在你可以在英文文本文件上放一个 shebang 了(如果你足够勇敢的话)[...] 这启发我研究如何用 LLM 实现这一点的模式。下面是最简单的例子,它利用了 LLM fragments(片段):

#!/usr/bin/env -S llm -f
Generate an SVG of a pelican riding a bicycle

你也可以使用 -T name_of_tool 选项来整合工具调用:

#!/usr/bin/env -S llm -T llm_time -f
Write a haiku that mentions the exact current time

甚至可以直接执行 YAML 模板,将额外工具定义为 Python 函数:

# !/usr/bin/env -S llm -t
model: gpt-5.4-mini
system: |
  Use tools to run calculations
functions: |
  def add(a: int, b: int) -> int:
      return a + b
  def multiply(a: int, b: int) -> int:
      return a * b

然后运行:

./calc.sh 'what is 2344 * 5252 + 134' --td

输出(得益于 --td tools debug 选项):

Tool call: multiply({'a': 2344, 'b': 5252})
12310688
Tool call: add({'a': 12310688, 'b': 134})
12310822
2344 × 5252 + 134 = **12,310,822**

阅读完整的 TIL 以获取更复杂的示例,该示例使用 Datasette SQL API 来回答关于我博客内容的问题。

Tags: llm, llm-tool-use, llms, ai, generative-ai

译自 simon-willison · 录于 二〇二六年五月十二日