Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- on run {input, parameters}
- set originalText to input as string
- set apiKey to "YOURGPTAPIKEY" -- Replace with your GPT-3.5 Turbo API key
- set pythonScript to "import json, sys, urllib.request
- original_text = sys.argv[1]
- api_key = sys.argv[2]
- prompt_template = 'Please rewrite the following text as English native, don’t put the answer in quotes, don’t return errors, just correct text: '
- prompt_text = prompt_template + original_text
- api_endpoint = 'https://api.openai.com/v1/chat/completions'
- request_headers = {
- 'Content-Type': 'application/json',
- 'Authorization': f'Bearer {api_key}'
- }
- request_body = {
- 'model': 'gpt-3.5-turbo',
- 'messages': [{'role': 'user', 'content': prompt_text}],
- 'temperature': 0.7
- }
- request_data = json.dumps(request_body).encode('utf-8')
- req = urllib.request.Request(api_endpoint, data=request_data, headers=request_headers)
- try:
- with urllib.request.urlopen(req) as response:
- response_json = json.loads(response.read())
- content = response_json['choices'][0]['message']['content'].strip()
- print(content)
- except urllib.error.HTTPError as e:
- error_message = e.read().decode('utf-8')
- print('Error:', error_message, file=sys.stderr)
- sys.exit(1)
- "
- set theResponse to ""
- try
- set theResponse to (do shell script "/usr/bin/python3 -c " & (quoted form of pythonScript) & " " & (quoted form of originalText) & " " & apiKey)
- on error errMsg
- display dialog "Error processing JSON response: " & errMsg buttons {"OK"} default button 1
- return
- end try
- return theResponse
- end run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement