in java/src/main/java/com/example/gemini/FunctionCalling.java [36:142]
public static Double functionCalling() {
// [START function_calling]
Client client = new Client();
FunctionDeclaration addFunction =
FunctionDeclaration.builder()
.name("addNumbers")
.parameters(
Schema.builder()
.type("object")
.properties(Map.of(
"firstParam", Schema.builder().type("number").description("First number").build(),
"secondParam", Schema.builder().type("number").description("Second number").build()))
.required(Arrays.asList("firstParam", "secondParam"))
.build())
.build();
FunctionDeclaration subtractFunction =
FunctionDeclaration.builder()
.name("subtractNumbers")
.parameters(
Schema.builder()
.type("object")
.properties(Map.of(
"firstParam", Schema.builder().type("number").description("First number").build(),
"secondParam", Schema.builder().type("number").description("Second number").build()))
.required(Arrays.asList("firstParam", "secondParam"))
.build())
.build();
FunctionDeclaration multiplyFunction =
FunctionDeclaration.builder()
.name("multiplyNumbers")
.parameters(
Schema.builder()
.type("object")
.properties(Map.of(
"firstParam", Schema.builder().type("number").description("First number").build(),
"secondParam", Schema.builder().type("number").description("Second number").build()))
.required(Arrays.asList("firstParam", "secondParam"))
.build())
.build();
FunctionDeclaration divideFunction =
FunctionDeclaration.builder()
.name("divideNumbers")
.parameters(
Schema.builder()
.type("object")
.properties(Map.of(
"firstParam", Schema.builder().type("number").description("First number").build(),
"secondParam", Schema.builder().type("number").description("Second number").build()))
.required(Arrays.asList("firstParam", "secondParam"))
.build())
.build();
GenerateContentConfig config = GenerateContentConfig.builder()
.toolConfig(ToolConfig.builder().functionCallingConfig(
FunctionCallingConfig.builder().mode("ANY").build()
).build())
.tools(
Collections.singletonList(
Tool.builder().functionDeclarations(
Arrays.asList(
addFunction,
subtractFunction,
divideFunction,
multiplyFunction
)
).build()
)
)
.build();
GenerateContentResponse response =
client.models.generateContent(
"gemini-2.0-flash",
"I have 57 cats, each owns 44 mittens, how many mittens is that in total?",
config);
if (response.functionCalls() == null || response.functionCalls().isEmpty()) {
System.err.println("No function call received");
return null;
}
var functionCall = response.functionCalls().getFirst();
String functionName = functionCall.name().get();
var arguments = functionCall.args();
Map<String, BiFunction<Double, Double, Double>> functionMapping = new HashMap<>();
functionMapping.put("addNumbers", (a, b) -> a + b);
functionMapping.put("subtractNumbers", (a, b) -> a - b);
functionMapping.put("multiplyNumbers", (a, b) -> a * b);
functionMapping.put("divideNumbers", (a, b) -> b != 0 ? a / b : Double.NaN);
BiFunction<Double, Double, Double> function = functionMapping.get(functionName);
Number firstParam = (Number) arguments.get().get("firstParam");
Number secondParam = (Number) arguments.get().get("secondParam");
Double result = function.apply(firstParam.doubleValue(), secondParam.doubleValue());
System.out.println(result);
// [END function_calling]
return result;
}