为什么需要结构化输出?
Spring AI结构化输出转换器帮助将LLM输出转换为结构化格式。如下图所示
- 下游应用程序需要llm生成结构化输出,以便于进行解析
- 将llm生成结果快速转换为数据类型,如JSON、XML或Java类,这些数据类型可以传递给其他应用程序函数和方法
处理流程
数据流图
- 在LLM调用之前,转换器将格式指令附加到Prompt中,为模型提供生成所需输出结构的明确指导。这些指令充当蓝图,塑造模型的响应以符合指定的格式。
- 在LLM调用之后,转换器接受模型的输出文本并将其转换为结构化类型的实例。此转换过程包括解析原始文本输出并将其映射到相应的结构化数据表示(如JSON、XML或特定于域的数据结构)

提示: StructuredOutputConverter是将模型输出转换为结构化输出的最佳方法。- 因为模型可能不理解prompt或不能生成结构化输出,所以需要考虑实现验证机制以确保模型输出符合预期。
StructuredOutputConverter不用于LLM工具调用,因为Tool Calling在默认情况下固有地提供结构化输出。
Structured Output API
接口定义
/**
* Converts the (raw) LLM output into a structured responses of type. The
* {@link FormatProvider#getFormat()} method should provide the LLM prompt description of
* the desired format.
*
* @param <T> Specifies the desired response type.
* @author Mark Pollack
* @author Christian Tzolov
*/
public interface StructuredOutputConverter<T> extends Converter<String, T>, FormatProvider {
}
/**
* A converter converts a source object of type {@code S} to a target of type {@code T}.
*
* <p>Implementations of this interface are thread-safe and can be shared.
*
* <p>Implementations may additionally implement {@link ConditionalConverter}.
*/
@FunctionalInterface
public interface Converter<S, T> {
@Nullable
T convert(S source);
default <U> Converter<S, U> andThen(Converter<? super T, ? extends U> after) {
Assert.notNull(after, "'after' Converter must not be null");
return (s) -> {
T initialResult = (T)this.convert(s);
return initialResult != null ? after.convert(initialResult) : null;
};
}
}
/**
* Implementations of this interface provides instructions for how the output of a
* language generative should be formatted.
*
* @author Mark Pollack
*/
public interface FormatProvider {
/**
* Get the format of the output of a language generative.
* @return Returns a string containing instructions for how the output of a language
* generative should be formatted.
*/
String getFormat();
}
API视角下的数据流图

类图

-
AbstractConversionServiceOutputConverter<T>- Offers a pre-configured GenericConversionService for transforming LLM output into the desired format. No defaultFormatProviderimplementation is provided. -
AbstractMessageOutputConverter<T>- Supplies a pre-configured MessageConverter for converting LLM output into the desired format. No defaultFormatProviderimplementation is provided. -
BeanOutputConverter<T>- Configured with a designated Java class (e.g., Bean) or a ParameterizedTypeReference, this converter employs aFormatProviderimplementation that directs the AI Model to produce a JSON response compliant with aRFC8259,JSON Schemaderived from the specified Java class. Subsequently, it utilizes anObjectMapperto deserialize the JSON output into a Java object instance of the target class. -
MapOutputConverter- Extends the functionality ofAbstractMessageOutputConverterwith aFormatProviderimplementation that guides the AI Model to generate an RFC8259 compliant JSON response. Ad

最低0.47元/天 解锁文章
116






