Swift
Swift
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 theid 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 Swift Downloads
func chilkatTest() {
var success: Bool = false
let ai = CkoAi()!
// Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
ai.provider = "openai"
// Use your provider's API key.
ai.apiKey = "MY_API_KEY"
// We can upload directly from a file in the filesystem, or from a Chilkat BinData.
var contentType: String? = "image/jpeg"
var localFilePath: String? = "qa_data/jpg/starfish.jpg"
var filenameOnServer: String? = "starfish.jpg"
// Upload directly from a file.
var file_id: String? = ai.uploadFile(path: localFilePath, mimeType: contentType)
if ai.lastMethodSuccess == false {
print("\(ai.lastErrorText!)")
print("AI File Upload Failed.")
}
else {
print("File ID: \(file_id!)")
print("File uploaded.")
}
// Upload from the contents of a Chilkat BinData
let bd = CkoBinData()!
success = bd.loadFile(path: localFilePath)
if success == false {
print("\(bd.lastErrorText!)")
return
}
file_id = ai.uploadFileBd(bd: bd, filename: filenameOnServer, mimeType: contentType)
if ai.lastMethodSuccess == false {
print("\(ai.lastErrorText!)")
print("AI File Upload Failed.")
}
else {
print("File ID: \(file_id!)")
print("File uploaded.")
}
}