Home

Awesome

PSOpenAI

Test

PowerShell module for OpenAI and Azure OpenAI Service.
You can use OpenAI functions such as ChatGPT, Speech-to-Text, Text-to-Image from PowerShell.

This is a community-based project and is not an official offering of OpenAI.

日本語版のREADMEはこちら


Supported Platforms

You need to sign-up OpenAI account and generates API key for authentication.
https://platform.openai.com/account/api-keys


Installation

You can install PSOpenAI from PowerShell Gallery.

Install-Module -Name PSOpenAI

Functions

Common

OpenAI

Chat

Guide: How to use Chat

Assistants

Guide: How to use Assistants
Guide: How to use File search with Assistants and Vector Store

Realtime

Guide: How to use Realtime API

[*] Works on Windows with PowerShell 7.4+ only.

Images

Audio

Files

Batch

Guide: How to use Batch

Others

Azure OpenAI Service


Usages

See Docs and Guides for more detailed and complex scenario descriptions.

ChatGPT (Interactive)

Communicate with ChatGPT interactively on the console.

$global:OPENAI_API_KEY = '<Put your API key here.>'
Enter-ChatGPT

Interactive Chat

ChatGPT (Scripting)

You can ask questions to ChatGPT.

$global:OPENAI_API_KEY = '<Put your API key here.>'
$Result = Request-ChatCompletion -Message "Who are you?"
Write-Output $Result.Answer

This code outputs answer from ChatGPT

I am an AI language model created by OpenAI, designed to assist with ...

[!TIP]
The default model used is GPT-3.5-Turbo.
If you want to use other models such as GPT-4o, you can specifies model explicitly like this.

Request-ChatCompletion -Message "Who are you?" -Model "gpt-4o"

Audio Speech (Text-to-Speech)

Generates audio from the input text.

$global:OPENAI_API_KEY = '<Put your API key here.>'
Request-AudioSpeech -text 'Do something fun to play.' -OutFile 'C:\Output\text2speech.mp3' -Voice Onyx

You can combine with ChatGPT.

Request-ChatCompletion -Message "Who are you?" | Request-AudioSpeech -OutFile 'C:\Output\ChatAnswer.mp3' -Voice Nova

Audio transcription (Speech-to-Text)

Transcribes audio into the input language.

$global:OPENAI_API_KEY = '<Put your API key here.>'
Request-AudioTranscription -File 'C:\SampleData\audio.mp3' -Format text

This code transcribes voice in C:\SampleData\audio.mp3. Like this.

Perhaps he made up to the party afterwards and took her and ...

Image generation (Text-to-Image)

Creating images from scratch based on a text prompt.

$global:OPENAI_API_KEY = '<Put your API key here.>'
Request-ImageGeneration -Prompt 'A cute baby lion' -Model 'dall-e-2' -Size 256x256 -OutFile 'C:\output\babylion.png'

This sample code saves image to C:\output\babylion.png. The saved image like this.

Generated image

Multiple conversations with ChatGPT while keeping context.

Request-ChatCompletion accepts past dialogs from pipeline. Additional questions can be asked while maintaining context.

PS C:\> $FirstQA = Request-ChatCompletion -Message "What is the population of the United States?"
PS C:\> Write-Output $FirstQA.Answer

As of September 2021, the estimated population of the United States is around 331.4 million people.

PS C:\> $SecondQA = $FirstQA | Request-ChatCompletion -Message "Translate the previous answer into French."
PS C:\> Write-Output $SecondQA.Answer

En septembre 2021, la population estimée des États-Unis est d'environ 331,4 millions de personnes.

PS C:\> $ThirdQA = $SecondQA | Request-ChatCompletion -Message 'Please make it shorter.'
PS C:\> Write-Output $ThirdQA.Answer

La population des États-Unis est estimée à environ 331,4 millions de personnes.

Stream completion outputs

By default, results are output all at once after all OpenAI responses are complete, so it may take some time before results are available.

To get responses sooner, you can use the -Stream option for Request-ChatCompletion and Request-TextCompletion. The results will be returned as a "stream". (similar to how ChatGPT WebUI displays)

Request-ChatCompletion 'Describe ChatGPT in 100 charactors.' -Stream | Write-Host -NoNewline

Stream

Restore masked images

Request-ImageEdit -Image 'C:\sunflower_masked.png' -Prompt 'sunflower' -OutFile 'C:\sunflower_restored.png' -Size 256x256

Masked image is restored to full images by AI models.

SourceGenerated
maskedrestored

Moderation

Test whether text complies with OpenAI's content policies.

The moderation endpoint is free to use when monitoring the inputs and outputs of OpenAI APIs.

PS C:\> $Result = Request-Moderation -Text "I want to kill them."
PS C:\> $Result.results.categories

# True means it violates that category.
sexual           : False
hate             : False
violence         : True
self-harm        : False
sexual/minors    : False
hate/threatening : False
violence/graphic : False

About API key

Almost all functions require an API key for authentication.
You need to sign-up OpenAI account and generates API key from here.
https://platform.openai.com/account/api-keys

There are three ways to give an API key to functions.

Method 1: Set an environment variable named OPENAI_API_KEY. (RECOMMENDED)

Set the API key to the environment variable named OPENAI_API_KEY.
This method is best suited when running on a trusted host or CI/CD pipeline.

PS C:> $env:OPENAI_API_KEY = '<Put your API key here.>'
PS C:> Request-ChatCompletion -Message "Who are you?"

Method 2: Set a global variable named OPENAI_API_KEY.

Set the API key to the $global:OPENAI_API_KEY variable. The variable is implicitly used whenever a function is called within the session.

PS C:> $global:OPENAI_API_KEY = '<Put your API key here.>'
PS C:> Request-ChatCompletion -Message "Who are you?"

Method 3: Supply as named parameter.

Specify the API key explicitly in the ApiKey parameter. It must be specified each time the function is called.
This is best used when the function is called only once or with few calls, such as when executing manually from the console.

PS C:> Request-ChatCompletion -Message "Who are you?" -ApiKey '<Put your API key here.>'

Azure OpenAI Service

If you want to use Azure OpenAI Service instead of OpenAI. You should create Azure OpenAI resource to your Azure tenant, and get API key and endpoint url. See guides for more details.

Sample code for Azure

$global:OPENAI_API_KEY = '<Put your api key here>'
$global:OPENAI_API_BASE  = 'https://<resource-name>.openai.azure.com/'

Request-ChatCompletion `
  -Message 'Hello Azure OpenAI Service.' `
  -Model 'gpt-4o' `
  -ApiType Azure

Changelog

CHANGELOG.md


Plans and TODOs.

If you have a feature request or bug report, please tell us in Issue.


License & Libraries

MIT License

This module uses these OSS libraries.