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调用工具后返回的结果

相关推荐
代码不停43 分钟前
自动化测试(java)
java·开发语言·selenium·自动化
行百里er1 小时前
Redis 完结篇——Redis 热点问题在 Spring Boot 项目中实战
redis·后端
唠点键盘之外的2 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)
数据库·人工智能·spring
高频因子挖掘机2 小时前
只取最近 60 个交易日,却每次拉全历史:量化脚本变慢的原因与改法
后端·github·api
鬼手点金3 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
孟健3 小时前
从5000亿Token到83%字节匹配:AI逆向工程与重构的工程闭环实践
后端·架构·ai编程
paopaokaka_luck3 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
码事漫谈4 小时前
丢得静悄悄,谈一谈 Agent 的记忆是怎么没的
后端
迪飞特科技4 小时前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
考虑考虑4 小时前
@JsonInclude注解使用
spring boot·后端·spring