Advertisement
Thunder-Menu

ChatGpt3.ps1

Apr 12th, 2023 (edited)
2,498
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.86 KB | Source Code | 0 0
  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3.  
  4. $form = New-Object System.Windows.Forms.Form
  5. $form.Text = "ChatGPT"
  6. $form.Width = 600
  7. $form.Height = 400
  8.  
  9. $messageBox = New-Object System.Windows.Forms.TextBox
  10. $messageBox.Multiline = $true
  11. $messageBox.ReadOnly = $true
  12. $messageBox.ScrollBars = "Vertical"
  13. $messageBox.Location = New-Object System.Drawing.Point(10, 10)
  14. $messageBox.Size = New-Object System.Drawing.Size(560, 280)
  15. $form.Controls.Add($messageBox)
  16.  
  17. $messageInput = New-Object System.Windows.Forms.TextBox
  18. $messageInput.Multiline = $true
  19. $messageInput.ScrollBars = "Vertical"
  20. $messageInput.Location = New-Object System.Drawing.Point(10, 300)
  21. $messageInput.Size = New-Object System.Drawing.Size(450, 50)
  22. $form.Controls.Add($messageInput)
  23.  
  24. $sendButton = New-Object System.Windows.Forms.Button
  25. $sendButton.Location = New-Object System.Drawing.Point(470, 300)
  26. $sendButton.Size = New-Object System.Drawing.Size(100, 50)
  27. $sendButton.Text = "Send"
  28. $form.Controls.Add($sendButton)
  29.  
  30. $OPENAI_API_KEY = "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"
  31. $PROXY_URL = "https://api.openai.com/v1"
  32. $MAX_LINES = 2048
  33. $GPT_MODEL = "text-davinci-003"
  34.  
  35. function SendMessage() {
  36.     $message = $messageInput.Text.Trim()
  37.  
  38.     if ([string]::IsNullOrWhiteSpace($message)) {
  39.         return
  40.     }
  41.  
  42.     $messageInput.Text = ""
  43.     $messageBox.AppendText("You: $message`n")
  44.  
  45.     $prompt = "$message`nAI:"
  46.  
  47.     $requestBody = @{
  48.         prompt = $prompt
  49.         max_tokens = 2048
  50.         temperature = 1
  51.         n = 25
  52.         stop = "AI:"
  53.     } | ConvertTo-Json
  54.  
  55.     try {
  56.         $response = Invoke-RestMethod -Uri "$PROXY_URL/engines/$GPT_MODEL/completions" -Method Post -ContentType "application/json" -Headers @{ "Authorization" = "Bearer $OPENAI_API_KEY" } -Body $requestBody
  57.         $answer = $response.choices[0].text.Trim()
  58.  
  59.         $messageBox.AppendText("AI: $answer`n")
  60.  
  61.         # Limit the number of lines in the message box
  62.         $lines = $messageBox.Text -split "`n"
  63.         if ($lines.Length -gt $MAX_LINES) {
  64.             $messageBox.Text = ($lines | Select-Object -Last $MAX_LINES) -join "`n"
  65.         }
  66.  
  67.         # Scroll to the bottom of the message box
  68.         $messageBox.SelectionStart = $messageBox.Text.Length
  69.         $messageBox.ScrollToCaret()
  70.     } catch {
  71.         Write-Error "Failed to send message: $_"
  72.     }
  73. }
  74.  
  75. $sendButton.Add_Click({ SendMessage })
  76.  
  77. $messageInput.Add_KeyDown({
  78.     if ($_.KeyCode -eq "Enter" -and !($_.Shift)) {
  79.         # Enter
  80.         $_.SuppressKeyPress = $true
  81.         SendMessage
  82.     }
  83. })
  84.  
  85. $messageBox.Add_MouseWheel({
  86.     if ($_.Delta -lt 0) {
  87.         # Scrolling down
  88.         $messageBox.TopIndex += 1
  89.     } else {
  90.         # Scrolling up
  91.         $messageBox.TopIndex -= 1
  92.     }
  93. })
  94.  
  95. $form.Add_Shown({ $messageInput.Focus() })
  96.  
  97. [void] $form.ShowDialog()
  98.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement