Tcl
Tcl
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 theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set ai [new_CkAi]
# The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID.
CkAi_put_Provider $ai "openai"
# Use your provider's API key.
CkAi_put_ApiKey $ai "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.
set contentType "application/json"
set localFilePath "qa_data/hamlet.json"
set filenameOnServer "hamlet.json"
# Upload directly from a file.
set file_id [CkAi_uploadFile $ai $localFilePath $contentType]
if {[CkAi_get_LastMethodSuccess $ai] == 0} then {
puts [CkAi_lastErrorText $ai]
puts "AI File Upload Failed."
} else {
puts "File ID: $file_id"
puts "File uploaded."
}
# Upload from the contents of a StringBuilder
set sb [new_CkStringBuilder]
set success [CkStringBuilder_LoadFile $sb $localFilePath "utf-8"]
if {$success == 0} then {
puts [CkStringBuilder_lastErrorText $sb]
delete_CkAi $ai
delete_CkStringBuilder $sb
exit
}
set file_id [CkAi_uploadFileSb $ai $sb $filenameOnServer $contentType]
if {[CkAi_get_LastMethodSuccess $ai] == 0} then {
puts [CkAi_lastErrorText $ai]
puts "AI File Upload Failed."
} else {
puts "File ID: $file_id"
puts "File uploaded."
}
# Upload from the contents of a BinData
set bd [new_CkBinData]
set success [CkBinData_LoadFile $bd $localFilePath]
if {$success == 0} then {
puts [CkBinData_lastErrorText $bd]
delete_CkAi $ai
delete_CkStringBuilder $sb
delete_CkBinData $bd
exit
}
set file_id [CkAi_uploadFileBd $ai $bd $filenameOnServer $contentType]
if {[CkAi_get_LastMethodSuccess $ai] == 0} then {
puts [CkAi_lastErrorText $ai]
puts "AI File Upload Failed."
} else {
puts "File ID: $file_id"
puts "File uploaded."
}
delete_CkAi $ai
delete_CkStringBuilder $sb
delete_CkBinData $bd