Using LLM in the shebang line of a script
Summary
Simon Willison investigated methods for integrating the LLM CLI tool directly into shebang lines, inspired by a Hacker News comment suggesting English text files could use shebangs. The simplest approach leverages LLM fragments, allowing direct generation requests like creating an SVG of a pelican. More advanced patterns incorporate tool calls using the `-T name_of_tool` option, enabling LLM to execute specific functions, such as fetching the current time for a haiku. The article also demonstrates executing YAML templates directly via `llm -t`, which can define custom Python functions for calculations. An example shows a `calc.sh` script performing arithmetic operations by calling `add` and `multiply` functions defined within the YAML template, outputting the result along with tool debug information.
Key takeaway
For AI Engineers developing command-line utilities, consider embedding LLM CLI commands directly into shebang lines. This approach streamlines script execution for generative tasks or tool-augmented operations, reducing boilerplate. You can define custom Python functions within YAML templates for complex logic, making your scripts more powerful and self-contained. Explore the `-f`, `-T`, and `-t` options to integrate LLM capabilities efficiently into your shell scripts.
Key insights
LLM CLI can execute generative AI tasks and tool calls directly from a script's shebang line.
Principles
- Shebangs can execute LLM commands.
- LLM fragments simplify direct text generation.
- YAML templates enable custom tool definitions.
Method
Integrate `llm` with `-f` for fragments, `-T` for tools, or `-t` for YAML templates directly into a shebang line to execute generative AI tasks or custom functions.
In practice
- Generate SVGs directly from a script.
- Incorporate current time into generated text.
- Define custom arithmetic functions for scripts.
Topics
- LLM CLI tool
- Shebang line
- LLM fragments
- Tool calls
- YAML templates
Best for: AI Engineer, Software Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Simon Willison's Weblog.