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

39 lines
999 B
Markdown
Raw Permalink Normal View History

2026-06-01 23:40:55 +02:00
```kotlin title="Kotlin"
import dev.kreuzberg.*
import java.nio.file.Paths
import java.util.Optional
fun main() {
val schema = mapOf(
"type" to "object",
"properties" to mapOf(
"title" to mapOf("type" to "string"),
"authors" to mapOf("type" to "array", "items" to mapOf("type" to "string")),
"date" to mapOf("type" to "string")
),
"required" to listOf("title", "authors", "date"),
"additionalProperties" to false
)
val llm = LlmConfig.builder()
.withModel("openai/gpt-4o-mini")
.build()
val structured = StructuredExtractionConfig(
schema,
"document",
null,
true,
null,
llm
)
val config = ExtractionConfig.builder()
.withStructuredExtraction(Optional.of(structured))
.build()
val result = Kreuzberg.extractFileSync(Paths.get("paper.pdf"), null, config)
result.structuredOutput()?.let { println(it) }
}
```