Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.4.0+

Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads an image file to an AI provider using the provider's File API and returns the id that can later be used to reference the file in an query. This currently works with ChatGPT and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local ai = chilkat.Ai.new()

-- Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
ai.provider = "openai"
-- Use your provider's API key.
ai.api_key = "MY_API_KEY"

-- We can upload directly from a file in the filesystem, or from a Chilkat BinData.

local contentType = "image/jpeg"
local localFilePath = "qa_data/jpg/starfish.jpg"
local filenameOnServer = "starfish.jpg"

-- Upload directly from a file.
local file_id = ai:upload_file(localFilePath, contentType)
if not ai.last_method_success then
    print(ai.last_error_text)
    print("AI File Upload Failed.")
else
    print("File ID: " .. file_id)
    print("File uploaded.")
end

-- Upload from the contents of a Chilkat BinData
local bd = chilkat.BinData.new()
local success = bd:load_file(localFilePath)
if not success then
    print(bd.last_error_text)
    return
end

file_id = ai:upload_file_bd(bd, filenameOnServer, contentType)
if not ai.last_method_success then
    print(ai.last_error_text)
    print("AI File Upload Failed.")
else
    print("File ID: " .. file_id)
    print("File uploaded.")
end