T
traeai
Sign in
返回首页
Simon Willison's Weblog

Using LLM in the shebang line of a script

7.5Score

TL;DR · AI Summary

LLM can be used directly in the shebang line of a script, supporting tool calls and complex template processing, but use with caution.

Key Takeaways

  • LLM supports defining tools and executing calculations in the shebang.
  • Examples show how to call functions via shebang for math operations.
  • Complex scenarios can leverage Datasette SQL API for data handling.

Outline

Jump quickly between sections.

  1. Introduce the background of LLM in shebang.

  2. Show the simplest LLM shebang example.

  3. Demonstrate enabling tool calls via options.

  4. Explain how to define functions and execute via shebang.

  5. Mention more complex applications combining Datasette API.

Mindmap

See how the topics connect at a glance.

查看大纲文本(无障碍 / 无 JS 友好)
  • LLM in Shebang
    • 基本模式
      • 最简示例
    • 工具调用
      • 选项启用
    • 高级模板
      • 函数定义

Highlights

Key sentences worth saving and sharing.

#LLM#shebang#script#tool calls
Open original article

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:

code
#!/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:

code
#!/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:

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

Which outputs (thanks to that --td tools debug option):

code
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.

AI may generate inaccurate information. Please verify important content.