Spring-AI系列——Tool Calling获取当前时间

文章目录


一、调用流程

二、代码

tool包下的TimeTools.java类

java 复制代码
package org.example.tool;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class TimeTools {

    @Tool(description = "通过时间id获取当前时间")
    public String getTimeByZoneId(@ToolParam(description = "时区id,比如 Asia/Shanghai") String zoneId) {
        ZoneId zid = ZoneId.of(zoneId);
        ZonedDateTime zoneDateTime = ZonedDateTime.now(zid);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
        return zoneDateTime.format(formatter);
    }
}

controller.ZhipuChatClientController.java

java 复制代码
@RestController
@RequestMapping("/chatClient")
public class ZhipuChatClientController {

    private final ChatClient chatClient;

    public ZhipuChatClientController(ChatClient.Builder builder) {
        this.chatClient = builder
                .defaultTools(new TimeTools())
                .build();
    }



    @GetMapping("/simple")
    public String simple(@RequestParam(name = "query") String query) {
        ZhiPuAiChatOptions chatOptions = new ZhiPuAiChatOptions();
        chatOptions.setModel("glm-4-flash");
        chatOptions.setTemperature(0.0);
        chatOptions.setMaxTokens(15536);
        return chatClient.prompt()
                .system("你是一个有用的AI助手")
                .user(query)
                .options(chatOptions)
                .call().content();
    }
 }

三、效果

加了Tool Calling前

加了Tool Calling后

四、底层调用情况

Spring AI会在发送给大模型的请求体中添加所有tool的工具信息

大模型返回给Spring AI的信息中,指出调用具体的工具名称及参数

SpringAI调用工具后返回的结果

相关推荐
-西门吹雪1 天前
c++线程之std::async浅析
java·jvm·c++
a努力。1 天前
国家电网Java面试被问:最小生成树的Kruskal和Prim算法
java·后端·算法·postgresql·面试·linq
朝九晚五ฺ1 天前
从零到实战:鲲鹏平台 HPC 技术栈与并行计算
java·开发语言
CUIYD_19891 天前
Freemarker 无法转译 & 字符
java·开发语言·spring
自在极意功。1 天前
简单介绍SpringMVC
java·mvc·springmvc·三层架构
superman超哥1 天前
Rust Vec的内存布局与扩容策略:动态数组的高效实现
开发语言·后端·rust·动态数组·内存布局·rust vec·扩容策略
Yuiiii__1 天前
一次并不简单的 Spring 循环依赖排查
java·开发语言·数据库
tkevinjd1 天前
JUC4(生产者-消费者)
java·多线程·juc
野槐1 天前
java基础-面向对象
java·开发语言
sww_10261 天前
Openfeign源码浅析
java·spring cloud