Sample code for 30+ languages & platforms
Zig

OpenAI (ChatGPT) Upload JSON String

See more OpenAI ChatGPT Examples

Demonstrate how to upload a file (from a JSON string) to a OpenAI user's organization.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl https://api.openai.com/v1/files \
    //   -H "Authorization: Bearer $OPENAI_API_KEY" \
    //   -F purpose="fine-tune" \
    //   -F file="@mydata.jsonl"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.setHttpVerb("POST");
    req.setPath("/v1/files");
    req.setContentType("multipart/form-data");
    req.addParam("purpose", "fine-tune");

    // { "prompt": "aa", "completion": "bb" }
    // { "prompt": "cc", "completion": "dd" }
    const sb_json_data = try chilkat.StringBuilder.init();
    defer sb_json_data.deinit();
    sb_json_data.appendLine("{ \"prompt\": \"aa\", \"completion\": \"bb\" }", true) catch {};
    sb_json_data.appendLine("{ \"prompt\": \"cc\", \"completion\": \"dd\" }", true) catch {};
    try req.addStringForUpload2("file", "mydata2.jsonl", try sb_json_data.getAsString(alloc), "utf-8", "application/octet-stream");

    // This is NOT a real key.  Change the "sk-vi...." to your own key.
    req.addHeader("Authorization", "Bearer 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");
    req.addHeader("Expect", "100-continue");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("api.openai.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "id": "file-XjGxS3KTG0uNmNOK362iJua3",
    //   "object": "file",
    //   "bytes": 140,
    //   "created_at": 1613779121,
    //   "filename": "mydata2.jsonl",
    //   "purpose": "fine-tune"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    _ = try j_resp.stringOf(alloc, "id");
    _ = try j_resp.stringOf(alloc, "object");
    _ = j_resp.intOf("bytes");
    _ = j_resp.intOf("created_at");
    _ = try j_resp.stringOf(alloc, "filename");
    _ = try j_resp.stringOf(alloc, "purpose");
}