-
-
Save spullara/0fc3e88150f66179017b9aa1758d49d2 to your computer and use it in GitHub Desktop.
#!/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 |
$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} |
Wonder if you get better results if the prompt starts with:
PROMPT="You are the best at writing shell commands. The output to uname -a is ${uname -a}. [...]"
Wonder if you get better results if the prompt starts with:
PROMPT="You are the best at writing shell commands. The output to uname -a is ${uname -a}. [...]"
What is the intent of this change?
"I know.... PowerShell" (keanu voice) c/o @beatty
What is the intent of this change?
I wonder if, for example, it could correctly provide different arguments for linux/bsd variants of commands.
Got it. Let me see what the best way to do that would be.
Hi, this is amazing, thank you!
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
.
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.
Example: