Coder Social home page Coder Social logo

psopenai's Introduction

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

  • 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


Installation

You can install PSOpenAI from PowerShell Gallery.

Install-Module -Name PSOpenAI

Functions

Common

OpenAI

Chat

Assistants

Guide: How to use Assistants

Images

Audio

Files

Others

Azure OpenAI Service

Assistants for Azure OpenAI Service
  • Get-AzureAssistant
  • New-AzureAssistant
  • Set-AzureAssistant
  • Remove-AzureAssistant
  • Get-AzureThread
  • New-AzureThread
  • Set-AzureThread
  • Remove-AzureThread
  • Get-AzureThreadMessage
  • Add-AzureThreadMessage
  • Get-AzureThreadRun
  • Start-AzureThreadRun
  • Stop-AzureThreadRun
  • Wait-AzureThreadRun
  • Receive-AzureThreadRun
  • Get-AzureThreadRunStep

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.
If you can and want to use GPT-4, you can specifies model explicitly like this.

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

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.

Source Generated
masked restored

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-AzureChatCompletion `
  -Message 'Hello Azure OpenAI Service.' `
  -Deployment 'gpt-35-turbo' `

Changelog

CHANGELOG.md


Plans and TODOs.

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

  • More docs, samples.
  • Performance improvements.
  • Add an option for change output types / formats.

Licenses

MIT License

psopenai's People

Contributors

mkht avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

psopenai's Issues

Press Enter to confirm and for automatic line wrapping in PowerShell ISE

I usually use PowerShell ISE for code editing, and I have encountered two issues:

  1. After inputting a question, I need to press Enter three times to confirm. Can I use Ctrl+Enter to confirm directly?
  2. When the text length of ChatGPT's answer is too long, a scrollbar appears, displaying it all in one line, which is not very convenient for reading. Can it automatically wrap at the width of the window?
    demo Gif:
    gpt

add -Proxy parameter

my System: windows 10 Pro 21H2, Powershell 5.1

I'm a novice in PowerShell. The network I'm using cannot directly access the ChatGPT API. I need to use a proxy for access. However, when using the following code, I don't receive any response.

$env:http_proxy = "http://127.0.0.1:1081"
$env:https_proxy = "http://127.0.0.1:1081"
$global:OPENAI_API_KEY = "sk-XXXXX"
$Result = Request-ChatCompletion -Message "Who are you?"
Write-Output $Result.Answer

Therefore, I hope to add a -Proxy parameter, just like below, so that I can successfully use the following code to install the module

Install-Module -Name PSOpenAI -Force -Proxy "http://127.0.0.1:1081"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.