Text Completion with OpenAI API in Windows PowerShell: A Step-by-Step Guide Without curl

SURANJAN DAS
7 min readOct 2, 2023

Howdy, folks! We’re about to embark on a wild and witty journey into the world of text completion using the OpenAI API in Windows PowerShell. And guess what? We’re doing it without the usual curl shenanigans!

In this step-by-step guide, we’ll be your trusty trail guides through the digital frontier. Forget about wrestling with curl commands; we’re here to make text completion as easy as a Sunday morning ride in the Texas countryside.

So, grab your Stetson hats and PowerShell scripts, partner! We’re about to embark on an adventure that’ll leave you saying, “Yeehaw!” as we navigate the digital prairies with humor, knowledge, and a whole lot of Windows PowerShell magic. Saddle up, ’cause it’s high noon in the world of text completion, and we’re shootin’ for the stars!

Note: If you already have an API key, you can skip this part and jump directly to the thrilling world of text completion with Windows PowerShell. Your key is your passport to the AI adventure ahead!

Step 1: Visit OpenAI.com

Our journey begins at OpenAI.com. Head over there and get ready to unlock the power of the OpenAI API.

Step 2: Log In or Sign Up

If you’re already an OpenAI user, you can skip this step and jump straight to the action. For newcomers, click on “Sign Up” and follow the registration process to create your account.

Step 3: Choose Your Path

Once you’ve logged in or signed up, you’ll be greeted with three intriguing options: ChatGPT, DALL·E, and API. For this adventure, we’re taking the API route, so go ahead and click on “API.”

Step 4: Welcome to the API Hub

You’re now in the heart of the OpenAI API platform at platform.openai.com. This is where the magic happens.

Step 5: Access Your Account

At the top right corner, you’ll spot your account icon. Click on it, and a menu will appear.

Step 6: View API Keys

In the menu, locate and click on “View API Keys.” This is where you’ll manage the keys to the kingdom.

Step 7: Create a New Secret Key

The moment of truth! Click on “Create new secret key.” You’ll be prompted to give your key a name. Make it memorable, but keep it secure.

Step 8: Save Your Key Securely

After you’ve named your key, hit “OK.” But here’s the critical part: once you create your secret key, you won’t see it again. So, copy and save it in a secure place — like a digital vault. You’ll need this key to access the OpenAI API and unleash its text completion superpowers.

Warning: Please ensure that you guard your API key like the crown jewels. Never share it publicly or expose it in any code repositories. If your API key falls into the wrong hands, it could lead to unauthorized use of your account, and you may lose any free credits you have. For those with a credit card on file, unauthorized access could potentially result in unexpected charges. Keep it safe and secure!

And there you have it — your very own API key is now in your hands. You’re ready to dive into the world of text completion with the OpenAI API, all without the need for curl.

If you are interested in acquiring knowledge about the conventional approach involving HTML and JavaScript for making text completion requests, we invite you to read the comprehensive guide available at this link

Let’s delve into the real thing — the realm of OpenAI’s powerful API. Fear not, for you don’t need to be a seasoned coder to set sail. In this guide, we’ll walk you through the process step by step, making it accessible to both beginners and experts alike.

Step 1: Launch PowerShell

Our adventure begins with the launch of PowerShell. This versatile and user-friendly command-line interface provides the perfect backdrop for our API exploration. Open your PowerShell terminal, and you’re one step closer to unlocking the magic of text completions with OpenAI.

Step 2: Copy and Paste the Code

Now, for the main act. Copy and paste the following lines of code one by one into your PowerShell terminal. This code will craft your request to the OpenAI API, fetching you a text completion:

$url = "https://api.openai.com/v1/completions"  # The API endpoint

$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer YOUR_API_KEY_HERE" # Replace with your API key
}

$body = @{
"model" = "babbage-002" # Choose a model compatible for completions
"prompt" = "I am a vegan and you?" # Your prompt message to send to the API
"temperature" = 0.7 # Lower value for precision, higher for randomness
}

$response = Invoke-RestMethod -Uri $url -Headers $headers -Method POST -Body (ConvertTo-Json $body)

# Display the entire response
$response

Let’s break the above code for your understanding:

$url = "https://api.openai.com/v1/completions"  # The API endpoint

In this line, we define the variable $url and set it to the URL of the OpenAI API endpoint for text completions. This is the destination where our API request will be sent.

$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer YOUR_API_KEY_HERE" # Replace with your API key
}
  • Here, we create the $headers variable, which is a hash table (@{}) containing HTTP headers. We specify two headers:
  • "Content-Type": This header indicates that the content of our request is in JSON format.
  • "Authorization": This header includes your API key for authentication. Replace "YOUR_API_KEY_HERE" with your actual OpenAI API key.
$body = @{
"model" = "babbage-002" # Choose a model compatible for completions
"prompt" = "Hello, how are you?" # Your prompt message to send to the API
"temperature" = 0.7 # Lower value for precision, higher for randomness
}
  • In this section, we create the $body variable, another hash table. This hash table contains the request parameters for our API call:
  • "model": Specifies the language model to use. In this example, we've used "babbage-002", but you can choose a different model depending on your requirements. Models compatible for endpoint /v1/completions are gpt-3.5-turbo-instruct, babbage-002, davinci-002
  • "prompt": This is the text prompt you want to send to the API. In our example, it's set to "Hello, how are you?". You can replace it with any text you'd like.
  • "temperature": This parameter controls the randomness of the output. A lower value (e.g., 0.2) results in more deterministic responses, while a higher value (e.g., 0.7) makes the output more random.
  • There are also other optional parameters in fine-tuning the response. Examples include suffix, top_p, n, max_tokens etc. You can read about them here.
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method POST -Body (ConvertTo-Json $body)
  • This line is where the API request is made. We use Invoke-RestMethod to send an HTTP POST request to the URL specified in $url. We include the headers, method, and request body as parameters. The ConvertTo-Json $body part converts our $body hash table into a JSON-formatted string.
# Display the entire response
$response
  • Finally, we display the entire response from the OpenAI API by echoing the $response variable to the console. This is where you'll see the output of the text completion generated by the API.

Step 3: Personalize Your Request

Before hitting that enter key, make it your own. Replace "YOUR_API_KEY_HERE" with your actual OpenAI API key. This is your golden ticket to accessing the API's superpowers.

Feel free to experiment with the model, prompt, and temperature values to tailor your request. Want precision? Lower the temperature. Craving randomness? Crank it up. Your request, your rules.

Step 4: Hit Enter

With your personalized code ready to roll, hit that enter key and watch the magic happen. Your request is now on its way to the OpenAI API.

Check ‘text’ field of choices. You may get different response.

Step 5: Feast on the Response

Your response from the OpenAI API is here! Take a moment to examine the output in your PowerShell terminal. This is where your text completion will be displayed.

And there you have it — an OpenAI API request served right from your PowerShell terminal. You’ve officially unlocked the power to craft text completions at your fingertips.

Expanding Your Horizons: Managing API Usage Limits

In the quest to harness the capabilities of the OpenAI API, you may encounter occasional speed bumps, such as reaching usage limits. Fear not; there’s a solution at your disposal.

Understanding Usage Limits

OpenAI, like any powerful resource, imposes certain usage limits to ensure fair and responsible use of its services. These limits are in place to maintain the quality and availability of the API for all users.

Overcoming Usage Limit Challenges

If you find yourself in a situation where you’ve exhausted your usage limits, here’s a viable solution:

Create a New Account: Consider signing up for a new OpenAI account using a different email address and phone number. Once your new account is set up, you can proceed to generate a fresh API key as we’ve discussed in our this guide.

Ensuring a Smooth Journey

While this approach allows you to bypass usage limits, it’s important to note that it should be done responsibly and ethically. Be mindful of OpenAI’s terms of service and guidelines to ensure your usage remains within acceptable bounds.

The Journey Continues

This is just the beginning of your exploration into the world of AI-powered text completions. As you grow more comfortable with this process, you can delve deeper into customization and discover the limitless possibilities that await.

So, what’s next on your AI adventure? Whether it’s crafting creative content, automating tasks, or unraveling complex problems, the OpenAI API is your trusty companion on the journey ahead. Happy coding!

--

--

No responses yet