【Spring AI】08. 输出解析器

文章目录

Output Parsers

OutputParser接口允许您获取结构化输出,例如将输出映射到 Java 类或从 AI 模型的基于字符串的输出中获取值数组。

您可以将其类比为 Spring JDBC 概念中的RowMapper或ResultSetExtractor。开发人员希望快速将 AI 模型的结果转换为可以传递给应用程序中其他函数和方法的数据类型。OutputParser有助于实现这一目标。

API 概述


本节提供了OutputParser接口的指南。

OutputParser

这里是 OutputParser 接口定义

java 复制代码
public interface OutputParser<T> extends Parser<T>, FormatProvider {

}

它继承了 Parser 接口

java 复制代码
@FunctionalInterface
public interface Parser<T> {
    T parse(String text);
}

和 FormatProvider 接口

java 复制代码
public interface FormatProvider {

	String getFormat();

}

Parser接口解析文本字符串以生成类型 T 的实例。

FormatProvider为 AI 模型提供文本指令,以便格式化输出,以便可以通过Parser将其解析为类型 T。 这些文本指令通常附加在用户输入到 AI 模型的末尾。


可用实现


OutputParser接口具有以下可用的实现。

  • BeanOutputParser:指定 Java 类的 JSON 模式,并使用 JSON 模式规范的DRAFT_2020_12,因为 OpenAI 表示这将提供最佳结果。 AI 模型的 JSON 输出然后被反序列化为 Java 对象,也就是JavaBean。
  • MapOutputParser:类似于BeanOutputParser,但 JSON 负载被反序列化为java.util.Map<String, Object>实例。
  • ListOutputParser:指定输出为逗号分隔的列表。
    近期,OpenAI 模型已经付出了相当大的努力,以提高模型通过简单指定"以 JSON 返回"的能力,但并非所有模型都支持直接支持返回结构化数据。

示例用法


您可以运行一个完全可工作的示例 Spring AI Azure Workshop,其中的一部分演示了BeanOutputParser的使用。这个工程代码的一部分如下所示。

该示例的用例是使用 AI 模型为演员生成电影作品列表。

使用的用户提示词是

java 复制代码
String userMessage = """
        Generate the filmography for the actor {actor}.
        {format}
        """;

下面显示的类 ActorsFilms

java 复制代码
public class ActorsFilms {

	private String actor;

	private List<String> movies;

    // getters and toString omitted
}

这是一个控制器类,显示了这些类的使用

java 复制代码
    @GetMapping("/ai/output")
    public ActorsFilms generate(@RequestParam(value = "actor", defaultValue = "Jeff Bridges") String actor) {
        var outputParser = new BeanOutputParser<>(ActorsFilms.class);

        String userMessage =
                """
                Generate the filmography for the actor {actor}.
                {format}
                """;

        PromptTemplate promptTemplate = new PromptTemplate(userMessage, Map.of("actor", actor, "format", outputParser.getFormat() ));
        Prompt prompt = promptTemplate.create();
        Generation generation = chatClient.call(prompt).getResult();

        ActorsFilms actorsFilms = outputParser.parse(generation.getOutput().getContent());
        return actorsFilms;
    }

相关推荐
7177776 小时前
国内 AI 研发效能工具怎么选:2026 年基于公开信息的选型框架与 Gitee 实践
人工智能·gitee
Csvn6 小时前
换供应商要改一遍代码?把模型接入收进一层网关(C04)
人工智能·aigc·agent
孟健7 小时前
开源AutoClip切直播:1小时48分出19段,整月API只花0.08元
人工智能·开源·aigc
翰墨之道7 小时前
AI日报:豆包工作ARR超20亿,GPT-6.2跑分与实测割裂
人工智能·gpt·数据挖掘
Maynor9967 小时前
Trymodel 使用介绍:一个平台同时用 Claude、GPT、Gemini 和 Grok
人工智能·gpt·ai·程序员·教程·claude·语音
字节跳动视频云技术团队8 小时前
接大模型别再自己配 Key、搭代理了,火山引擎 IGA Pages 一步搞定
人工智能
阿里云大数据AI技术8 小时前
EMR Serverless StarRocks 产品介绍:从统一 OLAP 走向多模态分析与检索
人工智能
Forget the Dream9 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
学无止境小袁同学9 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴10 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib