A ChatGPT-like ChatBot in PowerShell
I built quite a capable but basic ChatGPT-like ChatBot in PowerShell with approx. 200 lines of code. Heres is how a conversation with it looks like (if you can read it...):
Why? Because I wanted to test and play around with OpenAIs ?Completions“ API (gpt-3.5-turbo model) natively in PowerShell and not need to rely on the Python libraries or other modules.
My conclusion: The Completion API is powerful!
As soon as you understand how you need to call it and pass context, it is exceptionally easy to get a conversation going, have something summarized, provide code, fix code errors or much more.
This offers many possibitilites to incorporate the Completion API into PowerShell scripts of all kinds and to programatically ask the API to complete your prompt.
GitHub Repo
I published the Script on GitHub. You may contribute or fork it if you find it helpful.
How-To use it
First of all, you‘ll need the APIKey from OpenAI.
Then to launch the ChatGPT-like PowerShell ChatBot, download the script ?PowerShellOpenAIAPIWrapper.ps1“ from the GitHub Repo linked above.
Then open it in your editor of choice, declare the parameters needed like:
Ultimately, you can start the conversation using the line below, but make sure you loaded the functions beforehand:
Start-ChatGPTforPowerShell -APIKey "YOUR_API_KEY" -model "gpt-3.5-turbo" -temperature 0.5 -stop "\n" -max_tokens 600
How does it work?
Its actually quite simple.
I lay out the main functions below.
领英推荐
New-OpenAIAPIPrompt
This function is used to construct a new prompt for sending to the OpenAI API. The prompt can be used as a new conversation or as a continuation of an existing conversation.?This function returns the prompt, ready for the next function to use it.
Invoke-OpenAIAPI
This function actually calls the OpenAI API using the prompt created by the previous function. It then calls ?New-OpenAIAPIPrompt“ again, once it received the answer from the API, to append the answer to the prompt.
Wrapper functions
These functions do not contain any logic and could also be offloaded to the caller function, but I use them for modularity.
These are:
Start-ChatGPTforPowerShell
This function cobbles together the functions and provides the logic for the ChatBot by using a While-loop to contiously ask for Input. If the user enters ?q“ or ?quit“ he can abort the conversation, export it or start a new one.
Further development
All Messages going back and forth in a conversation are added to the prompt, so that API has enough context.
When the prompt gets too big, the API returns a 400 "Bad Request" Error and this "breaks" the conversation. So I wanted to implement the fuctionality to export and import prompts of previous conversations, so that you can pick it up again. That works, but not when the API returns the 400 Error. Somehow, the export then remains empty. I want to fix that.
I'll also have to think about truncating a few messages from the prompt, when the conversation has been going for a while. That way, I can also make sure that we don't run into the "Bad Request" as we only pass along the messages in the prompt that are needed for context (Instructor plus the last few messages to maintain context.)
Generally speaking, I'd also like to further modularize the code, so that I can easily call the Completion API with whatever I might need to pass it.
An inspiration ist this module.
But wait. Why do you need a ChatBot in PowerShell?
Well, you don't. One may argue about the value of that. But regardless of that discussion, I also think its quite funny to be able to have a natural conversation directly in my PowerShell session. It's great to ask the terminal how to code itself :D
Did ChatGPT help you on this?
Certainly! When I run across errors I asked ChatGPT how to approach that. Sometimes the answers were spot on. Sometimes as wrong as it coud be and it just made up stuff. It needs supervision, but is great for creating comments or documentation for your code.
Encourager for the unconventional ?? | Pushing boundaries in #ecommerce ?? & #retail ??? | Impact on circular economy ??. Talking about practical learnings, failure, and success on my journey ??
1 年??
I like to automate things.
1 年For more information about the ChatGPT API, see OpenAI‘s announcement a few days ago (Whisper API looks interesting as well): https://openai.com/blog/introducing-chatgpt-and-whisper-apis