Sample code for 30+ languages & platforms
Ruby

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 Ruby Downloads

Ruby
require 'chilkat'

success = false

ai = Chilkat::CkAi.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.put_Provider("openai")
# Use your provider's API key.
ai.put_ApiKey("MY_API_KEY")

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

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

# Upload directly from a file.
file_id = ai.uploadFile(localFilePath,contentType)
if (ai.get_LastMethodSuccess() == false)
    print ai.lastErrorText() + "\n";
    print "AI File Upload Failed." + "\n";
else
    print "File ID: " + file_id + "\n";
    print "File uploaded." + "\n";
end

# Upload from the contents of a Chilkat BinData
bd = Chilkat::CkBinData.new()
success = bd.LoadFile(localFilePath)
if (success == false)
    print bd.lastErrorText() + "\n";
    exit
end

file_id = ai.uploadFileBd(bd,filenameOnServer,contentType)
if (ai.get_LastMethodSuccess() == false)
    print ai.lastErrorText() + "\n";
    print "AI File Upload Failed." + "\n";
else
    print "File ID: " + file_id + "\n";
    print "File uploaded." + "\n";
end