Spring-AI 如何使用Structured Output Converter将LLM输出转换为结构化数据

为什么需要结构化输出?

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 default FormatProvider implementation is provided.

  • AbstractMessageOutputConverter<T> - Supplies a pre-configured MessageConverter for converting LLM output into the desired format. No default FormatProvider implementation is provided.

  • BeanOutputConverter<T> - Configured with a designated Java class (e.g., Bean) or a ParameterizedTypeReference, this converter employs a FormatProvider implementation that directs the AI Model to produce a JSON response compliant with a RFC8259 , JSON Schema derived from the specified Java class. Subsequently, it utilizes an ObjectMapper to deserialize the JSON output into a Java object instance of the target class.

  • MapOutputConverter - Extends the functionality of AbstractMessageOutputConverter with a FormatProvider implementation that guides the AI Model to generate an RFC8259 compliant JSON response. Ad

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

enjoy编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值