Read on:

Virtualization Trends Series: A Brief History of Virtualization: Part 1
Virtualization Trends Series: The Evolution and Future of Hypervisors: Part 2
Virtualization Trends Series: Do you Actually Need a Multi-Cloud Strategy: Part 3

While the 2010s’ were the data decade, it looks like the 2020s will somewhat be the AI decade. Use cases for artificial intelligence have been growing drastically in numbers, driving demand for new technology and easier adoption path. Unless you have been living under a rock, you have at least heard of ChatGPT, this AI powered chatbot developed by the company OpenAI that offers incredibly smart answers to most questions you can throw at it.
If you don’t know what ChatGPT is, head over to https://chat.openai.com/ and click the TRY CHATGPT button. Chances are their servers will be overloaded as more and more people start using it, in which case you will be granted by a funny message from the AI.

Protect Your Data with BDRSuite

Cost-Effective Backup Solution for VMs, Servers, Endpoints, Cloud VMs & SaaS applications. Supports On-Premise, Remote, Hybrid and Cloud Backup, including Disaster Recovery, Ransomware Defense & more!

ChatGPT and PowerShell

“ChatGPT servers are often at capacity.”

However, note that queries issued against the API, as we are about to do, will work regardless.

Create an OpenAI API key

First things first, API key to authenticate against OpenAI. For that we need to create an account and then generate the key.

Download Banner
  • Head over to https://platform.openai.com/account and click Sign up.
  • ChatGPT and PowerShell

  • Then create your account with your preferred method. I chose to go with Google SSO personally.
  • Once you are logged in, go to your profile and click View API Keys.
  • ChatGPT and PowerShell

  • From there you can create a new by clicking Create new secret key.
  • ChatGPT and PowerShell

  • At this point you can copy the key or keep this window open for now. If you click OK you won’t be able to display the key again but you can always revoke it and create a new one.

ChatGPT and PowerShell

Add a payment method

Note that you may encounter errors when making API requests which state that you’ve reached your quota. If it is the case, you can unblock it by adding a payment method to your OpenAI account.

You exceeded your current quota, please check your plan and billing details

Note that it will be very cheap for regular testing. I did 15 requests for about 1000 tokens and it cost less than $0.01.

ChatGPT and PowerShell

You can also set an upper limit to make sure you don’t get charged more than what you’re comfortable spending.

  • To add a payment method, go to Billing > Payment methods > Add payment method.
  • ChatGPT and PowerShell

  • You can then add a limit in Billing > Usage limits > Hard/Soft limit according to how you want to set this up.
  • ChatGPT and PowerShell

What are Models and Tokens?

Before jumping in PowerShell, it is best to explain a couple concepts to understand some of the settings we will be using, namely models and tokens.

AI models

An AI model is a program or algorithm that is trained on a specific dataset to perform specific tasks or make predictions. They are designed to learn from data and improve their performance over time. Different models are trained with various amounts of data, making them more or less “intelligent”.

Model Dataset Size Number of Parameters
GPT 40 GB 117 million
GPT-2 1.5 TB 1.5 billion
GPT-3 570 GB 175 billion

Back to ChatGPT, you can select from several models to process your requests according to the OpenAI API documentation with different price points. Some are more advanced than others, hence the higher price tag and there can be a big difference in results quality depending on which model you use.

For instance, I got drastically better results at producing a PowerShell function with Davinci than with Curie, where I could tell it was getting lost.

Model Number of Parameters Strengths Price
Ada 40 million Good for simple language tasks, such as autocomplete or simple question-answering

$0.0004 per 1k token
Babbage 1.5 billion Good for simple language tasks and generating short text, such as headlines or short descriptions $0.0005 per 1k token
Curie 6 billion Good for a wide range of language tasks, including generating coherent paragraphs and longer text $0.002 per 1k token
Davinci 175 billion Good for a wide range of language tasks, including more complex tasks such as creative writing, chatbots, and content generation $0.02 per 1k token

In a nutshell, when querying ChatGPT’s API in PowerShell, you can use any of these models. Davinci will give you the best answers but will cost the most, while Ada will be more basic but cheaper. Note that it will very much depend on what you are asking as Davinci might sometimes be overkill.
Because I am only using it for testing, I mostly use Davinci and I will use the other ones only to compare answers. However, an organization that relies heavily on AI will need to put some effort into finding the cheapest model for a specific task as it can lead to large cost savings.

OpenAI tokens

In the context of the OpenAI Completion API, a token represents a single word or character in the text that is being generated or completed. When you send a request to the API, you specify the maximum number of tokens you want the model to generate.

The API then charges you based on the number of tokens used in the response, including both the input prompt and the generated output. So if you provide a prompt with 90 tokens and request the model to generate 200 tokens in response, you will be charged for a total of 290 tokens.
As seen previously, the cost will vary according to which model you used, which are priced per 1000 tokens.

ChatGPT and PowerShell

ChatGPT in PowerShell

Now that we understand OpenAI and ChatGPT a little better, let’s get into PowerShell and start asking it questions. As an exercise, I asked ChatGPT to generate the PoweShell function and it did pretty well. I still had to fix a couple things and add the parameters but it did well all things considered.

  • First, create an environment variable with the API key you generated at the beginning. If you no longer have it, revoke it and create a new one

$env:OPENAI_API_KEY = “sk-Pwq…hdyI9O”

  • Paste the function in your prompt

function Ask-ChatGPT {
param(
$prompt,
[validateset(“text-curie-001″,”text-davinci-002”,”text-babbage-001”,”text-ada-001”)]
$model = “text-curie-001”,
$api_key = $env:OPENAI_API_KEY,
$max_tokens = 2020
)

# Build the request body
$body = @{
“prompt” = $prompt;
“model” = $model;
}

if ($max_tokens) {$body.add(“max_tokens”,$max_tokens)}

# Send the request
$response = Invoke-RestMethod -Uri “https://api.openai.com/v1/completions” -Method Post -Headers @{ “Authorization” = “Bearer $api_key”;”Content-Type” = “application/json” } -Body (ConvertTo-Json $body)

# Return the response
return $response.choices.text
}

The parameters are as follows:

  • Prompt: The question you ask ChatGPT
  • Model: Which OpenAI model to use. I made it simpler by setting valid values in the validateset so you can just autocomplete. The default is curie
  • Api_key: the OpenAI API key you generated with your account
  • Max_tokens: The maximum number of tokens to use in the request. I defaulted it to 2020 as the maximum is 2048 but you might want to edit this to your needs

You can now start using the Ask-ChatGPT command with specific models and maximum number of tokens to use.

In the example below, I ask ChatGPT with different modes to write a meta description for this blog post.

Davinci answer:

Ask-ChatGPT “write a meta description for a blog post about using openai API for chatgpt in Powershell in less than 120 characters” -model “text-davinci-003” -max_tokens 2020

Using OpenAI API to create a chatbot in Powershell: Learn how to use the OpenAI API to quickly create a powerful chatbot in Powershell!

Curie answer:

Ask-ChatGPT “write a meta description for a blog post about using openai API for chatgpt in Powershell in less than 120 characters” -model text-curie-001 -max_tokens 2020

Chatbots using Powershell can now easily use the openai API to get started with conversational AI.

Babbage answer:

Ask-ChatGPT “write a meta description for a blog post about using openai API for chatgpt in Powershell in less than 120 characters” -model text-babbage-001 -max_tokens 2020

How to use the OpenAI Arena ChatGPT API to talk to robots in Powershell using the standard Windows cmdlets.

Ada answer:

Ask-ChatGPT “write a meta description for a blog post about using openai API for chatgpt in Powershell in less than 120 characters” -model text-ada-001 -max_tokens 2020

If you’re looking to use the openai API to connect to your Powershell sessions in a Greater than 114 white-space free text post, you’ll love this little piece oficy. openai provides a comprehensive set of tools andusa services forbilling shows and messages in a fraction of the time that it takes to outrightcoclitcting your own messages.

The openai team iskewing towards chat platforms like Fiverr and Mailhar Examiner for their younger audience, so be sure toimilocked your account and bezihar total uprighternote: You’re absolutely anyway when using openai as you would be when using any other chat application withether your password or helmet. openai provides complete control over who, what, and when you want tocommunicate, making it the perfect tool for Sending B2B messages and events in a more simpler, faster, andragehot

As you can see, the replies generated with Davinci and Curie are pretty good. However, the answer with Babbage doesn’t make much sense and Ada produced complete nonsense.

Conclusion

While AI was already on the rise for some time, OpenAI really changed the game and started a revolution by opening ChatGPT to the public. Many companies applied it to their own use cases, and it was adopted widely in the world by everyone, not only tech geeks.
Identifying the right model to use can be tricky as it depends on the use case and will be a trial and error process to optimize tokens cost and save money.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Rate this post