Loading…

READY TO ROCK?

Quick Tip: ChatGPT with Powershell

ChatGPT is quite an interesting innovation. AI has endless possibilities. I have been testing ChatGPT for the last several days. Incidentally there is a very well documented API for it and here is a quick tip to use Chat with Powershell. Please note that you will need to create an API key to use it. Needless to say the one you see in the below script is a dummy value. get your own API key from https://beta.openai.com/account/api-keys

# Define the API endpoint and API key
$uri = "https://api.openai.com/v1/completions"
$apiKey = "sk-1wg6StevaTDummy_Value_Get_Your_OWN_KeyV5Shwz"

#Type your query in the following string
$YourQuery = "Why are you so helpful and What do you what in response?"

#Chat GPT supports maximum context length is 4096 tokens
$max_tokens = 4096 - ($prompt | Measure-Object -Word).words
# Define the request body
$RequestBody = @{
    prompt = $YourQuery
    model = "text-davinci-003"
    temperature = 0
    max_tokens= $max_tokens
}

# Convert the request body to a JSON string
$requestBody = ($requestBody | ConvertTo-Json)

# Create the request headers
$headers = @{
    "Content-Type" = "application/json"
    "Authorization" = "Bearer $apiKey"
}

# Send the request to the API
$response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $requestBody

# Print the response
Write-Host $response.choices[0].text

Found a funny response shared by someone

Don’t worry, That’ i’s not the response I got from ChatGPT when I asked the same question.