Hello everyone. found the sources and decided to push them to git.
started playing around with the Chatgpt api via lua. its super easy and as i already compiled luasec linked with openssl some time ago.
Here's how you do it - Have Fun!
local https = require("https") --ssl.https
local ltn12 = require("ltn12")
local json = require("dkjson")
local api_key = "get an api key from https://platform.openai.com/"
local prompt = "Hello world from SciTE'S lua interpreter, do you know Scintilla ?"
local response_body = {}
local request_body = json.encode({
model = "gpt-3.5-turbo",
messages = {
{role = "user", content = prompt}
}
})
local res, code, headers, status = https.request{
url = "https://api.openai.com/v1/chat/completions",
method = "POST",
headers = {
["Content-Type"] = "application/json",
["Authorization"] = "Bearer " .. api_key,
["Content-Length"] = tostring(#request_body)
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body)
}
-- decode and write response
local response_text = table.concat(response_body)
local parsed = json.decode(response_text)
if parsed.error then
if parsed.error.code == "insufficient_quota" then
print("Error: Quota exceeded.")
else
print("API Error: " .. (parsed.error.message or "Unknown error"))
end
else
local answer = parsed.choices and parsed.choices[1] and parsed.choices[1].message.content or "No answer"
print("ChatGPT said:\n" ..answer)
end