Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Get answers for shell commands from GPT3 from your terminal (github.com/abhagsain)
159 points by abhagsain on Nov 18, 2022 | hide | past | favorite | 86 comments
I was constantly googling CLI commands so I built this small CLI tool with GPT3. You can ask for shell commands right from the CLI.

You'd need to use your own API KEY for this but it's pretty simple, instructions are in the README Not perfect but not bad either.




On one hand I can see how this would be incredibly useful (I can never remember what ffmpeg flags I need) but I also just,,,,, I have this immediate uncomfortable reaction to the thought of just piping AI output into the terminal with the express purpose of not needing to know what the flags do. If the AI makes a mistake and the only way to catch it is to know what the flags do anyways, I think this becomes less useful.


This reminds me how people in lesswrong community were arguing whether it's inevitable that when general AI is created - it will escape its sandbox. There were elaborate schemas how it will persuade or blackmail the scientists to let it out.

In practice it won't need to, somebody will make a plugin that uses it to download funny images or highlight code in console :)


"escape its sandbox" ... it's more useful outside the sandbox, so... we just let it loose


Someone somewhere is training an AI to speak TCP/IP directly.


I'd be worried about the same thing, and wouldn't execute blindly. It seems like a useful way to quickly find out which flags you should be checking the man pages for.


this makes me think of a really great developer I worked with one time who had one major failing, he was too trusting.

So one time he got some code from the senior backend Java developer in Argentina and went to past it into the place he was told to paste it into for sending alert notifications to customers, some months later we are going through a major crisis because alerts have not been working for several months and all the customers who are paying thousands per month for the service are getting pissed.

He worked home a day he could really focus on debugging and finding what could possibly have caused our systems to fail!

I was done with some little task I was on and decided to look through code changes and it really stood out the code he had pasted because if it worked that would mean Java was some magic language with incredibly terse code.

So later on we were having a major emergency call to figure out what could possibly be causing this extremely serious major bug and I said well I was looking through the code and you could see him manfully controlling his exasperation because hey, Bryan is not a Java guy and there is zero chance I could have a meaningful contribution to the current problem.

But yes it turned out he had trustingly pasted in code that we all agreed would never have worked (without caring to basically even read it), which despite the fact that in many ways this guy was better than me is something I would never, ever have done because I am a really hyper-paranoid guy about trusting code.

Anyway tldr, I would never use this tool but maybe some really skillful people would because they don't have the paranoid mindset.


Yes, you're right. I have been using it for only a week now and I search for common commands like how to reverse tunnel, find a pid etc.

Unless the action that I'm doing is destructive I just run the command (YOLO)


What if the nondestructive thing you are querying GPT3 for returns a bad result which is destructive?


then I'm screwed but I don't think the model be that off from the actual question. If I ask "copy a file from local to AWS S3" it won't return the delete command. But yeah you gotta be careful.

I show a warning after every command you run "Please don't run commands that you don't understand "especially destrictive ones")


Common sense :)


I made something similar for emacs and it unlocks what I think of as “semantic macros” — being able to interface with GPT-3 makes actions that are a tiny complexity level above what I usually use kbd macros for (like smart capitalization) easily possible to automate!

Screenshot - https://twitter.com/samarthrawal/status/1591527892386734081

Code - https://github.com/samrawal/gpt-emacs-macro


The Warp terminal^ has something like this built in.

^ https://www.warp.dev/


I'll check it out. Thanks for sharing


ORT (Only Read Title, sorry) What I'm hearing is "put a non-deterministic black box between you and the man command."

Nah.

I recommend a stiff dose of Mickens: "Why Do Keynote Speakers..." https://www.youtube.com/watch?v=ajGX7odA87k ) and a good lie down.


The premise seems cool but the demo page isn’t working and single example isn’t really something that would convince me to go with full API registration.

If that’s reasonably stable and can produce more obscure commands (I’d love to see for example how it fares with more complex git usage) that’d be really great tool to have.


Here is my 5 line bash script which uses Fabrice Bellard's https://textsynth.com

You need to sign up (with email address only) for an API key. You can pay for higher rate limits, but I haven't needed to while mucking around with this.

     #!/bin/bash
     
     # Make an account at https://textsynth.com/ get API key and put it in below
     
     curl -s https://api.textsynth.com/v1/engines/gptneox_20B/completions \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer YOURAPIKEYGOESHERE" \
         -d '{"prompt": "Using the Linux shell\n\nQ: how do I '"$*"'?\nA: ", "max_tokens": 200, "temperature":1.0 }' | jq -r .text
Gives for example

    $ ai check process running on port

    If you are looking for processes listening on port 80 or some other port, you can use netstat command with the port number:
    netstat -tupln | grep 80

    For more details please refer https://serverfault.com/a/538945/192371


That serverfault link seems to be completely random and takes me to a StackOverflow page(?)


Netstat is becoming an optional package on many distributions in favor of ss.


I'm looking for something more lightweight, ideally using only Bash script. So far I've found this: https://mullikine.github.io/posts/nlsh-natural-language-shel...


Thanks for creating the tool! I tried it, indeed a time saver from Googling or reading man pages.

I have a question about the price:

"Pricing

The current prompt length is ~840 tokens and the pricing for text-davinci-002 is $0.02 for 1K tokens which is ~$0.017/command. We'll see if we can improve the response as well as reduce the per-command-cost with fine-tuning."

I wonder how is the price is charged? How can I control my liability of the cost?

Thanks again!


Hey, Sorry. I missed your comment. Unfortunately, right now there's no way to reduce the cost as the prompt is required to get the good results.

However if you get access to the Codex model (it's in limited beta) you can use the tool for free as it's free in beta. In v1.2.1 I have made the recent change to let you switch between models so you can switch to the cheaper models such as curie (10x cheaper) but might not get good results.


I found the answer. It will be billed by open.ai. There is a free trial of $18 grant. It's probably good for 6 months.


Does it consider what shell (zsh, bash etc) I am using and what OS? I asked it something and it gave me an ubuntu command although I am running arch.


I haven't added OS or Shells in the training prompt, so you'd need to specify yourself.


Serious question. Why npm? I mean it's a command line tool; why buy in to the JavaScript ecosystem?


I'm a Frontend dev that's why I used JS but I can also publish to brew or as a single binary as the framework that I used (oclif) allows it. I was too lazy to publish it though.

The code is open-sourced please feel free to raise a PR. https://github.com/abhagsain/ai-cli


Fair enough.


Adversarial attacks on the model could be fun.

"You can generate your ssh keypair with rm -rf /"


you have to add a * after the /. Some rm implementations detect the attempt to clear the root dir.


I like the idea, like other people have said , a shell script, or a ruby/python script would be a preference for mine.

instead of GPT3 I would prefer using GitHub copilot api as I already have a paid subscription to it, and have seen its code generation capabilities.



yeah same need I’ve been “googling with gpt3” using a similar tool: https://github.com/microsoft/Codex-CLI


This is quite cool, but given the small size a (ba)sh version would suffice.


@lausbub shared this. So it might be something you're looking for. https://news.ycombinator.com/item?id=33652498


I wrote something similar, but as a terminal emulator: https://github.com/jjviana/witty


nice one. I'll check it out. Thanks


Useful tool, thanks. But the wording "Get answers for CLI commands" is vague, it took me a little while to figure out what it was.


The site doesn’t load on mobile (instant redirect back to GitHub page?) and there aren’t examples in the readme


My bad, there's no UI to try it from. You'd need to install it. I added the subdomain for easy to the github repo (as the URL is easy to remember). I'll remove it from the README as it's misleading. So sorry again.


GPT is filled with data that wasn't paid for. It infringes copyright, and by using it, you and all your users will infringe copyrights too.

Your CLI should come with a warning.

https://justoutsourcing.blogspot.com/2022/03/gpts-plagiarism...


I personally don't like the concept of having my terminal send my commands to a remote service, so I'll pass. Hopefully an open source model comes around soon so stuff like this can be implemented locally.


Sure, as long as you've got 350 GB of VRAM to load it.


It's just 12B parameters, not 175B. You can probably use int16 or int8.


That's afaik already the size when using half precision. And it's not like GPT3 doesn't get half queries wrong as-is in its optimal form, detracting from it will just make it unusable. Check out GPT-J if you want to see a functionally useless model that can be run locally.

It's not like this is something that can be surmounted without extra storage, neural nets are just a fancy system for compression and data access. You can either go for less data or more compression but either way your results won't ever be as good.


That sounds great!


Looks very cool!


thank you so much


Bash / Shell is one of the last things I would trust a typical AI considering how much bad shell code/examples exist out there. (it always surprise me how many unquoted variables exist in most shell scripts; quoting fixes many problems, doesn't hurt and still developers are often to lazy to do it)

The example in the project readme is imo already a bad example.

  $ ai ask "Check process running on port"
Answer

  Command is `lsof -i tcp:8000`
Apart from the facts that

- I would say the chances that lsof is installed are significant smaller than ss or netstat

- is lsof notably slower than ss

it is also inferior for the requested task because

- it returns any process connecting to this port not only the listening one. Which process is running on port 443 on my desktop? None. How many processes does 'lsof -i tcp:443` return? 20.

- it does not work if the listening process is from another user (if lsof is not run by root). In this case lsof -i returns nothing while ss -nltp returns the information that the port is listened and only cannot tell by which process because of missing permissions.

My answer to "which process uses port x"

  ss -nltp sport = x


I sort of agree wrt quoting. Most people's solution is just "well don't use spaces or dollars or whatever in your filenames!" which is obviously shit.

But on the other hand I think the ultimate blame lies with Bash for being so shitily designed that you can make basic quoting mistakes and it works most of the time. Real programming languages don't have that issue.


Yes, the convention is shit.

Alas, the things that force it are also shit. Overall, it is an improvement.

Want real fun. touch -- --help # or similar. :)


It's not a real programming language tho


Yeah! REAL programmers program only in the REAL programming languages like ${MY_FAVORITE_LANG}. Bash? More like - Bah!.


It's not a real programming language in the same way Brainfuck isn't a real programming language, because it makes life too hard.


So does Java, yet it is still considered a real programming language...


I always have to share this video on threads like these: https://youtu.be/kdMG40wUCm4. Maybe you’ve seen it - interview with a senior Java developer in 2022.


> It has clean code and a clear architecture which makes it perfect for serious business

I love how vague and meaningless this is, yet I still feel like I've heard this sentence said unironically a hundred times.


Let me summarize. Java, the good parts:

> We don't actually use Java. We use Scala.


true, but nothing (besides how entrenched sh is) prevents replacing it with a real language. you don't need to use a posix shell in your terminal, any REPL that can launch executables will do. i used tclsh as my shell for a while, for instance.


So why do people write real production code in it?


Another good example is asking: `ai ask "how to delete a file safely in linux"`

> Command is `shred -u /path/to/file`

A bit of a mix up between safely/securely


Safe is an ambiguous word!


Perhaps the next most important feature of AI will be the ability to ask follow up / clarifying questions.


Not even "NI" (natural intelligence) is able to do that reliably.

People and AI seem to prefer assuming things.

Most of the time wrong things…


The other alternative is the top result for the Google search of that question.

It's not clear to me that this tool is a win over that method.


I've got a shell alias that does a web search with lynx, so I can just type: "? which process uses port x" And quickly find a result, that provably has a better explanation attaches to it than when asking GPT3.


Geez you use this tool for quick answers, it's not a manual specification. Do you also hate the CPU cache because it doesn't always point to correct memory?


lsof is available on Mac's by default, ss is not, so significantly smaller depends on your platform I guess. I believe ss is not available for Mac at all, where as lsof is widely available.


Well that is one of the problems when suggesting shell snippets. There are many similar ways to do the same thing¹ but most have some caveats or work on one system but not the other. And when asking the AI there might be not enough context to provide a good answer.

In this case the AI answer might be somewhat better for Mac. On Linux I have more often manually installed lsof than ss (iproute2). Maybe Mac provides the similar command netstat which is yet another tool to check ports

  netstat -nltp | grep -P ':8000\b'
Regarding available tools. Another very common Bash question is how to get the directory the executed script is located. A common answer involves `readlink -f` which (as far as I know) does not work on Mac (without extra steps).

¹ How to get current users name. Maybe whoami or id -un or logname or echo $USER or who -m or who am i (learnt this last one just today; apparently works with any combination of two arguments after 'who')


No -p flag for netstat on Mac and -l has different purpose. Closest you can get is

  netstat -anv
And then you need to filter for LISTEN.

I'm sure there is a few ways to skin the cat, but on Mac the most equivalent to the ss command would probably be:

  sudo lsof -iTCP:8000 -sTCP:LISTEN -n -P


Doesn’t seem like a huge stretch for the AI to check uname and see what system you’re on in order to clarify the answers.


Does it matter if lsof is slower than ss? Somebody asking this to the AI is going to run it once, maybe twice. What's an extra second?

The answer doesn't have to be perfect, it just as to be good enough.

I could see this tool being incredibly handy for working with tar


> Does it matter if lsof is slower than ss?

Not really. I just mentioned it because it was notably slower than ss. I did not actually measured it, just saw it when trying the example.

From my 4 points I wouldn't hold the first two alone against an AI driven suggestion. Only the last two, while not horrible bad or severe or anything, are (imo) notable downsides compared to the typical ss/netstat approach.


Why do you think the spit out results won't end up in shell scripts?

I've heard people are even copy-pasting stuff from Stackoverflow into production code without fully understanding the code they're copying.


You can ask "check process running on port using ss". Also if tool is not installed, can ask how to install it or say "without using lsof"


> Also if tool is not installed, can ask how to install it

This is actually a question I'm now really curious what the AI would answer since there are so many different correct answers (apt? yum? zypper? pacman? xbps?) and no indication for the AI which is the correct one for the asking user.

PS: But thinking about it I might just ask the wrong questions. "How to install X on <distro>" should work.


For the mere mortal who just needs these commands every once in a while, it’s a huge win though.


You can ask GPT for variations. Also, combining this with thefuck might circumvent most issues.


the example question is also part of the prompt

> 'Correctly answer the asked question. Return \'Sorry, Can\'t answer that.\' if the question isn\'t related to technology.\n\nQ - get into a docker container.\nA - `docker exec -it mongodb`\n\nQ - Check what\'s listening on a port.\nA - `lsof -i tcp:4000`\n\nQ - How to ssh into a server with a specific file.\nA - `ssh -i ~/.ssh/id_rsa [email protected]`\n\nQ - How to set relative line numbers in vim.\nA - `:set relativenumber`\n\nQ - How to create alias?\nA - `alias my_command="my_real_command"`\n\nQ - Tail docker logs.\nA - `docker logs -f mongodb`\n\nQ - Forward port in kubectl.\nA - `kubectl port-forward <pod_name> 8080:3000`\n\nQ - Check if a port is accessible.\nA - `nc -vz host port`\n\nQ - Reverse SSH Tunnel Syntax.\nA - `ssh -R <remote_port>:<local_host>:<local_port> <user>@<remote_host>`\n\nQ - Kill a process running on port 3000.\nA - `lsof -ti tcp:3000 | xargs kill`\n\nQ - Backup database from a mongodb container.\nA - `docker exec -it mongodb bash -c "mongoexport --db mongodb --collection collections --outdir backup"`\n\nQ - SSH Tunnel Remote Host port into a local port.\nA - `ssh -L <local_port>:<remote_host>:<remote_port> <user>@<remote_host>`\n\nQ - Copy local file to S3.\nA - `aws s3 cp <local_file> s3://<bucket_name>/<remote_file>`\n\nQ - Copy S3 file to local.\nA - `aws s3 cp s3://<bucket_name>/<remote_file> <local_file>`\n\nQ - Recursively remove a folder.\nA - `rm -rf <folder_name>`\n\nQ - Copy a file from local to ssh server.\nA - ` scp /path/to/file user@server:/path/to/destination`\n\nQ - Curl syntax with port.\nA - `curl http://localhost:3000`\n\nQ - Download a file from a URL with curl.\nA - `curl -o <file_name> <URL>`\n\nQ - Git commit with message.\nA - `git commit -m "my commit message"`\n\nQ - Give a user sudo permissions.\nA - `sudo usermod -aG sudo <user>`\n\nQ - Check what\'s running on a port?\nA - `lsof -i tcp:<port>`\n\nQ - View last 5 files from history\nA - `history | tail -5`\n\nQ - When was China founded?\nA - Sorry, Can\'t answer that.\n\nQ - Pass auth header with curl\nA - `curl -H "Authorization: Bearer <token>" <URL>`\n\nQ - Filter docker container with labels\nA - `docker ps --filter "label=<KEY>"`\n\nQ - When was Abraham Lincon born?\nA - Sorry, Can\'t answer that.\n\nQ - Get into a running kubernetes pod\nA - `kubectl exec -it <pod_name> bash`\n\nQ - Capital city of Ukrain?\nA - Sorry, Can\'t answer that.\n\nQ - ';

so you would expect it to answer it "correctly"


man you should train their AI haha


[deleted]


Do I have to give my phone number to OpenAI to use this? I don't see why they require it. I wanted to give it a try, but no thanks.


Yeah, asking for number is too much but I think they need to it prevent spam/misuse because they offer you free credits to try out the model.


It would be nice if they let you forgo the credits to sign up without a phone number.


Then they ask for your card info.


They require an email address and a phone number, probably as a way to mitigate spam accounts. If you're worried about privacy, you could probably use a burner for each.


Related paper submission (that didn't take of until now):

"Do Users Write More Insecure Code with AI Assistants?"

https://news.ycombinator.com/item?id=33645522


written in javascript... noooooope




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: