Sample code for 30+ languages & platforms
PowerBuilder

Upload a File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads a 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, Gemini, Claude, and Grok.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ai
string ls_ContentType
string ls_LocalFilePath
string ls_FilenameOnServer
string ls_File_id
oleobject loo_Sb
oleobject loo_Bd

li_Success = 0

loo_Ai = create oleobject
li_rc = loo_Ai.ConnectToNewObject("Chilkat.Ai")
if li_rc < 0 then
    destroy loo_Ai
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID.
loo_Ai.Provider = "openai"
// Use your provider's API key.
loo_Ai.ApiKey = "MY_API_KEY"

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

// Some AI providers require a content-type.
// Also, some AI providers are picky about what content-type's are accepted.
// Check the AI provider's documentation.
// "application/json" is generally always acceptable.
// "text/plain" can be used as a fallback for any text file.
ls_ContentType = "application/json"
ls_LocalFilePath = "qa_data/hamlet.json"
ls_FilenameOnServer = "hamlet.json"

// Upload directly from a file.
ls_File_id = loo_Ai.UploadFile(ls_LocalFilePath,ls_ContentType)
if loo_Ai.LastMethodSuccess = 0 then
    Write-Debug loo_Ai.LastErrorText
    Write-Debug "AI File Upload Failed."
else
    Write-Debug "File ID: " + ls_File_id
    Write-Debug "File uploaded."
end if

// Upload from the contents of a StringBuilder
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Sb.LoadFile(ls_LocalFilePath,"utf-8")
if li_Success = 0 then
    Write-Debug loo_Sb.LastErrorText
    destroy loo_Ai
    destroy loo_Sb
    return
end if

ls_File_id = loo_Ai.UploadFileSb(loo_Sb,ls_FilenameOnServer,ls_ContentType)
if loo_Ai.LastMethodSuccess = 0 then
    Write-Debug loo_Ai.LastErrorText
    Write-Debug "AI File Upload Failed."
else
    Write-Debug "File ID: " + ls_File_id
    Write-Debug "File uploaded."
end if

// Upload from the contents of a BinData
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Bd.LoadFile(ls_LocalFilePath)
if li_Success = 0 then
    Write-Debug loo_Bd.LastErrorText
    destroy loo_Ai
    destroy loo_Sb
    destroy loo_Bd
    return
end if

ls_File_id = loo_Ai.UploadFileBd(loo_Bd,ls_FilenameOnServer,ls_ContentType)
if loo_Ai.LastMethodSuccess = 0 then
    Write-Debug loo_Ai.LastErrorText
    Write-Debug "AI File Upload Failed."
else
    Write-Debug "File ID: " + ls_File_id
    Write-Debug "File uploaded."
end if



destroy loo_Ai
destroy loo_Sb
destroy loo_Bd