langchain4j笔记-08

结构化输出

1. JSON Schema
java 复制代码
    @Test
    void test08() {
        ResponseFormat responseFormat = ResponseFormat.builder()
                .type(ResponseFormatType.JSON) // type can be either TEXT (default) or JSON
                .jsonSchema(JsonSchema.builder()
                        .name("Person") // OpenAI requires specifying the name for the schema
                        .rootElement(JsonObjectSchema.builder() // see [1] below
                                .addStringProperty("name")
                                .addIntegerProperty("age")
                                .addNumberProperty("height")
                                .addBooleanProperty("married")
                                .required("name", "age", "height", "married") // see [2] below
                                .build())
                        .build())
                .build();

        UserMessage userMessage = UserMessage.from("""
        John is 42 years old and lives an independent life.
        He stands 1.75 meters tall and carries himself with confidence.
        Currently unmarried, he enjoys the freedom to focus on his personal goals and interests.
        """);

        ChatRequest chatRequest = ChatRequest.builder()
                .responseFormat(responseFormat)
                .messages(userMessage)
                .build();

        ChatResponse chat = BASE_MODEL.chat(chatRequest);
        AiMessage aiMessage = chat.aiMessage();

    }

DeepSeek 似乎不支持这个JSON的格式声明

AiService中使用, 在springboot中不需要额外的声明就可以返回POJO
java 复制代码
interface PersonExtractor {
    Person extractPersonFrom(String text);
}
@Test
void test08() {
    PersonExtractor personExtractor = AiServices.create(PersonExtractor.class, BASE_MODEL);

    String text = """
            John is 42 years old and lives an independent life.
            He stands 1.75 meters tall and carries himself with confidence.
            Currently unmarried, he enjoys the freedom to focus on his personal goals and interests.
            """;

    Person person = personExtractor.extractPersonFrom(text);

    System.out.println(person); // Person[name=John, age=42, height=1.75, married=false]
}

属性的必填和可选, 使用 @JsonProperty(required = true)

java 复制代码
class Address {
    @Description("街道地址")
    @JsonProperty(required = true)
    public String street;
}
Polymorphic Types 多态类型
java 复制代码
sealed interface Animal permits Dog, Cat {}

record Dog(String name, String breed) implements Animal {}

record Cat(String name, boolean indoor) implements Animal {}

interface AnimalExtractor {

    Animal extractAnimalFrom(String text);
}
相关推荐
leonidZhao1 小时前
Java25新特性:加密对象的PEM编码
java
计算机安禾1 小时前
【c++面向对象编程】第21篇:运算符重载基础:语法、规则与不可重载的运算符
java·前端·c++
萧曵 丶1 小时前
JUC 实际业务高频面试题浅谈
java·juc·aqs·lock
初圣魔门首席弟子1 小时前
bug 2026.05.15(以前能运行的java springboot项目突然间不能运行后台数据了)
java·开发语言·bug
古怪今人1 小时前
项目和模块 一个目录下创建多个项目 IDEA Multi-Project Workspace插件
java·ide·intellij-idea
小英雄大肚腩丶2 小时前
RabbitMQ消息队列
java·数据结构·spring boot·分布式·rabbitmq·java-rabbitmq
fengxin_rou2 小时前
用户模块架构实战:DTO 与 Domain 分层、Optional 空值处理、事务只读优化详解
java·后端·架构·用户实战
Heartache Doctor2 小时前
[论文阅读笔记] A Survey on Multimodal Large Language Models
论文阅读·笔记·语言模型
redaijufeng3 小时前
C++构造函数详解:从基础原理到实际应用
java·jvm·c++