Advertisement
Ewgeniy

Untitled

Feb 23rd, 2023 (edited)
2,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. 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
  2. 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 }
  3. 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
  4.         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  
  5.     return table.concat(result, "&") end  local function get_openai_result()
  6.     local encoded_data = encode_data(data)
  7.     local response, err = internet.request(endpoint, encoded_data, headers)
  8.     if not response then         error("Failed to send HTTP request: " .. err)     end
  9.          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
  10. end
  11. local success, result = pcall(get_openai_result) if success then     print(result) -- Process the result here
  12. else     handle_error(result) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement