Sample code for 30+ languages & platforms
DataFlex

Referencing a Pre-Uploaded Image File by file_id in an AI Request

See more AI Examples

Currently, OpenAI is the only AI provider that supports referencing a pre-uploaded image in a request for text generation.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoAi
    String sFile_id
    Variant vSbResponse
    Handle hoSbResponse
    String sTemp1

    Move False To iSuccess

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // OpenAI is currently the only AI provider that supports referencing a pre-uploaded image by file_id
    // in a request for text generation.
    Set ComProvider Of hoAi To "openai"

    // Use your provider's API key.
    Set ComApiKey Of hoAi To "MY_API_KEY"

    // Choose a model.
    Set ComModel Of hoAi To "gpt-5-mini"

    // See the following example showing how to upload a file
    // Upload an Image File to an AI Provider

    // Use the file_id of the pre-uploaded file.
    Move "file-5CnBeuFasqNcAfti3cx4Fe" To sFile_id
    Get ComInputAddImageFileId Of hoAi sFile_id "application/json" To iSuccess

    Get ComInputAddText Of hoAi "What is in this image?" To iSuccess

    // Ask the AI for text output.
    Get ComAsk Of hoAi "text" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the text response.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End
    Get pvComObject of hoSbResponse to vSbResponse
    Get ComGetOutputTextSb Of hoAi vSbResponse To iSuccess
    Get ComGetAsString Of hoSbResponse To sTemp1
    Showln sTemp1


End_Procedure