Except you’ve been off the grid on an extended sabbatical the final six months you’ve certainly heard of ChatGPT. Like many I began exploring the probabilities of OpenAI ChatGPT when it hit the world by storm final yr. Fairly rapidly I noticed the facility of it, but in addition questioned about utilizing it through API. Then in January this yr Doug Finke a Microsoft MVP launched the primary beta of his PowerShellAI module. This opened up the probabilities to make use of Generative AI (ChatGPT) with PowerShell and thereby VSCode.
The PowerShellAI module has developed extensively since then as each OpenAI and Microsoft have launched further capabilities and the module has been prolonged with further cmdlets to work together with them.
On this submit I’m going to point out how I’ve been utilizing the module within the following situations:
- ChatGPT 3.5-turbo and ChatGPT-4
- Producing content material with Generative AI with a PowerShell GUI
- Copilot for PowerShell
- Context (session) based mostly ChatGPT with PowerShell
- Producing Photos with PowerShell
Getting began with the PowerShellAI Module
The newest model is offered from each Doug Finke’s GitHub Repo and the PowerShell Gallery. Consider this module remains to be below fixed refinement. Hold checking again for updates principally from Doug but in addition from PRs from the neighborhood.
The quickest methodology is to put in from the PowerShell Gallery.
Set up-Module -Identify PowerShellAI
If upgrading from a earlier model, you’ll need to make use of the -force parameter.
Set up-Module -Identify PowerShellAI -force
Acquiring an OpenAI API Key
To make use of the OpenAI massive language modules (LLM) with the PowerShellAI Module you’ll need an API Key. Create an OpenAI Account and generate an API Key from right here. OpenAI will provide you with $5 credit score that’s legitimate for 3 months. That permits numerous experimenting. I didn’t use all of that $5 earlier than the three months have been up. After the three months you’ll need to offer a billing choice (bank card) to proceed to make use of their fashions. Alternatively presently you might change over to Azure OpenAI if in case you have a subscription with out there month-to-month credit. I’m not going to element Azure OpenAI as a part of this submit.
Configuring the PowerShellAI Module along with your OpenAI API Key
There are a few methods of doing this. The way in which I began doing it within the early iterations of the module is to generate a safe credential and put it aside as an xml file which on Home windows is simply ever in a position to be learn by the identical profile on the identical host that created the file.
With the final two strains I used to be importing my API Key file and reversing the safe credential password again to clear textual content and setting the OpenAIKey setting variable. The instance to try this is under.
$env:OpenAIKey = $chatGPTAPIKey
Subsequently with the addition of the Set-OpenAIKey cmdlet this may be simplified to the next which is now included on the prime of all my PowerShellAI scripts. Import the beforehand securely exported credential and set the OpenAIKey variable utilizing the Set-OpenAIKey cmdlet.
Utilizing ChatGPT 3.5-turbo and ChatGPT-4 with PowerShell
Now now we have the PowerShellAI PowerShell Module put in and the environment configured with an OpenAI API Key we are able to begin utilizing it.
By default, the current variations of the PowerShellAI Module default to GPT-4. You’ll be able to confirm utilizing the Get-ChatSessionOptions cmdlet.
The mannequin could be modified (together with different parameters) utilizing the Set-ChatSessionOptions cmdlet. The next picture exhibits switching to the GPT-3.5-turbo LLM.
Except you’ve added your self to the GPT-4 waitlist and been accepted you will be unable to make use of the GPT-4 mannequin.
Aside from altering fashions I’ve solely ever used Set-ChatSessionOptions to vary the max_tokens and temperature for particular use instances up entrance for a session. When you simply want to vary it briefly for a question or two it’s also possible to specify max_tokens and temperature on the chat cmdlets.
If you’re new to massive language fashions you’ll doubtless wish to know what Tokens are. Succinctly they’re how the AI language mannequin providers take enter and interpret the textual content strings to then generate the following tokens (the response). The OpenAI Tokenizer will allow you to perceive how that’s completed.
A useful rule of thumb is that one token typically corresponds to ~4 characters of textual content for widespread English textual content. This interprets to roughly ¾ of a phrase (so 100 tokens ~= 75 phrases).
An API for accessing new AI fashions developed by OpenAI
The extra tokens you configure your session for the bigger the enter and responses will likely be. Totally different fashions have totally different sizes for max_tokens and the billing to be used additionally differs. On the time of this submit the present pricing is proven under.
The opposite lever to regulate at the side of max_tokens and the mannequin getting used is temperature.
Temperature is a price between 0 and 1 that primarily allows you to management how assured the mannequin ought to be when making these predictions. Decreasing temperature means it would take fewer dangers, and completions will likely be extra correct and deterministic. Growing temperature will end in extra various completions.
Interacting with Generative AI (ChatGPT) with PowerShell
It’s as easy and offering prompts through the Get-GPT3Completion cmdlet. It has an alias of GPT to make it faster to kind.
As a result of we’re interacting programmatically utilizing PowerShell we are able to get responses in a method we are able to do one thing simple with the output. Within the instance under I request on a regular basis zones in Oceania with GMT offset as JSON then convert that to a PowerShell Object. I then present the PowerShell Object as a desk and record.
$response = (GPT "what are the totally different time zones in Oceania with GMT offset as json") | ConvertFrom-Json
Consider to test for accuracy. The above consequence on the floor appears to be like complete. An omission above is that South Australia can be within the GMT+9.5 time zone. The Prepare dinner Islands are additionally a part of Oceania and will not be listed as are the Chatham Islands.
Producing content material with Generative AI with a PowerShell GUI
A PowerShell GUI for ChatGPT? Completely. Doug Finke but once more as one other tangent on prime of the PowerShellAI Module created an instance PowerShell Script that makes use of the presentationframework for a rudimentary graphical person interface. However don’t let the interface put you off, the power to additionally change the tone of the response and specify the variety of tokens (considered one of my PRs) means that is fairly highly effective.
Obtain the PowerShell Content material Generator from Doug’s Repo right here. Be sure you have your OpenAI API Key file in the identical listing because the script and create a brand new begin script one thing like the instance under.
By way of the GUI you’ll be able to choose the Tone and Sort of content material you want to create. You’ll be able to change the variety of tokens too if as an illustration you select Weblog Put up because the Sort. Then enter your matter and choose Generate.
Copilot for PowerShell
Copilot lets you use Generative AI to generate code for you. Utilizing PowerShell for instance I can request a Operate that makes a WebRequest and returns the consequence.
The immediate I gave is:
write a PowerShell Operate named Get-PublicIPAddress that makes use of Invoke-RestMethod to name http://ipinfo.io/json setting the reponse as a variable named IPInfo and returns the consequence to the console
It did (even with the typo) generate the quite simple Operate. Copying the output into my VSCode terminal I then referred to as it.
Working the perform returned the main points from the API. A quite simple instance however exhibits what is feasible. I discovered if you happen to break what you are attempting to attain down into small items of logic after which mix them afterwards you’ll be able to pace up what you might be doing. Don’t attempt to do something too complicated in search of a full consequence through a single immediate.
Context (session) based mostly ChatGPT with PowerShell
When you’ve used the OpenAI Portal you’ll be conscious which you can have a dialog with subsequent prompts keep context from earlier prompts. An instance can be “what’s the inhabitants of New Zealand”, with a observe up immediate “and Australia”. We are able to do that in PowerShell too. As a reminder, if in case you have not joined the GPT-4 waitlist and been accepted you’ll need to vary the Chat Session Possibility for the mannequin to GTP-3.5-turbo. You’ll be able to then can programmatically work together with context.
Lastly, we are able to additionally programmatically generate pictures. I’ve received to be trustworthy that I don’t at present have a goal to try this, however it’s potential utilizing the Get-DalleImage cmdlet. The output of the command is the trail of the picture. You’ll be able to change the dimensions with the measurement parameter.
As you’ll be able to see there are quite a few potentialities for what you are able to do with Doug’s PowerShellAI Module. I encourage you to discover the probabilities and get aware of Generative AI. And carry on prime of the enhancements and additions to the module and OpenAI usually. The capabilities are increasing on a really fast cadence.