Created
July 13, 2023 12:28
-
-
Save ivryb/646da11d9a5dbb2151a2053c4d510dd0 to your computer and use it in GitHub Desktop.
Correct selection with ChatGPT
This file contains hidden or 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
/* | |
# Correct selection with ChatGPT | |
Fix grammar and spelling mistakes in any text field. | |
Highlight some text and press `cmd+option+g` to send it through ChatGPT to replace the text response. | |
*/ | |
// Name: Correct selection | |
// Description: Fix grammar and spelling mistakes in any text field. | |
// Author: Evan Fisher | |
// Twitter: @ivryb | |
// Shortcut: cmd option g | |
import '@johnlindquist/kit'; | |
import Bottleneck from 'bottleneck'; | |
import { createChat } from 'completions'; | |
const openAiKey = await env('OPENAI_API_KEY', { | |
hint: `Grab a key from <a href="https://platform.openai.com/account/api-keys">here</a>`, | |
}); | |
const chat = createChat({ | |
apiKey: openAiKey, | |
model: 'gpt-3.5-turbo', | |
}); | |
const correctionPrompt = (text) => | |
`Please fix the grammar and spelling of the following text and return it without any other changes: ###${text}###`; | |
const limiter = new Bottleneck({ | |
maxConcurrent: 1, | |
minTime: 100, | |
}); | |
const type = (text) => { | |
return new Promise((resolve) => { | |
keyboard.type(text); | |
resolve(); | |
}); | |
}; | |
const wrappedType = limiter.wrap(type); | |
const text = await getSelectedText(); | |
if (text) { | |
await chat.sendMessage(correctionPrompt(text), { | |
onUpdate: async ({ message }) => { | |
const content = message.choices[0]?.delta?.content; | |
if (content) { | |
wrappedType(content); | |
} | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment