Generative AI (ChatGPT) with PowerShell

Unless you’ve been off the grid on a long sabbatical the last six months you’ve surely heard of ChatGPT. Like many I started exploring the possibilities of OpenAI ChatGPT when it hit the world by storm last year. Pretty quickly I saw the power of it, but also wondered about using it via API. Then in January this year Doug Finke a Microsoft MVP released the first beta of his PowerShellAI module. This opened up the possibilities to use Generative AI (ChatGPT) with PowerShell and thereby VSCode.

The PowerShellAI module has evolved extensively since then as both OpenAI and Microsoft have released additional capabilities and the module has been extended with additional cmdlets to interact with them.

In this post I’m going to show how I’ve been using the module in the following scenarios:

  • ChatGPT 3.5-turbo and ChatGPT-4
  • Generating content with Generative AI with a PowerShell GUI
  • Copilot for PowerShell
  • Context (session) based ChatGPT with PowerShell
  • Generating Images with PowerShell

Getting started with the PowerShellAI Module

The latest version is available from both Doug Finke’s GitHub Repo and the PowerShell Gallery. Keep in mind this module is still under constant refinement. Keep checking back for updates mostly from Doug but also from PRs from the community.

The quickest method is to install from the PowerShell Gallery.

Install-Module -Name PowerShellAI

If upgrading from a previous version, you will need to use the -force parameter.

Install-Module -Name PowerShellAI -force

Obtaining an OpenAI API Key

To use the OpenAI large language modules (LLM) with the PowerShellAI Module you will need an API Key. Create an OpenAI Account and generate an API Key from here. OpenAI will give you $5 credit that is valid for 3 months. That allows quite a lot of experimenting. I didn’t use all of that $5 before the 3 months were up. After the 3 months you will need to provide a billing option (credit card) to continue to use their models. Alternatively at this time you could switch over to Azure OpenAI if you have a subscription with available monthly credits. I’m not going to detail Azure OpenAI as part of this post.

Configuring the PowerShellAI Module with your OpenAI API Key

There are a couple of ways of doing this. The way I started doing it in the early iterations of the module is to generate a secure credential and save it as an xml file which on Windows is only ever able to be read by the same profile on the same host that created the file.

With the last two lines I was importing my API Key file and reversing the secure credential password back to clear text and setting the OpenAIKey environment variable. The example to do that is below.

$env:OpenAIKey = $chatGPTAPIKey

Subsequently with the addition of the Set-OpenAIKey cmdlet this can be simplified to the following which is now included at the top of all my PowerShellAI scripts. Import the previously securely exported credential and set the OpenAIKey variable using the Set-OpenAIKey cmdlet.

Using ChatGPT 3.5-turbo and ChatGPT-4 with PowerShell

Now we have the PowerShellAI PowerShell Module installed and our environment configured with an OpenAI API Key we can start using it.

By default, the recent versions of the PowerShellAI Module default to GPT-4. You can verify using the Get-ChatSessionOptions cmdlet.

The model can be changed (along with other parameters) using the Set-ChatSessionOptions cmdlet. The following image shows switching to the GPT-3.5-turbo LLM.

Unless you have added yourself to the GPT-4 waitlist and been approved you will not be able to use the GPT-4 model.

Other than changing models I’ve only ever used Set-ChatSessionOptions to change the max_tokens and temperature for specific use cases up front for a session. If you just need to change it temporarily for a query or two you can also specify max_tokens and temperature on the chat cmdlets.

OpenAI Tokens

If you are new to large language models you will likely want to know what Tokens are. Succinctly they are how the AI language model services take input and interpret the text strings to then generate the next tokens (the response). The OpenAI Tokenizer will help you understand how that is accomplished.

A helpful rule of thumb is that one token generally corresponds to ~4 characters of text for common English text. This translates to roughly ¾ of a word (so 100 tokens ~= 75 words).

OpenAI API

An API for accessing new AI models developed by OpenAI

Generative AI (ChatGPT) with PowerShell

The more tokens you configure your session for the larger the input and responses will be. Different models have different sizes for max_tokens and the billing for use also differs. At the time of this post the current pricing is shown below.

OpenAI Temperature

The other lever to adjust in conjunction with max_tokens and the model being used is temperature.

Temperature is a value between 0 and 1 that essentially lets you control how confident the model should be when making these predictions. Lowering temperature means it will take fewer risks, and completions will be more accurate and deterministic. Increasing temperature will result in more diverse completions.

Quickstart tutorial – OpenAI API

Interacting with Generative AI (ChatGPT) with PowerShell

It is as simple and providing prompts via the Get-GPT3Completion cmdlet. It has an alias of GPT to make it quicker to type.

Because we are interacting programmatically using PowerShell we can get responses in a way we can do something easy with the output. In the example below I request all the time zones in Oceania with GMT offset as JSON then convert that to a PowerShell Object. I then show the PowerShell Object as a table and list.

$response = (GPT "what are the different time zones in Oceania with GMT offset as json") | ConvertFrom-Json

Keep in mind to check for accuracy. The above result on the surface looks comprehensive. An omission above is that South Australia is also in the GMT+9.5 time zone. The Cook Islands are also part of Oceania and are not listed as are the Chatham Islands.

Generating content with Generative AI with a PowerShell GUI

A PowerShell GUI for ChatGPT? Absolutely. Doug Finke yet again as another tangent on top of the PowerShellAI Module created an example PowerShell Script that uses the presentationframework for a rudimentary graphical user interface. But don’t let the interface put you off, the ability to also change the tone of the response and specify the number of tokens (one of my PRs) means this is quite powerful.

Download the PowerShell Content Generator from Doug’s Repo here. Make sure you have your OpenAI API Key file in the same directory as the script and create a new start script something like the example below.

Through the GUI you can select the Tone and Type of content you wish to create. You can change the number of tokens too if for instance you choose Blog Post as the Type. Then input your topic and select Generate.

Generative AI (ChatGPT) with PowerShell with a GUI

Copilot for PowerShell

Copilot allows you to use Generative AI to generate code for you. Using PowerShell as an example I can request a Function that makes a WebRequest and returns the result.

The prompt I gave is:

write a PowerShell Function named Get-PublicIPAddress that uses Invoke-RestMethod to call http://ipinfo.io/json setting the reponse as a variable named IPInfo and returns the result to the console

It did (even with the typo) generate the very simple Function. Copying the output into my VSCode terminal I then called it.

Running the function returned the details from the API. A very simple example but shows what is possible. I found if you break what you are trying to achieve down into small pieces of logic and then combine them afterwards you can speed up what you are doing. Don’t try and do anything too complex looking for a full result via a single prompt.

Generative AI (ChatGPT) with PowerShell with copilot

Context (session) based ChatGPT with PowerShell

If you’ve used the OpenAI Portal you will be aware that you can have a conversation with subsequent prompts maintain context from previous prompts. An example would be “what is the population of New Zealand”, with a follow up prompt “and Australia”. We can do this in PowerShell too. As a reminder, if you have not joined the GPT-4 waitlist and been approved you will need to change the Chat Session Option for the model to GTP-3.5-turbo. You can then can programmatically interact with context.

Generative AI (ChatGPT) with PowerShell with context

Generating Images

Finally, we can also programmatically generate images. I’ve got to be honest that I don’t currently have a purpose to do that, but it is possible using the Get-DalleImage cmdlet. The output of the command is the path of the image. You can change the size with the size parameter.

Generative AI (ChatGPT) with PowerShell to generate images

Summary

As you can see there are numerous possibilities for what you can do with Doug’s PowerShellAI Module. I encourage you to explore the possibilities and get familiar with Generative AI. And keep on top of the improvements and additions to the module and OpenAI in general. The capabilities are expanding on a very quick cadence.