Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local internet = require("internet") local api_key = "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" local endpoint = "https://api.openai.com/v1/engines/davinci/search" -- Add this line to include a User-Agent header in the request
- local headers = { ["User-Agent"] = "Mozilla/5.0 (Linux; Android 10; MAR-LX1M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Mobile Safari/537.36", ["Content-Type"] = "application/x-www-form-urlencoded", ["Authorization"] = "Bearer " .. api_key }
- local data = { documents = {}, query = "hi, dude", file = "", model = "", max_rerank = 10, return_metadata = false } local function encode_data(data) local result = {} for key, value in pairs(data) do
- if type(value) == "string" then table.insert(result, key .. "=" .. value) elseif type(value) == "table" then for _, v in ipairs(value) do table.insert(result, key .. "=" .. v) end end end
- return table.concat(result, "&") end local function get_openai_result()
- local encoded_data = encode_data(data)
- local response, err = internet.request(endpoint, encoded_data, headers)
- if not response then error("Failed to send HTTP request: " .. err) end
- local result = "" for chunk in response do result = result .. chunk end return result end local function handle_error(error_message) print("Error: " .. error_message) -- Log the error to a file or service for further analysis
- end
- local success, result = pcall(get_openai_result) if success then print(result) -- Process the result here
- else handle_error(result) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement