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.
-
About OpenAI API
https://platform.openai.com/docs -
About Azure OpenAI Service
https://learn.microsoft.com/en-us/azure/ai-services/openai/overview
日本語版のREADMEはこちら
- Windows PowerShell 5.1
- PowerShell 7 or higher
- Windows, macOS or Linux
You need to sign-up OpenAI account and generates API key for authentication.
https://platform.openai.com/account/api-keys
You can install PSOpenAI from PowerShell Gallery.
Install-Module -Name PSOpenAI
- ConvertFrom-Token
- ConvertTo-Token
- Get-CosineSimilarity
- Get-OpenAIContext
- Set-OpenAIContext
- Clear-OpenAIContext
Guide: How to use Assistants
Guide: How to use File search with Assistants and Vector Store
- Get-Assistant
- New-Assistant
- Set-Assistant
- Remove-Assistant
- Get-Thread
- New-Thread
- Set-Thread
- Remove-Thread
- Get-ThreadMessage
- Add-ThreadMessage
- Remove-ThreadMessage
- Get-ThreadRun
- Start-ThreadRun
- Stop-ThreadRun
- Wait-ThreadRun
- Receive-ThreadRun
- Get-ThreadRunStep
- Get-VectorStore
- New-VectorStore
- Set-VectorStore
- Remove-VectorStore
- Add-VectorStoreFile
- Get-VectorStoreFile
- Remove-VectorStoreFile
- Start-VectorStoreFileBatch
- Get-VectorStoreFileBatch
- Stop-VectorStoreFileBatch
- Wait-VectorStoreFileBatch
- Get-VectorStoreFileInBatch
See Docs and Guides for more detailed and complex scenario descriptions.
Communicate with ChatGPT interactively on the console.
$global:OPENAI_API_KEY = '<Put your API key here.>'
Enter-ChatGPT
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"
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
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 ...
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.
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.
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
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.
Source | Generated |
---|---|
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
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.
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?"
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?"
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.>'
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.
$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.' `
-Deployment 'gpt-35-turbo' `
-ApiType Azure
If you have a feature request or bug report, please tell us in Issue.
- More docs, samples.
- Performance improvements.
- Add a support for fine-tuning.
This module uses these OSS libraries.
- Newtonsoft.Json by jamesnk (MIT License)
- NJsonSchema by rsuter (MIT License)
- Microsoft.DeepDev.TokenizerLib by microsoft (MIT License)