Files
fil/docs/snippets/zig/llm/structured_extraction.md

31 lines
1.1 KiB
Markdown
Raw Normal View History

2026-06-01 23:40:55 +02:00
<!-- snippet:syntax-only -->
```zig title="Zig"
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});
}
```