Created
July 9, 2025 00:05
-
-
Save chriscarrollsmith/ecb57d8aa206759c4525f8df4fb6d977 to your computer and use it in GitHub Desktop.
Create a natural-language version of any command-line tool, powered by `llm`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# create-nl-tool.sh - Programmatically create natural language interfaces | |
# for CLI tools | |
# Usage: ./create-nl-tool.sh <tool-name> | |
# Example: ./create-nl-tool.sh repomix | |
set -e | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 <tool-name>" | |
echo "Example: $0 repomix" | |
echo "" | |
echo "This script will:" | |
echo "1. Check if the tool exists and has --help" | |
echo "2. Create an LLM template for the tool" | |
echo "3. Generate a natural language wrapper script" | |
exit 1 | |
fi | |
TOOL_NAME="$1" | |
NL_TOOL_NAME="nl-${TOOL_NAME}" | |
TEMPLATE_NAME="${TOOL_NAME}" | |
# Check if the tool exists | |
if ! command -v "$TOOL_NAME" &> /dev/null; then | |
echo "Error: Tool '$TOOL_NAME' not found in PATH" | |
exit 1 | |
fi | |
# Check if the tool has --help option | |
if ! "$TOOL_NAME" --help &> /dev/null; then | |
echo "Error: Tool '$TOOL_NAME' does not support --help option" | |
exit 1 | |
fi | |
echo "Creating natural language interface for '$TOOL_NAME'..." | |
# Step 1: Create the LLM template | |
echo "Step 1: Creating LLM template..." | |
HELP_OUTPUT=$("$TOOL_NAME" --help | sed 's/\$/\\$/g') | |
# Create the system prompt for the LLM template | |
SYSTEM_PROMPT="Use the help output to generate a '$TOOL_NAME' command to achieve \ | |
the user's objective(s). Only return the command, no other text. | |
$HELP_OUTPUT" | |
llm --system "$SYSTEM_PROMPT" --save "$TEMPLATE_NAME" | |
echo "✓ Template '$TEMPLATE_NAME' saved" | |
# Step 2: Create the natural language wrapper script | |
echo "Step 2: Creating wrapper script '$NL_TOOL_NAME'..." | |
# Generate a context-appropriate example prompt | |
EXAMPLE_PROMPT=$(llm "Write an example user prompt, <=15 words, for accomplishing \ | |
something with $TOOL_NAME. Imagine concrete details for the hypothetical use case. \ | |
For example, for the 'mv' command, the user might write, 'Move the README file to \ | |
my home directory, please.' Output just the prompt with no other text.\n$HELP_OUTPUT") | |
cat > "$NL_TOOL_NAME" << EOF | |
#!/bin/bash | |
# $NL_TOOL_NAME - Natural language interface to $TOOL_NAME | |
# Usage: $NL_TOOL_NAME "<natural language prompt>" | |
# Example: $NL_TOOL_NAME "$EXAMPLE_PROMPT" | |
if [ \$# -eq 0 ]; then | |
echo "Usage: $NL_TOOL_NAME \"<natural language prompt>\"" | |
echo "Example: $NL_TOOL_NAME \"$EXAMPLE_PROMPT\"" | |
exit 1 | |
fi | |
# Pass the prompt to llm with the $TOOL_NAME template, extract the command, | |
# and execute it | |
llm "\$1" --template $TEMPLATE_NAME -x | bash | |
EOF | |
# Make the script executable | |
chmod +x "$NL_TOOL_NAME" | |
echo "✓ Wrapper script '$NL_TOOL_NAME' created and made executable" | |
# Step 3: Provide usage instructions | |
echo "" | |
echo "Setup complete! To use your natural language interface:" | |
echo "" | |
echo "1. Test it locally:" | |
echo " ./$NL_TOOL_NAME \"your natural language request\"" | |
echo "" | |
echo "2. To make it available system-wide, move it to your PATH:" | |
echo " sudo mv $NL_TOOL_NAME /usr/local/bin/" | |
echo " # Or add current directory to PATH temporarily:" | |
echo " export PATH=\"\$PATH:\$(pwd)\"" | |
echo "" | |
echo "3. Example usage:" | |
if [ "$TOOL_NAME" = "repomix" ]; then | |
echo " $NL_TOOL_NAME \"Pack all the Python files in the current folder\"" | |
echo " $NL_TOOL_NAME \"Create a repository pack excluding node_modules\"" | |
else | |
echo " $NL_TOOL_NAME \"<describe what you want to do with $TOOL_NAME>\"" | |
fi | |
echo "" | |
echo "Note: Make sure you have 'llm' installed and configured." | |
echo "Visit https://llm.datasette.io/ for installation instructions." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment