Advertisement
Cogger

ChatGPT-One-Shot.ps1

May 4th, 2023
948
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Originally written by, https://github.com/yzwijsen
  2. # https://raw.githubusercontent.com/yzwijsen/chatgpt-powershell/main/ChatGPT-One-Shot.ps1
  3.  
  4. <#
  5. This script will take a rough email draft and help you turn it into a professionally written email.
  6. It's a minimal example showing how to set a system message in order to elicit a one-shot response from ChatGPT.
  7. #>
  8.  
  9. # Define API key and endpoint
  10. $ApiKey = "<your API key>"
  11. $ApiEndpoint = "https://api.openai.com/v1/chat/completions"
  12.  
  13. <#
  14. System message.
  15. You can use this to give the AI instructions on what to do, how to act or how to respond to future prompts.
  16. Default value for ChatGPT = "You are a helpful assistant."
  17. #>
  18. $AiSystemMessage = "You are an email writing assistant. I will provide you with a rough draft of an email or some general message that I want to convey and you will respond with a clean, professionally writen email. Please provide 3 seperate answers ranging from informal, formal to business formal. All answers must be in the same language as the provided input."
  19.  
  20. # Function to send a message to ChatGPT
  21. function Invoke-ChatGPT ($message) {
  22.     # List of Hashtables that will hold the system message and user message.
  23.     [System.Collections.Generic.List[Hashtable]]$messages = @()
  24.  
  25.     # Sets the initial system message
  26.     $messages.Add(@{"role" = "system"; "content" = $AiSystemMessage}) | Out-Null
  27.  
  28.     # Add the user input
  29.     $messages.Add(@{"role"="user"; "content"=$message})
  30.  
  31.     # Set the request headers
  32.     $headers = @{
  33.     "Content-Type" = "application/json"
  34.     "Authorization" = "Bearer $ApiKey"
  35.     }  
  36.  
  37.     # Set the request body
  38.     $requestBody = @{
  39.         "model" = "gpt-3.5-turbo"
  40.         "messages" = $messages
  41.         "max_tokens" = 2000 # Max amount of tokens the AI will respond with
  42.         "temperature" = 0.5 # lower is more coherent and conservative, higher is more creative and diverse.
  43.     }
  44.  
  45.     # Send the request
  46.     $response = Invoke-RestMethod -Method POST -Uri $ApiEndpoint -Headers $headers -Body (ConvertTo-Json $requestBody)
  47.  
  48.     # Return the message content
  49.     return $response.choices[0].message.content
  50. }
  51.  
  52. # Get user input
  53. $userInput = Read-Host "Rough email draft or message"
  54.  
  55. # Query ChatGPT
  56. $AiResponse = Invoke-ChatGPT $UserInput
  57.  
  58. # Show response
  59. write-output $AiResponse
  60.  
  61. Read-Host "Press enter to Exit..."
Advertisement
Comments
  • Cogger
    1 year
    # text 0.12 KB | 0 0
    1. Provide a rough email draft or message and receive a professionally written response in three different levels of formality.
Add Comment
Please, Sign In to add comment
Advertisement