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);
}
相关推荐
dianziyao_14 分钟前
第 4.1 篇:让 Codex 理解你的双链——AI 辅助发现笔记间的关联
人工智能·笔记
小羊没烦恼!26 分钟前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
小荷才露尖尖角,早有蜻蜓立上头2 小时前
过滤器的4种创建方式
java
大圣编蚕2 小时前
Java ByteArrayInputStream 详解:从入门到实战
java·开发语言·算法
程序员阿明2 小时前
idea把插件啥的不默认放在C盘
java·ide·intellij-idea
linweidong3 小时前
中科闻歌Java面试题及参考答案
java·python·大模型·提示词·ai agent·mcp·rag原理
OpenAnolis小助手3 小时前
一句话看透 JVM,SysOM 诊断 Skill 新增 Java 应用诊断能力
java·开发语言·jvm·阿里云·操作系统控制台·sysom
ForteScarlet3 小时前
Kotlin 2.4.20 现已发布,新特性多不多?
android·java·开发语言·开源·kotlin·jetbrains
萧瑟余晖3 小时前
Java深入解析篇六十二之安全机制详解
java·开发语言
xian_wwq3 小时前
【学习笔记】OWASP Agentic 应用安全(二)
笔记·学习·安全·owasp