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);
}
相关推荐
数智化管理手记1 小时前
海量数据如何沉淀有效数据资产?数据标准化治理方案如何搭建?
java·大数据·人工智能
cfm_29141 小时前
SpringAI + Ollama 本地大模型
java·开发语言·人工智能·语言模型
青山是哪个青山1 小时前
LangChain 学习笔记(三):LangSmith 调试与监控
笔记·学习·langchain
鲸采云SRM采购管理系统2 小时前
采购管理系统哪家好?2026最新测评对比
java·服务器·数据库
0x533 小时前
Java网络编程(二)
java·服务器·网络
蒸蒸yyyyzwd3 小时前
cpp选手备战后端学习笔记day6 秋招笔试题
笔记·学习
Flittly4 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
Poo_Chai4 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
瑞码空间4 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
树码小子4 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea