Skip to content

Instantly share code, notes, and snippets.

@stimpy77
Created May 29, 2024 17:03
Show Gist options
  • Save stimpy77/c34f74978d960e0f3b7ddfb834918f24 to your computer and use it in GitHub Desktop.
Save stimpy77/c34f74978d960e0f3b7ddfb834918f24 to your computer and use it in GitHub Desktop.
# "2+2=" | gpt
# outputs: 4
#
# I like to have this in my profile by putting it in %documents%\PowerShell\
# and putting this in Microsoft.PowerShell_profile.ps1:
#
# . "$PSScriptRoot\gpt.ps1"
function Query-Gpt {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
[string]$Prompt,
[Parameter(Mandatory = $false)]
[string]$Model,
[Parameter(Mandatory = $false)]
[switch]$RememberHistory,
[Parameter(Mandatory = $false)]
[switch]$ClearHistory
)
Begin {
if ($RememberHistory) { $global:RememberGptHistory = $true }
$RememberHistory = $global:RememberGptHistory
if ($ClearHistory) { $global:gptHistory = $null }
}
Process {
$DEFAULT_MODEL = "gpt-4o"
if (-not $Prompt) {
$Prompt = $input | Out-String
if (-not $Prompt) { Write-Error "Prompt is required"; return }
}
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$Prompt = $Prompt.Trim()
$defaultModelUsed = $false
if (-not $Model) {
$Model = $DEFAULT_MODEL
$defaultModelUsed = $true
}
if ($defaultModelUsed) { Write-Host "(Model: $Model)" }
if ($null -eq $env:OPENAI_PERSONAL_API_KEY -and $null -eq $env:OPENAI_API_KEY) {
Write-Error "OPENAI_PERSONAL_API_KEY environment variable is not defined";
return
}
$apiKey = $env:OPENAI_PERSONAL_API_KEY
if ($null -eq $apiKey) {
$apiKey = $env:OPENAI_API_KEY
}
$apiUrl = "https://api.openai.com/v1/chat/completions"
$headers = @{ "Authorization" = "Bearer $apiKey"; "Content-Type" = "application/json" }
$bodyObject = @{ messages = @(); model = $Model; temperature=0 }
if ($RememberHistory) {
if ($null -eq $global:gptHistory) { $global:gptHistory = @() }
$bodyObject['messages'] = $global:gptHistory
}
if ($bodyObject['messages'].Count -eq 0) {
$windowSize = $Host.UI.RawUI.WindowSize
$widthHeight = $windowSize.ToString()
$bodyObject['messages'] += @{ role = "system"; content = "You are rendering directly in Windows Terminal (dimensions [width,height]:[$widthHeight]) with ANSI characters rendering supported. Disable Markdown formatting. Colorize and stylize with ANSI cues; use ANSI characters *NOT* escape sequences." }
}
#$body = $bodyObject | ConvertTo-Json
Write-Host $body
$bodyObject['messages'] += @{ role = "user"; content = $Prompt }
$body = $bodyObject | ConvertTo-Json
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body
$responseContent = $response.choices[0].message.content
if ($RememberHistory) {
$global:gptHistory += @{ role = "assistant"; content = $responseContent }
}
Write-Output $responseContent
$stopwatch.Stop()
$elapsed = $stopwatch.Elapsed.ToString()
Write-Output "[$elapsed]"
}
}
Set-Alias -Name gpt -Value Query-Gpt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment