Sample code for 30+ languages & platforms
DataFlex

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

See more AI Examples

Some AI proviers have a Files API that lets you upload content once and then reference it later in generation requests using a file_id. This is especially useful for large documents, reuse across multiple prompts, or keeping inputs structured and separate from your prompt text.

This example demonstrates how to use a file_id in a request.

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

    // The provider can be "openai", "google", "claude", "mistral", "deepseek", "xai", or any provider
    // that supports referencing pre-uploaded files by a file_id (or file uri).
    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 a File to an AI Provider (OpenAI, Google, Antropic, X)

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

    Get ComInputAddText Of hoAi "Explain why Hamlet delays taking revenge, using only evidence from the file." 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