Zig Requires Chilkat v11.4.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const ai = try chilkat.Ai.init();
defer ai.deinit();
// Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
ai.setProvider("openai");
// Use your provider's API key.
ai.setApiKey("MY_API_KEY");
// We can upload directly from a file in the filesystem, or from a Chilkat BinData.
const content_type = "image/jpeg";
const local_file_path = "qa_data/jpg/starfish.jpg";
const filename_on_server = "starfish.jpg";
// Upload directly from a file.
var file_id: [:0]const u8 = ai.uploadFile(alloc, local_file_path, content_type) catch "";
if (!ai.getLastMethodSuccess()) {
std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
std.debug.print("AI File Upload Failed.\n", .{});
} else {
std.debug.print("File ID: {s}\n", .{file_id});
std.debug.print("File uploaded.\n", .{});
}
// Upload from the contents of a Chilkat BinData
const bd = try chilkat.BinData.init();
defer bd.deinit();
bd.loadFile(local_file_path) catch {
std.debug.print("{s}\n", .{try bd.getLastErrorText(alloc)});
return;
};
file_id = ai.uploadFileBd(alloc, bd, filename_on_server, content_type) catch "";
if (!ai.getLastMethodSuccess()) {
std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
std.debug.print("AI File Upload Failed.\n", .{});
} else {
std.debug.print("File ID: {s}\n", .{file_id});
std.debug.print("File uploaded.\n", .{});
}
}