registerTool method

void registerTool(
  1. GroqToolItem tool
)

Registers a tool to the chat, which always can be called by the model. tool the tool to register Example:

final weatherTool = GroqToolItem(
  functionName: 'get_weather',
  functionDescription: 'Get weather information for a specified location',
  parameters: [
    GroqToolParameter(
      parameterName: 'location',
      parameterDescription: 'City or location name',
      parameterType: GroqToolParameterType.string,
      isRequired: true,
    ),
    GroqToolParameter(
      parameterName: 'units',
      parameterDescription: 'Temperature units (metric or imperial)',
      parameterType: GroqToolParameterType.string,
      isRequired: false,
      allowedValues: ['metric', 'imperial'],
    ),
  ],
  function: (args) {
    final location = args['location'] as String;
    final units = args['units'] as String? ?? 'metric';
    return {
      'location': location,
      'temperature': units == 'metric' ? 22 : 71.6,
      'units': units,
    };
  },
);
chat.registerTool(weatherTool);

Implementation

void registerTool(GroqToolItem tool) {
  //assert that the tool is not already registered
  assert(
      !_registeredTools
          .any((element) => element.functionName == tool.functionName),
      'Tool with the name ${tool.functionName} is already registered');
  _registeredTools.add(tool);
}