Object call(Struct args) {
      // Extract the arguments from the Struct
      Map<String, Value> argsMap = args.getFieldsMap();
      List<Object> argsList = new ArrayList<>();
      for (int i = 0; i < orderedParameterNames.size(); i++) {
        String parameterName = orderedParameterNames.get(i);
        if (!argsMap.containsKey(parameterName)) {
          throw new IllegalArgumentException(
              "The parameter \""
                  + parameterName
                  + "\" was not found in the arguments requested by the model. Args map: "
                  + argsMap);
        }
        Value value = argsMap.get(parameterName);
        switch (value.getKindCase()) {
          case NUMBER_VALUE:
            // Args map only returns double values, but the function may expect other types(int,
            // float). So we need to cast the value to the correct type.
            Class<?> parameterType = callableFunction.getParameters()[i].getType();
            if (parameterType.equals(int.class)) {
              argsList.add((int) value.getNumberValue());
            } else if (parameterType.equals(float.class)) {
              argsList.add((float) value.getNumberValue());
            } else {
              argsList.add(value.getNumberValue());
            }
            break;
          case STRING_VALUE:
            argsList.add(value.getStringValue());
            break;
          case BOOL_VALUE:
            argsList.add(value.getBoolValue());
            break;
          case NULL_VALUE:
            argsList.add(null);
            break;
          default:
            throw new IllegalArgumentException(
                "Unsupported value type "
                    + value.getKindCase()
                    + " for parameter "
                    + parameterName);
        }
      }

      // Invoke the function
      logger.info(
          "Automatically calling function: "
              + callableFunction.getName()
              + argsList.toString().replace('[', '(').replace(']', ')'));
      try {
        return callableFunction.invoke(null, argsList.toArray());
      } catch (Exception e) {
        throw new IllegalStateException(
            "Error raised when calling function \""
                + callableFunction.getName()
                + "\" as requested by the model. ",
            e);
      }
    }
Calls the callable function with the given arguments.

@param args the arguments to pass to the function
@return the result of the function call
@throws IllegalStateException if there are errors when invoking the function
@throws IllegalArgumentException if the args map doesn't contain all the parameters of the
    function or the value types in the args map are not supported
java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/AutomaticFunctionCallingResponder.java