当前位置: 首页 > news >正文

spring-ai-alibaba第三章ollama集成Tool

1、当我们使用大模型问

请告诉我现在北京时间几点了

回答如下

此时就需要大模型的 function call 的功能,也就是 给大模型加上工具

2、代码如下

获取时间的工具类

package com.alibaba.cloud.ai.example.chat.ollama.tools.time;

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

/**
 * @author yingzi
 * @date 2025/3/27:10:56
 */
public class TimeUtils {

    public static String getTimeByZoneId(String zoneId) {

        // Get the time zone using ZoneId
        ZoneId zid = ZoneId.of(zoneId);

        // Get the current time in this time zone
        ZonedDateTime zonedDateTime = ZonedDateTime.now(zid);

        // Defining a formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

        // Format ZonedDateTime as a string
        String formattedDateTime = zonedDateTime.format(formatter);

        return formattedDateTime;
    }
}

获取时间的工具

package com.alibaba.cloud.ai.example.chat.ollama.tools.time.method;

import com.alibaba.cloud.ai.example.chat.ollama.tools.time.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

public class TimeTools {
    private static final Logger logger = LoggerFactory.getLogger(TimeTools.class);

    @Tool(description = "Get the time of a specified city.")
    public String  getCityTimeMethod(@ToolParam(description = "Time zone id, such as Asia/Shanghai") String timeZoneId) {
        logger.info("The current time zone is {}", timeZoneId);
        return String.format("The current time zone is %s and the current time is " + "%s", timeZoneId,
                TimeUtils.getTimeByZoneId(timeZoneId));
    }
}

controller代码如下

package com.alibaba.cloud.ai.example.chat.ollama.controller;

import com.alibaba.cloud.ai.example.chat.ollama.tools.time.method.TimeTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/time")
public class TimeController {

    private final ChatClient ollamaiChatClient;

    public TimeController(ChatClient.Builder chatClientBuilder) {
        this.ollamaiChatClient = chatClientBuilder.build();
    }

    /**
     * 无工具版
     */
    @GetMapping("/chat")
    public String simpleChat(@RequestParam(value = "query", defaultValue = "请告诉我现在北京时间几点了") String query) {
        return ollamaiChatClient.prompt(query).call().content();
    }

    /**
     * 调用工具版 - function
     */
    @GetMapping("/chat-tool-function")
    public String chatTranslateFunction(@RequestParam(value = "query", defaultValue = "请告诉我现在北京时间几点了") String query) {
        return ollamaiChatClient.prompt(query).tools("getCityTimeFunction").call().content();
    }

    /**
     * 调用工具版 - method
     */
    @GetMapping("/chat-tool-method")
    public String chatTranslateMethod(@RequestParam(value = "query", defaultValue = "请告诉我现在北京时间几点了") String query) {
        return ollamaiChatClient.prompt(query).tools(new TimeTools()).call().content();
    }

}

调用接口  http://127.0.0.1:10005/time/chat-tool-method  如下

http://www.dtcms.com/a/106320.html

相关文章:

  • 【redis】缓存 更新策略(定期、实时生存),缓存预热、穿透、雪崩、击穿详解
  • ERP管理系统:Java+Vue,含源码及文档,一体化管理资源,优化流程,强化企业运营效率
  • React.memo()和 useMemo()的用法是什么,有哪些区别
  • CSS3学习教程,从入门到精通,CSS3 媒体查询实现响应式布局语法指南(21)
  • Unity插件SuperScrollView详解(基础篇)
  • Redis安全与配置问题——AOF文件损坏问题及解决方案
  • 机器人--ros2--IMU
  • 用jQuery和Canvas打造2D版“我的世界+超级玛丽“游戏
  • 知名界面控件DevExpress v24.2.6全新可用|发布重要更改
  • 安卓一些接口使用
  • dbgpt7.0 docker部署
  • Unity工具—默认取消Image和RowImage的Raycast Target
  • Vue3入门
  • 智谱大模型(ChatGLM3)PyCharm的调试指南
  • AI大模型从0到1记录学习 day11
  • 三步构建企业级操作日志系统:Spring AOP + 自定义注解的优雅实践
  • Redis的一些高级指令
  • HBase安装与配置——单机版到完全分布式部署
  • 【蓝桥杯】回文字符串
  • 自己用python写的查询任意网络设备IP地址工具使用实测
  • 什么是 继续预训练、SFT(监督微调)和RLHF
  • 【Java/数据结构】Map与Set(图文版)
  • AllData数据中台商业版发布版本1.2.9相关白皮书发布
  • UML 4+1 视图:搭建软件架构的 “万能拼图”
  • zabbix“专家坐诊”第281期问答
  • Logstash开启定时任务增量同步mysql数据到es的时区问题
  • 淘宝搜索关键字与商品数据采集接口技术指南
  • 软考 中级软件设计师 考点知识点笔记总结 day09 操作系统进程管理
  • 自然语言处理(24:(第六章4.)​seq2seq模型的应用)
  • 卸载360壁纸