Files
fil/docs/snippets/dart/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.2 KiB

import 'dart:convert';

import 'package:kreuzberg/kreuzberg.dart';

Future<void> main() async {
  final schema = jsonEncode(<String, Object?>{
    'type': 'object',
    'properties': <String, Object?>{
      'title': <String, Object?>{'type': 'string'},
      'authors': <String, Object?>{
        'type': 'array',
        'items': <String, Object?>{'type': 'string'},
      },
      'date': <String, Object?>{'type': 'string'},
    },
    'required': <String>['title', 'authors', 'date'],
    'additionalProperties': false,
  });

  final config = ExtractionConfig(
    useCache: true,
    enableQualityProcessing: true,
    forceOcr: false,
    disableOcr: false,
    structuredExtraction: StructuredExtractionConfig(
      schema: schema,
      schemaName: 'paper_metadata',
      strict: true,
      llm: const LlmConfig(model: 'openai/gpt-4o-mini'),
    ),
    resultFormat: ResultFormat.unified,
    outputFormat: OutputFormat.plain(),
    includeDocumentStructure: false,
    maxArchiveDepth: 3,
    useLayoutForMarkdown: false,
  );

  final result = await KreuzbergBridge.extractFile('paper.pdf', null, config);
  final structured = result.structuredOutput;
  if (structured != null) {
    print(structured);
  }
}