Last active
October 13, 2025 06:50
-
-
Save codemilli/87dccf27bbd80f2e84c1321263963108 to your computer and use it in GitHub Desktop.
A zsh function that converts natural language queries into shell commands using OpenAI's API.
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
| how() { | |
| emulate -L zsh | |
| setopt NO_GLOB | |
| local query="$*" | |
| local prompt="You are a command line expert. The user is asking how to do something on the command line. Their question: '${query}'. Provide ONLY the exact shell command(s) needed to accomplish this task. No explanations, no markdown, no code blocks - just the raw command(s). If multiple commands are needed, separate them with &&." | |
| local json_payload | |
| json_payload=$(jq -n \ | |
| --arg model "gpt-4o-mini" \ | |
| --arg content "$prompt" \ | |
| '{ | |
| model: $model, | |
| messages: [{ | |
| role: "user", | |
| content: $content | |
| }], | |
| max_tokens: 500, | |
| temperature: 0.3 | |
| }') | |
| local response | |
| response=$(curl -s -X POST "$OPENAI_ENDPOINT" \ | |
| -H "Content-Type: application/json" \ | |
| -H "Authorization: Bearer $OPENAI_API_KEY" \ | |
| -d "$json_payload") | |
| # For debug | |
| # echo "$response" >&2 | |
| local cmd | |
| cmd=$(echo "$response" | jq -r '.choices[0].message.content' | tr -d '\000-\037' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') | |
| print -z -- "$cmd" | |
| } | |
| alias how='noglob how' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment