Last active
March 30, 2023 07:01
-
-
Save mika76/bb05b295c8c8aeb4347db1df589cfd7d to your computer and use it in GitHub Desktop.
Powershell chat gpt cmdlet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use: