Files
fil/docs/snippets/zig/llm/structured_extraction.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.1 KiB

const std = @import("std");
const kreuzberg = @import("kreuzberg");

// Structured extraction is configured via the JSON `structured_extraction`
// field on `ExtractionConfig`. The schema is a JSON Schema string and
// `llm.model` selects the provider via liter-llm.
pub fn main() !void {
    const config_json =
        \\{
        \\  "structured_extraction": {
        \\    "schema": "{\"type\":\"object\",\"properties\":{\"title\":{\"type\":\"string\"},\"authors\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"date\":{\"type\":\"string\"}},\"required\":[\"title\",\"authors\",\"date\"],\"additionalProperties\":false}",
        \\    "schema_name": "Paper",
        \\    "strict": true,
        \\    "llm": {
        \\      "model": "openai/gpt-4o-mini"
        \\    }
        \\  }
        \\}
    ;

    const result_json = try kreuzberg.extract_file_sync("paper.pdf", null, config_json);
    defer std.heap.c_allocator.free(result_json);

    const stdout = std.io.getStdOut().writer();
    try stdout.print("{s}\n", .{result_json});
}