Skip to content

Instantly share code, notes, and snippets.

@spullara
Last active August 9, 2023 03:15
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
#!/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}
@spullara
Copy link
Author

Example:

chat convert search.json from a line oriented json file to an array
jq -s . search.json > temp.json; mv temp.json search.json
Execute? [n]: 

@noahlt
Copy link

noahlt commented Mar 20, 2023

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}. [...]"

@spullara
Copy link
Author

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?

@spullara
Copy link
Author

"I know.... PowerShell" (keanu voice) c/o @beatty

@noahlt
Copy link

noahlt commented Mar 21, 2023

What is the intent of this change?

I wonder if, for example, it could correctly provide different arguments for linux/bsd variants of commands.

@spullara
Copy link
Author

Got it. Let me see what the best way to do that would be.

@maurelian
Copy link

Hi, this is amazing, thank you!

@maurelian
Copy link

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.

@spullara
Copy link
Author

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