对于springAi的tools功能,我还是那句话,真香。他把我很繁琐的接口调用直接很优雅的实现了
查询天气例子
@RestController
public class ToolController {
@Resource(name = "QWenChatClient")
private ChatClient qwenChatClient;
@GetMapping(value = "/tool", produces = "text/stream;charset=UTF-8")
public Flux<String> tool_1(@RequestParam("question")String question){
return qwenChatClient
.prompt()
.user(question).stream().content();
}}@Configuration
public class ChatClientConfig {
@Resource
private ToolService toolService;
@Bean(name = "QWenChatClient")
public ChatClient qwenChatClient(DashScopeChatModel dashScopeChatModel) {
DashScopeChatOptions dashScopeChatOptions = new DashScopeChatOptions();
dashScopeChatOptions.setModel("qwen-max");
return ChatClient
.builder(dashScopeChatModel)
.defaultOptions(dashScopeChatOptions)
.defaultTools(toolService)
.build();
}}@Service
public class ToolService {
private static final String data = "{\"message\":\"success感谢又拍云(upyun.com)提供CDN赞助\",\"status\":200,\"date\":\"20250908\",\"time\":\"2025-09-08 22:16:48\",\"cityInfo\":{\"city\":\"天津市\",\"citykey\":\"101030100\",\"parent\":\"天津\",\"updateTime\":\"20:07\"},\"data\":{\"shidu\":\"59%\",\"pm25\":14,\"pm10\":43,\"quality\":\"优\",\"wendu\":\"27.9\",\"ganmao\":\"各类人群可自由活动\",\"forecast\":[{\"date\":\"08\",\"high\":\"高温 29℃\",\"low\":\"低温 21℃\",\"ymd\":\"2025-09-08\",\"week\":\"星期一\",\"sunrise\":\"05:46\",\"sunset\":\"18:31\",\"aqi\":51,\"fx\":\"东南风\",\"fl\":\"2级\",\"type\":\"多云\",\"notice\":\"阴晴之间,谨防紫外线侵扰\"},{\"date\":\"09\",\"high\":\"高温 30℃\",\"low\":\"低温 21℃\",\"ymd\":\"2025-09-09\",\"week\":\"星期二\",\"sunrise\":\"05:47\",\"sunset\":\"18:30\",\"aqi\":56,\"fx\":\"西南风\",\"fl\":\"3级\",\"type\":\"晴\",\"notice\":\"愿你拥有比阳光明媚的心情\"},{\"date\":\"10\",\"high\":\"高温 30℃\",\"low\":\"低温 21℃\",\"ymd\":\"2025-09-10\",\"week\":\"星期三\",\"sunrise\":\"05:47\",\"sunset\":\"18:28\",\"aqi\":61,\"fx\":\"南风\",\"fl\":\"3级\",\"type\":\"晴\",\"notice\":\"愿你拥有比阳光明媚的心情\"},{\"date\":\"11\",\"high\":\"高温 28℃\",\"low\":\"低温 21℃\",\"ymd\":\"2025-09-11\",\"week\":\"星期四\",\"sunrise\":\"05:48\",\"sunset\":\"18:26\",\"aqi\":54,\"fx\":\"西南风\",\"fl\":\"2级\",\"type\":\"阴\",\"notice\":\"不要被阴云遮挡住好心情\"},{\"date\":\"12\",\"high\":\"高温 26℃\",\"low\":\"低温 21℃\",\"ymd\":\"2025-09-12\",\"week\":\"星期五\",\"sunrise\":\"05:49\",\"sunset\":\"18:25\",\"aqi\":53,\"fx\":\"西南风\",\"fl\":\"2级\",\"type\":\"多云\",\"notice\":\"阴晴之间,谨防紫外线侵扰\"},{\"date\":\"13\",\"high\":\"高温 28℃\",\"low\":\"低温 22℃\",\"ymd\":\"2025-09-13\",\"week\":\"星期六\",\"sunrise\":\"05:50\",\"sunset\":\"18:23\",\"aqi\":54,\"fx\":\"西南风\",\"fl\":\"2级\",\"type\":\"多云\",\"notice\":\"阴晴之间,谨防紫外线侵扰\"},{\"date\":\"14\",\"high\":\"高温 30℃\",\"low\":\"低温 22℃\",\"ymd\":\"2025-09-14\",\"week\":\"星期日\",\"sunrise\":\"05:51\",\"sunset\":\"18:21\",\"aqi\":44,\"fx\":\"西南风\",\"fl\":\"2级\",\"type\":\"阴\",\"notice\":\"不要被阴云遮挡住好心情\"},{\"date\":\"15\",\"high\":\"高温 28℃\",\"low\":\"低温 21℃\",\"ymd\":\"2025-09-15\",\"week\":\"星期一\",\"sunrise\":\"05:52\",\"sunset\":\"18:20\",\"aqi\":46,\"fx\":\"南风\",\"fl\":\"2级\",\"type\":\"小雨\",\"notice\":\"雨虽小,注意保暖别感冒\"},{\"date\":\"16\",\"high\":\"高温 24℃\",\"low\":\"低温 19℃\",\"ymd\":\"2025-09-16\",\"week\":\"星期二\",\"sunrise\":\"05:53\",\"sunset\":\"18:18\",\"aqi\":38,\"fx\":\"东北风\",\"fl\":\"2级\",\"type\":\"小雨\",\"notice\":\"雨虽小,注意保暖别感冒\"},{\"date\":\"17\",\"high\":\"高温 19℃\",\"low\":\"低温 16℃\",\"ymd\":\"2025-09-17\",\"week\":\"星期三\",\"sunrise\":\"05:54\",\"sunset\":\"18:17\",\"aqi\":25,\"fx\":\"东北风\",\"fl\":\"2级\",\"type\":\"小雨\",\"notice\":\"雨虽小,注意保暖别感冒\"},{\"date\":\"18\",\"high\":\"高温 21℃\",\"low\":\"低温 16℃\",\"ymd\":\"2025-09-18\",\"week\":\"星期四\",\"sunrise\":\"05:55\",\"sunset\":\"18:15\",\"aqi\":20,\"fx\":\"西北风\",\"fl\":\"2级\",\"type\":\"阴\",\"notice\":\"不要被阴云遮挡住好心情\"},{\"date\":\"19\",\"high\":\"高温 21℃\",\"low\":\"低温 16℃\",\"ymd\":\"2025-09-19\",\"week\":\"星期五\",\"sunrise\":\"05:56\",\"sunset\":\"18:13\",\"aqi\":27,\"fx\":\"西北风\",\"fl\":\"2级\",\"type\":\"阴\",\"notice\":\"不要被阴云遮挡住好心情\"},{\"date\":\"20\",\"high\":\"高温 27℃\",\"low\":\"低温 17℃\",\"ymd\":\"2025-09-20\",\"week\":\"星期六\",\"sunrise\":\"05:56\",\"sunset\":\"18:12\",\"aqi\":40,\"fx\":\"西南风\",\"fl\":\"2级\",\"type\":\"阴\",\"notice\":\"不要被阴云遮挡住好心情\"},{\"date\":\"21\",\"high\":\"高温 25℃\",\"low\":\"低温 17℃\",\"ymd\":\"2025-09-21\",\"week\":\"星期日\",\"sunrise\":\"05:57\",\"sunset\":\"18:10\",\"aqi\":46,\"fx\":\"东风\",\"fl\":\"2级\",\"type\":\"多云\",\"notice\":\"阴晴之间,谨防紫外线侵扰\"},{\"date\":\"22\",\"high\":\"高温 29℃\",\"low\":\"低温 16℃\",\"ymd\":\"2025-09-22\",\"week\":\"星期一\",\"sunrise\":\"05:58\",\"sunset\":\"18:09\",\"aqi\":48,\"fx\":\"西北风\",\"fl\":\"1级\",\"type\":\"小雨\",\"notice\":\"雨虽小,注意保暖别感冒\"}],\"yesterday\":{\"date\":\"07\",\"high\":\"高温 30℃\",\"low\":\"低温 20℃\",\"ymd\":\"2025-09-07\",\"week\":\"星期日\",\"sunrise\":\"05:45\",\"sunset\":\"18:33\",\"aqi\":54,\"fx\":\"西南风\",\"fl\":\"2级\",\"type\":\"晴\",\"notice\":\"愿你拥有比阳光明媚的心情\"}}}";
/**
* 查询天气
*/
@Tool(description = "查询天气")
public String queryQuestion(
@ToolParam(description = "地点")String location,
@ToolParam(description = "时间")String date
){
System.out.println(location);
System.out.println(date);
return data;
}
}当正常询问时返回值是正确的

当询问天气的时候

然后当我们想要返回函数调用的大模型返回的值的时候,只需要加个系统提示词,并且把tool修改一下就行
@Tool(description = "查询天气")
public String queryQuestion(
@ToolParam(description = "地点")String location,
@ToolParam(description = "时间")String date
){
JSONObject jsonObject = new JSONObject();
jsonObject.put("location",location);
jsonObject.put("date",date);
return jsonObject.toJSONString();
} @GetMapping(value = "/tool2", produces = "text/stream;charset=UTF-8")
public Flux<String> tool_2(@RequestParam("question")String question){
return qwenChatClient
.prompt()
.system("你是一个严格的函数调用回声器:当调用工具后,直接将工具返回的内容原样输出,不要添加任何额外文字、解释或格式。")
.user(question).stream().content();
}