7 ms·
Of course. I use this in Hammerspoon. The API call is simply in Lua.
by aeonflux 4y ago
Of course. I use this in Hammerspoon. The API call is simply in Lua.
- behnamoh 4y agoHow did you get the selected text in the focused app and replace it with the GPT response? I worked on it for hours and couldn't do it. I'd appreciate it if you could share that config.
- aeonflux 4y agoIf you want to go full circle you can send the ⌘-C and ⌘-V key events before/after they query. This will just send selected text and replace this with answer. I prefer not to do that, since queries are quite slow and I rarely block waiting for round trip. I also use vim and vim mode in lot's of apps, so replace works differently there. I prefer to use clipboard as the exchange place. I select text, copy it, then query the service. Once the query completes I can see the answer in popup and can paste it to my current editing place. This is the code: local hyper = {"cmd", "alt", "ctrl"} hs.hotkey.bind(hyper, "Y", function() local url = "https://api.openai.com/v1/completions" local api_key = "..." local headers = { authorization="Bearer " .. api_key, ["content-type"]="application/json", accept="application/json" } -- hs.eventtap.keyStroke({"cmd"}, "c") local message = hs.pasteboard.readString() local data = { prompt=message, model="text-davinci-003", max_tokens=32, temperature=0 } hs.notify.new({title="OpenAI query", informativeText=message}):send() hs.http.asyncPost(url, hs.json.encode(data), headers, function(status, body, headers) local response = hs.json.decode(body) local answer = response["choices"][1]["text"] print(body) hs.notify.new({title="OpenAI response", informativeText=answer}):send() hs.pasteboard.writeObjects(answer) -- hs.eventtap.keyStroke({"cmd"}, "v") end) end)
- behnamoh 4y agoThanks a ton! I'm going to test that. You have a good point about Vim. I tried the OP's method in Neovim and it didn't work exactly for the reason you mentioned. Clipboard as the exchange place sounds more reasonable.