r/PowerShell • u/ildrynar • Apr 16 '23
ChatGPT Powershell CLI - Simple example on how to use OpenAI's chatgpt api in powershell
I've created a simple PowerShell script that allows you to interact with ChatGPT using the OpenAI API. The script offers a command-line interface where you can ask questions or provide prompts, and it will return ChatGPT's response. Previous prompts and responses are saved so you can have a full conversation with ChatGPT. Check it out as a basic example of how to use the OpenAI API with PowerShell.
Enjoy and let me know what you think!
16
u/SomeAccountNameTaken Apr 16 '23
Be honest, did you ask ChatGPT to write this? :)
20
u/ildrynar Apr 16 '23
hehe, I tried but unfortunately it didn't know how to use it's own API correctly. It kept trying to use some old api endpoints and used incorrect parameters in the request body. That's probably because it's latest training data is from 2021. Anyway, I couldn't get it to build something that worked so had to write it myself sadly :p
I did ask it to write the github readme though, and that turned out ok :)
1
u/Responsible_Leg_9603 Aug 04 '24
you could have directed it via prompts to the new api documentation. that's what i did to make it code for me but that was the project, I'm trying to come up withs ways that neurodivergent individuals can code their ideas more efficiently because the guides almost always require a bit of programming knowledge to even understand, especially for the neurodivergent (of the ones that didn't already know much or any coding)
8
u/blownart Apr 16 '23
How is this different than https://github.com/dfinke/PowerShellAI ?
11
u/ildrynar Apr 16 '23
You're right, I actually came across this module as well and that's when I decided I shouldn't build my script out into a full module but just keep it as a simple example / snippet that you can use as a starting point. :)
Having a full blown module is great but sometimes I just want to build a small self-contained script to automate something.I'm actually thinking I should add an even simpler version just for one-shot ai prompts, to make it even easier to re-use.
3
3
3
u/DougFinke Apr 18 '23 edited Apr 18 '23
I wrote https://github.com/dfinke/PowerShellAI.
Supports both text-in and text-out (gpt | ai | copilot) functions.
PS> gpt 'what is 10+2'PS> git status | ai 'write a commit message'copilotresponds to your prompt, shows the code with line numbers, and you have the open to confirm running, and much more.
Supports conversation-in and message-out with
new-chatandchat. It persists the conversation. Context and memory are enabled for conversations.Supports Azure OpenAI connections.
Download it: Install-Module PowerShellAI
More details: https://github.com/dfinke/PowerShellAI
Video shorts I've made for the features: https://www.youtube.com/playlist?list=PL5uoqS92stXiW1xcAyMa7BMGgX-wdl_KV
2
u/ildrynar Apr 18 '23
Your module is amazing. When I found it I actually gave up on trying to build my own since you've got it all covered already it seems. ๐ Nice clean code as well! Anyway, great work!
2
u/DougFinke Apr 18 '23
๐Thank you!
More features and videos are coming. I suggest starring the repo and and subscribing to my YouTube channel as I and the community add more. Really cool stuff from the community is coming.
Also, I just posted:
Now the Power of PowerShellAI & ChatGPT in Polyglot Interactive Notebooks ๐
I integrated PowerShell interactive notebooks and ChatGPT.
Productivity++
1
Feb 16 '24
Hey DougFinke..
Can you expand a little more on the Set-OpenAIKey part of your instructions? For us noobs. I'm assuming from your instructions you're asking us to execute this command:
Set-OpenAIKey myapikeyWhere myapikey is the Open AI API Key that was generated.
This is the output
Set-OpenAIKey : Cannot process argument transformation on parameter 'Key'. Cannot convert the myapikey value of type "System.String" to type "System.Security.SecureString".
2
Apr 16 '23
I actually wrote something similar over a beer a few nights ago. I should share more. Thanks for thinking of the community. Really helps people :)
2
u/ildrynar Apr 18 '23
Thanks! Yeah, I know how that goes. I don't share enough either. Glad I got around to it this time though cause the feedback has been great. And hopefully it helps a few people save some time ๐
3
1
2
1
u/DougFinke Apr 22 '23 edited Apr 22 '23
๐PowerShellAI Copilot gets a makeover! Syntax highlighting, line numbering & code formatting now added. ๐จ
Thanks to Shaun Lawrie's immense contributions! ๐
More on PowerShellAI: https://github.com/dfinke/PowerShellAI
1
6
u/yamautomate Apr 17 '23
Looks familiar! :)
https://github.com/yamautomate/ShellGPT
Good job though and thanks for sharing!