在脚本 shebang 行中使用 LLM:直接运行自然语言脚本

Using LLM in the shebang line of a script

精选理由

这个技巧把 LLM 变成了 Unix 脚本的一等公民,做自动化或 CLI 工具的开发者可以直接用自然语言写可执行脚本,省去解析参数的麻烦。

AI 摘要

Simon Willison 分享了一个技巧:在脚本的 shebang 行中直接调用 LLM,让自然语言文本文件像可执行脚本一样运行。最简单的用法是 `#!/usr/bin/env -S llm -f`,后面跟自然语言指令即可生成内容(如 SVG)。还可以通过 `-T` 选项调用工具(如获取当前时间写俳句),甚至嵌入 YAML 模板定义 Python 函数作为工具,实现复杂计算。这个模式让 AI 模型无缝融入 Unix 脚本生态,开发者可以直接用自然语言编写可执行脚本。

原文 · Simon Willison’s Weblog

Using LLM in the shebang line of a script

TIL: Using LLM in the shebang line of a script Kim_Bruning on Hacker News : But seriously, you can put a shebang on an english text file now (if you're sufficiently brave) [...] This inspired me to look at patterns for doing exactly that with LLM . Here's the simplest, which takes advantage of LLM fragments : #!/usr/bin/env -S llm -f Generate an SVG of a pelican riding a bicycle But you can also incorporate tool calls using the -T name_of_tool option: #!/usr/bin/env -S llm -T llm_time -f Write a haiku that mentions the exact current time Or even execute YAML templates directly that define extra tools as Python functions: # !/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 Then: ./calc.sh 'what is 2344 * 5252 + 134' --td Which outputs (thanks to that --td tools debug option): Tool call: multiply({'a': 2344, 'b': 5252}) 12310688 Tool call: add({'a': 12310688, 'b': 134}) 12310822 2344 × 5252 + 134 = **12,310,822** Read the full TIL for a more complex example that uses the Datasette SQL API to answer questions about content on my blog. Tags: llm , llm-tool-use , llms , ai , generative-ai

在脚本 shebang 行中使用 LLM:直接运行自然语言脚本 · AI 热点