sb-deepseek-ChatClient --Function Call 函数调用(自定义)20250713
1、
package com.ds.aifunction.config;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;
@Component
public class CalculatorTool {
@Tool(description = "用于计算两个数字的和")
public double addTool(double num1, double num2) {
return num1 + num2;
}
@Tool(description = "用于计算两个数字的乘积")
public double multiplyTool(double num1, double num2) {
return num1 * num2;
}
}
2、FunctionController
package com.ds.aifunction.controller;
import com.ds.aifunction.config.CalculatorTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FunctionController {
@Autowired
private ChatModel chatModel;
@Autowired
private CalculatorTool calculatorTool;
@GetMapping(value = "/function",produces = MediaType.APPLICATION_STREAM_JSON_VALUE) // http://localhost:8080/function 访问
public String function(@RequestParam("userMessage") String userMessage) {
return ChatClient.builder(chatModel)
.build().prompt()
.system("""
您是算术计算器的代理。
您能够支持加法运算、乘法运算等操作,其余功能将在后续版本中添加,如果用户的问题不支持请告知详情。
在提供加法运算和乘法运算等操作之前,您必须从用户下获取如下信息:两个数字,运算类型。
请调用自定义函数执行加法和乘法运算。
请讲中文。
""")
.user(userMessage)
.tools(calculatorTool)
.call()
.content();
}
}
3、