Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
- $form = New-Object System.Windows.Forms.Form
- $form.Text = "ChatGPT"
- $form.Width = 600
- $form.Height = 400
- $messageBox = New-Object System.Windows.Forms.TextBox
- $messageBox.Multiline = $true
- $messageBox.ReadOnly = $true
- $messageBox.ScrollBars = "Vertical"
- $messageBox.Location = New-Object System.Drawing.Point(10, 10)
- $messageBox.Size = New-Object System.Drawing.Size(560, 280)
- $form.Controls.Add($messageBox)
- $messageInput = New-Object System.Windows.Forms.TextBox
- $messageInput.Multiline = $true
- $messageInput.ScrollBars = "Vertical"
- $messageInput.Location = New-Object System.Drawing.Point(10, 300)
- $messageInput.Size = New-Object System.Drawing.Size(450, 50)
- $form.Controls.Add($messageInput)
- $sendButton = New-Object System.Windows.Forms.Button
- $sendButton.Location = New-Object System.Drawing.Point(470, 300)
- $sendButton.Size = New-Object System.Drawing.Size(100, 50)
- $sendButton.Text = "Send"
- $form.Controls.Add($sendButton)
- $OPENAI_API_KEY = "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"
- $PROXY_URL = "https://api.openai.com/v1"
- $MAX_LINES = 2048
- $GPT_MODEL = "text-davinci-003"
- function SendMessage() {
- $message = $messageInput.Text.Trim()
- if ([string]::IsNullOrWhiteSpace($message)) {
- return
- }
- $messageInput.Text = ""
- $messageBox.AppendText("You: $message`n")
- $prompt = "$message`nAI:"
- $requestBody = @{
- prompt = $prompt
- max_tokens = 2048
- temperature = 1
- n = 25
- stop = "AI:"
- } | ConvertTo-Json
- try {
- $response = Invoke-RestMethod -Uri "$PROXY_URL/engines/$GPT_MODEL/completions" -Method Post -ContentType "application/json" -Headers @{ "Authorization" = "Bearer $OPENAI_API_KEY" } -Body $requestBody
- $answer = $response.choices[0].text.Trim()
- $messageBox.AppendText("AI: $answer`n")
- # Limit the number of lines in the message box
- $lines = $messageBox.Text -split "`n"
- if ($lines.Length -gt $MAX_LINES) {
- $messageBox.Text = ($lines | Select-Object -Last $MAX_LINES) -join "`n"
- }
- # Scroll to the bottom of the message box
- $messageBox.SelectionStart = $messageBox.Text.Length
- $messageBox.ScrollToCaret()
- } catch {
- Write-Error "Failed to send message: $_"
- }
- }
- $sendButton.Add_Click({ SendMessage })
- $messageInput.Add_KeyDown({
- if ($_.KeyCode -eq "Enter" -and !($_.Shift)) {
- # Enter
- $_.SuppressKeyPress = $true
- SendMessage
- }
- })
- $messageBox.Add_MouseWheel({
- if ($_.Delta -lt 0) {
- # Scrolling down
- $messageBox.TopIndex += 1
- } else {
- # Scrolling up
- $messageBox.TopIndex -= 1
- }
- })
- $form.Add_Shown({ $messageInput.Focus() })
- [void] $form.ShowDialog()
Advertisement
Comments
-
- it seems you leaked your OpenAI key to the public!
Add Comment
Please, Sign In to add comment
Advertisement