Connect to OpenAI API GPT 4 With PowerShell

This OpenAI API Chat GPT 4 article will show how to connect to the OpenAI platform using PowerShell and generate chat completions.

When using the OpenAPI platform to generate chat, images, files etc., we can connect to the platform using the official Python library, CURL, .NET, Node and any API client. With PowerShell 7, we can connect to any API service using REST API requests.

Starting with GPT-4, OpenAI made a few changes to how input is sent to the API endpoint to generate requests.

Note: This code uses the latest API version of the Completion API. The old version (also known as Legacy) used an input string called a prompt. The new API uses messages as the input of a list. The latest API is Chat Completions API, which uses the following endpoint: https://api.openai.com/v1/chat/completions. The Legacy API used the following endpoint https://api.openai.com/v1/completions

Chat Completions API

In this post, we will use the latest Chat Completions API that takes a list of messages as input and returns a message output in JSON format. The advantage of the new API is that it’s designed to create a conversation, but it can also take a single input.

The input parameter is the messages list that has, at a minimum, a single role (system, user or assistant) and content (question or content).

JSON Payload

To connect to the platform, we will use a JSON file containing the payload and the body requests. We are using a JSON file to make the code a bit cleaner and more portable.

To start, create a JSON file called body.json with the following content.

{
  "model": "gpt-4",
  "messages": [
    {
      "role": "assistant",
      "content": "Help me learn Powershell"
    },
    {
      "role": "user",
      "content": "How do you create a variable with PowerShell?"
    }
  ],
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}

Save the file and create a PowerShell script.

PowerShell Script

Before we start, ensure you have a valid API key to the OpenAI platform with an active plan.


$apiEndpoint = “https://api.openai.com/v1/chat/completions”
$apiKey = “API Key Goes Here”

$headers = @{
Authorization = “Bearer $apiKey”
}
$contenttype = “application/json”
$body = Get-Content .\body.json 


$response = Invoke-WebRequest -Uri $apiEndpoint -Method POST -Headers $headers -Body $body -ContentType $contenttype
$data = $response | ConvertFrom-Json
$data.choices[0].message.Content

Save the file, and run the script. The output should look like this.

API Output

Creating a variable in PowerShell is simple. You can use the dollar sign ($) followed by the variable's name to declare a variable. You assign a value to a variable using the equals sign (=).

Here's a basic example:

```powershell
$MyVariable = "Hello, world!"
```

Here, `$MyVariable` is a variable that holds the string value `"Hello, world!"`.

You can also store numbers, boolean values, date/time values, arrays, and more in PowerShell variables. For example:

```powershell
$Num = 10
$IsTrue = $true

Closing

This post was focused on PowerShell and OpenAI; however, it is essential to note that the platform supports any API client. Using PowerShell with the platform allows us to reduce the amount of code used to generate random content like user lists, random numbers, and more.

Using an external JSON file is optional; however, it allows us to keep the actual PowerShell code clean and readable.


by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.