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);
}
相关推荐
华如锦17 分钟前
面了很多 Java转AI Agent方向,一些面试题总结
java·开发语言·人工智能·python·ai
睡不醒男孩03082320 分钟前
CLup 6.x 版本中针对StarRocks 存算一体集群的完整操作手册
java·服务器·网络·clup
自传.20 分钟前
尚硅谷 Vibe Coding|第一章 AI 编程基础理论 学习笔记
笔记·学习·尚硅谷·vibe coding
程序员黑豆1 小时前
Java中怎么实现字符串拼接呢【AI全栈开发】
java
chase。2 小时前
【学习笔记】SimpleVLA-RL:通过强化学习扩展 VLA 训练
笔记·学习
java1234_小锋2 小时前
LangChain4j 开发Java Agent智能体- 多模态支持
java·开发语言·langchain4j
艳阳天_.2 小时前
星瀚弹框页面实现
java·前端·python
AOwhisky2 小时前
Redis 学习笔记(第一期):概述、安装配置与核心理论
运维·数据库·redis·笔记·学习·云计算