(no title)
aeonflux | 3 years ago
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|3 years ago