Created
December 25, 2024 03:57
-
-
Save hitamu/fd66925e90116d87f9b7863bd46c738e to your computer and use it in GitHub Desktop.
JavaScript for Automation: summarize text with LLM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function run(input) { | |
const key = 'your-key-here'; | |
return summarizeText(input[0], key) | |
} | |
function summarizeText(text, apiKey) { | |
const app = Application.currentApplication(); | |
app.includeStandardAdditions = true; | |
const endpoint = 'https://api.openai.com/v1/chat/completions'; | |
const requestData = { | |
model: 'gpt-4o-mini', | |
messages: [ | |
{ | |
role: 'system', | |
content: 'You are a helpful assistant that provides concise summaries.' | |
}, | |
{ | |
role: 'user', | |
content: `Please summarize the following text: ${text}` | |
} | |
], | |
temperature: 0.7, | |
max_tokens: 500 | |
}; | |
try { | |
// Properly escape the JSON data for shell command | |
const escapedData = JSON.stringify(requestData).replace(/'/g, "'\\''"); | |
const curlCommand = `curl -s "${endpoint}" \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer ${apiKey}" \ | |
-d '${escapedData}'`; | |
const response = app.doShellScript(curlCommand); | |
const jsonResponse = JSON.parse(response); | |
return jsonResponse.choices[0].message.content; | |
} catch (error) { | |
return `Error: ${error}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment