Advertisement
dergachoff

Mac Automator GPT Correction/Rewriting Service

Mar 18th, 2023
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AppleScript 1.49 KB | Source Code | 0 0
  1. on run {input, parameters}
  2.     set originalText to input as string
  3.    
  4.     set apiKey to “YOURAPIKEYHERE” -- Replace with your GPT-3.5 Turbo API key
  5.    
  6.     set promptTemplate to "Please rewrite the following text using proper English: "
  7.     set promptText to promptTemplate & originalText
  8.    
  9.     set apiEndpoint to "https://api.openai.com/v1/chat/completions"
  10.    
  11.     set requestBody to "{ \"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"" & promptText & "\"}], \"temperature\": 0.7 }"
  12.    
  13.     set curlCommand to "curl -s -X POST -H \"Content-Type: application/json\" -H \"Authorization: Bearer " & apiKey & "\" --data " & (quoted form of requestBody) & " " & apiEndpoint
  14.    
  15.     set jsonResponse to ""
  16.     try
  17.         set jsonResponse to do shell script curlCommand
  18.     on error errMsg
  19.         display dialog "Error executing curl command: " & errMsg buttons {"OK"} default button 1
  20.         return
  21.     end try
  22.    
  23.     set theResponse to ""
  24.    
  25.     try
  26.         set theResponse to (do shell script "/usr/bin/python3 -c \"import json, sys; jsonData = json.loads(sys.argv[1]); content = jsonData['choices'][0]['message']['content'].strip(); print(content.encode('unicode_escape').decode('unicode_escape'))\" " & (quoted form of jsonResponse))
  27.     on error errMsg
  28.         display dialog "Error processing JSON response: " & errMsg & " | JSON: " & jsonResponse buttons {"OK"} default button 1
  29.         return
  30.     end try
  31.    
  32.     return theResponse
  33. end run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement