Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oAi
LOCAL cContentType
LOCAL cLocalFilePath
LOCAL cFilenameOnServer
LOCAL cFile_id
LOCAL oBd

nSuccess := 0

oAi := CreateObject("Chilkat.Ai")

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

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

cContentType := "image/jpeg"
cLocalFilePath := "qa_data/jpg/starfish.jpg"
cFilenameOnServer := "starfish.jpg"

//  Upload directly from a file.
cFile_id := oAi:UploadFile(cLocalFilePath, cContentType)
IF (oAi:LastMethodSuccess == 0)
    ? oAi:LastErrorText
    ? "AI File Upload Failed."
ELSE
    ? "File ID: " + cFile_id
    ? "File uploaded."
ENDIF

//  Upload from the contents of a Chilkat BinData
oBd := CreateObject("Chilkat.BinData")
nSuccess := oBd:LoadFile(cLocalFilePath)
IF (nSuccess == 0)
    ? oBd:LastErrorText
    oAi:destroy()
    oBd:destroy()
    RETURN
ENDIF

cFile_id := oAi:UploadFileBd(oBd, cFilenameOnServer, cContentType)
IF (oAi:LastMethodSuccess == 0)
    ? oAi:LastErrorText
    ? "AI File Upload Failed."
ELSE
    ? "File ID: " + cFile_id
    ? "File uploaded."
ENDIF

oAi:destroy()
oBd:destroy()