Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoAi
    String sContentType
    String sLocalFilePath
    String sFilenameOnServer
    String sFile_id
    Variant vBd
    Handle hoBd
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatAi)) To hoAi
    If (Not(IsComObjectCreated(hoAi))) Begin
        Send CreateComObject of hoAi
    End

    // Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
    Set ComProvider Of hoAi To "openai"
    // Use your provider's API key.
    Set ComApiKey Of hoAi To "MY_API_KEY"

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

    Move "image/jpeg" To sContentType
    Move "qa_data/jpg/starfish.jpg" To sLocalFilePath
    Move "starfish.jpg" To sFilenameOnServer

    // Upload directly from a file.
    Get ComUploadFile Of hoAi sLocalFilePath sContentType To sFile_id
    Get ComLastMethodSuccess Of hoAi To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Showln "AI File Upload Failed."
    End
    Else Begin
        Showln "File ID: " sFile_id
        Showln "File uploaded."
    End

    // Upload from the contents of a Chilkat BinData
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComLoadFile Of hoBd sLocalFilePath To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBd To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoBd to vBd
    Get ComUploadFileBd Of hoAi vBd sFilenameOnServer sContentType To sFile_id
    Get ComLastMethodSuccess Of hoAi To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Showln "AI File Upload Failed."
    End
    Else Begin
        Showln "File ID: " sFile_id
        Showln "File uploaded."
    End



End_Procedure