-
-
Save spullara/0fc3e88150f66179017b9aa1758d49d2 to your computer and use it in GitHub Desktop.
Use this command to get suggestions on how to do things on the command line.
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 | |
TOKEN=< OpenAI token from https://platform.openai.com/account/api-keys > | |
PROMPT="You are the best at writing shell commands. Assume the OS is Ubuntu. I want you to respond with only the shell commands separated by semicolons and no commentary. Here is what I want to do: $@" | |
RESULT=`curl -s https://api.openai.com/v1/chat/completions \ | |
-H 'Content-Type: application/json' \ | |
-H "Authorization: Bearer $TOKEN" \ | |
-d "{ | |
\"model\": \"gpt-3.5-turbo\", | |
\"messages\": [{\"role\": \"user\", \"content\": \"$PROMPT\"}] | |
}" | jq '.choices[] | .message.content' -r` | |
echo $RESULT | |
read -rp "Execute? [n/y/c]: " input_var | |
input_var=${input_var:-n} | |
[ "$input_var" = "y" ] && bash -c "$RESULT" | |
[ "$input_var" = "c" ] && echo "$RESULT" | pbcopy |
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
$TOKEN="< OpenAI token from https://platform.openai.com/account/api-keys >" | |
$concatArgs = "" | |
for($i=0;$i -lt $args.count;$i++) { | |
# Concatenate the argument to the variable with a space added in between | |
$concatArgs += $args[$i] + " " | |
} | |
$PROMPT = "You are the best at writing shell commands. Assume the OS is Windows and you are using PowerShell. I want you to respond with only the shell commands separated by semicolons and no commentary. Here is what I want to do: $concatArgs" | |
$RESULT = Invoke-RestMethod -Method Post -Uri "https://api.openai.com/v1/chat/completions" -ContentType "application/json" -Headers @{'Authorization' = "Bearer $TOKEN"} -Body (ConvertTo-Json @{'model' = 'gpt-3.5-turbo'; 'messages' = @(@{'role' = 'user'; 'content' = $PROMPT})}) | Select-Object -ExpandProperty choices | Select-Object -ExpandProperty message | Select-Object -ExpandProperty content | |
Write-Host $RESULT | |
$input_var = Read-Host -Prompt "Execute? [n]" | |
$input_var = $input_var -replace "\s+", "" | |
if ([String]::IsNullOrEmpty($input_var)) {$input_var = "n"} | |
if ($input_var -eq "y") {Invoke-Expression $RESULT} |
small improvement that makes a big difference for me in the last few lines:
read -rp "Execute? [y/c(opy)]: " input_var input_var=${input_var:-y} [ "$input_var" = "y" ] && bash -c "$RESULT" [ "$input_var" = "c" ] && echo "$RESULT" | pbcopy
If the output is close, but not quite right you can copy to the clipboard and edit it by responding with
c
.
Added.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
small improvement that makes a big difference for me in the last few lines:
If the output is close, but not quite right you can copy to the clipboard and edit it by responding with
c
.