Administrator
Administrator
发布于 2025-09-08 / 11 阅读
0
0

【四】springAi格式化返回功能

这个功能我必须吹爆,太方便了! 具体就是之前:我们调用大模型想要返回格式化的对象,必须要提示词里面规定对象json,并且还要去掉多余的任何东西,类似```json 等,最后还要配置参数jsonSchema等,太繁琐! 用springAi实现超级方便

@SpringBootTest
class ChatBotApplicationTests {
    @Resource(name = "QWenChatClient")
    private ChatClient qwenChatClient;

    public record Address(
            String name,
            String phone,
            String province,
            String city,
            String district,//区县
            String detail//详情
    ){}
    /**
     * 大模型返回结构化数据
     */
    @Test
    void contextLoads() {

        Address address = qwenChatClient
                .prompt("请从用户的信息中提炼出用户的地址信息")
                .user("张三  杭州西湖区文三路188华星科技大厦A座1203  13890455678")
                .call()
                .entity(Address.class);//主要是entity这一步
        System.out.println(address);
    }

}


//最终效果

// Address[name=张三, phone=13890455678, province=, city=杭州, district=西湖, detail=文三路188华星科技大厦A座1203]


评论