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 如下