Skip to content

Instantly share code, notes, and snippets.

@mika76
Last active March 30, 2023 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika76/bb05b295c8c8aeb4347db1df589cfd7d to your computer and use it in GitHub Desktop.
Save mika76/bb05b295c8c8aeb4347db1df589cfd7d to your computer and use it in GitHub Desktop.
Powershell chat gpt cmdlet
function Invoke-ChatGPT {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$Prompt,
[Parameter(Mandatory = $false)]
[int]$MaxTokens = 50
)
# Set API key and endpoint
$APIKey = "your api key"
$Endpoint = "https://api.openai.com/v1/completions"
# Create headers
$headers = @{
"Authorization" = "Bearer $($APIKey)"
"Content-Type" = "application/json"
}
# Create payload
$body = @{
"model" = "text-davinci-003"
"prompt" = $Prompt
"max_tokens" = $MaxTokens
"n" = 1
"temperature" = 0.7
} | ConvertTo-Json
# Send POST request
$response = Invoke-WebRequest -Method POST -Uri $Endpoint -Headers $headers -Body $body
# Parse response
$parsedResponse = $response | ConvertFrom-Json
#$parsedResponse
# Display result
$markdownResult = $parsedResponse.choices[0].text.Trim()
$markdownResult | Show-Markdown
}
@mika76
Copy link
Author

mika76 commented Mar 30, 2023

To use:

> Import-Module .\ChatGPT.ps1
> Invoke-ChatGPT "can you give me a simple powershell script to order files by name" -MaxTokens 200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment