|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 Embabel Software, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.embabel.common.ai.converters |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonProcessingException |
| 19 | +import com.fasterxml.jackson.core.util.DefaultIndenter |
| 20 | +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter |
| 21 | +import com.fasterxml.jackson.databind.JsonNode |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 23 | +import com.github.victools.jsonschema.generator.* |
| 24 | +import com.github.victools.jsonschema.module.jackson.JacksonModule |
| 25 | +import com.github.victools.jsonschema.module.jackson.JacksonOption |
| 26 | +import org.slf4j.Logger |
| 27 | +import org.slf4j.LoggerFactory |
| 28 | +import org.springframework.ai.converter.StructuredOutputConverter |
| 29 | +import org.springframework.ai.util.LoggingMarkers |
| 30 | +import org.springframework.core.ParameterizedTypeReference |
| 31 | +import java.lang.reflect.Type |
| 32 | + |
| 33 | +/** |
| 34 | + * A Kotlin version of [org.springframework.ai.converter.BeanOutputConverter] that allows for customization |
| 35 | + * of the used schema via [postProcessSchema] |
| 36 | + */ |
| 37 | +open class JacksonOutputConverter<T> protected constructor( |
| 38 | + private val type: Type, |
| 39 | + val objectMapper: ObjectMapper, |
| 40 | +) : StructuredOutputConverter<T> { |
| 41 | + |
| 42 | + constructor( |
| 43 | + clazz: Class<T>, |
| 44 | + objectMapper: ObjectMapper, |
| 45 | + ) : this(clazz as Type, objectMapper) |
| 46 | + |
| 47 | + constructor( |
| 48 | + typeReference: ParameterizedTypeReference<T>, |
| 49 | + objectMapper: ObjectMapper, |
| 50 | + ) : this(typeReference.type, objectMapper) |
| 51 | + |
| 52 | + protected val logger: Logger = LoggerFactory.getLogger(javaClass) |
| 53 | + |
| 54 | + val jsonSchema: String by lazy { |
| 55 | + val jacksonModule = JacksonModule( |
| 56 | + JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, |
| 57 | + JacksonOption.RESPECT_JSONPROPERTY_ORDER |
| 58 | + ) |
| 59 | + val configBuilder = SchemaGeneratorConfigBuilder( |
| 60 | + SchemaVersion.DRAFT_2020_12, |
| 61 | + OptionPreset.PLAIN_JSON |
| 62 | + ) |
| 63 | + .with(jacksonModule) |
| 64 | + .with(Option.FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT) |
| 65 | + val config = configBuilder.build() |
| 66 | + val generator = SchemaGenerator(config) |
| 67 | + val jsonNode: JsonNode = generator.generateSchema(this.type) |
| 68 | + postProcessSchema(jsonNode) |
| 69 | + val objectWriter = this.objectMapper.writer( |
| 70 | + DefaultPrettyPrinter() |
| 71 | + .withObjectIndenter(DefaultIndenter().withLinefeed(System.lineSeparator())) |
| 72 | + ) |
| 73 | + try { |
| 74 | + objectWriter.writeValueAsString(jsonNode) |
| 75 | + } catch (e: JsonProcessingException) { |
| 76 | + logger.error("Could not pretty print json schema for jsonNode: {}", jsonNode) |
| 77 | + throw RuntimeException("Could not pretty print json schema for " + this.type, e) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Empty template method that allows for customization of the JSON schema in subclasses. |
| 83 | + * @param jsonNode the JSON schema, in the form of a JSON node |
| 84 | + */ |
| 85 | + protected open fun postProcessSchema(jsonNode: JsonNode) { |
| 86 | + } |
| 87 | + |
| 88 | + override fun convert(text: String): T? { |
| 89 | + val unwrapped = unwrapJson(text) |
| 90 | + try { |
| 91 | + return this.objectMapper.readValue<Any?>(unwrapped, this.objectMapper.constructType(this.type)) as T? |
| 92 | + } catch (e: JsonProcessingException) { |
| 93 | + logger.error( |
| 94 | + LoggingMarkers.SENSITIVE_DATA_MARKER, |
| 95 | + "Could not parse the given text to the desired target type: \"{}\" into {}", unwrapped, this.type |
| 96 | + ) |
| 97 | + throw RuntimeException(e) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private fun unwrapJson(text: String): String { |
| 102 | + var result = text.trim() |
| 103 | + |
| 104 | + if (result.startsWith("```") && result.endsWith("```")) { |
| 105 | + result = result.removePrefix("```json") |
| 106 | + .removePrefix("```") |
| 107 | + .removeSuffix("```") |
| 108 | + .trim() |
| 109 | + } |
| 110 | + |
| 111 | + return result |
| 112 | + } |
| 113 | + |
| 114 | + override fun getFormat(): String = |
| 115 | + """| |
| 116 | + |Your response should be in JSON format. |
| 117 | + |Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation. |
| 118 | + |Do not include markdown code blocks in your response. |
| 119 | + |Remove the ```json markdown from the output. |
| 120 | + |Here is the JSON Schema instance your output must adhere to: |
| 121 | + |```${jsonSchema}``` |
| 122 | + |""".trimMargin() |
| 123 | +} |
0 commit comments