Skip to main content

FunctionGemmaModel

FunctionGemma model backend for Ollama with custom tool calling format. FunctionGemma is a specialized Gemma model fine-tuned for function calling. It uses a custom chat template format that differs from OpenAI’s format. This backend handles conversion between CAMEL’s OpenAI-style tool schemas and FunctionGemma’s native format. Parameters:
  • model_type (Union[ModelType, str]): Model for which a backend is created (e.g., “functiongemma”).
  • model_config_dict (Optional[Dict[str, Any]], optional): A dictionary of configuration options. If :obj:None, :obj:FunctionGemmaConfig().as_dict() will be used. (default: :obj:None)
  • api_key (Optional[str], optional): Not required for local Ollama. (default: :obj:None)
  • url (Optional[str], optional): The URL to the Ollama server. (default: :obj:http://localhost:11434)
  • token_counter (Optional[BaseTokenCounter], optional): Token counter to use for the model. If not provided, :obj:OpenAITokenCounter( ModelType.GPT_4O_MINI) will be used. (default: :obj:None)
  • timeout (Optional[float], optional): The timeout value in seconds for API calls. If not provided, will fall back to the MODEL_TIMEOUT environment variable or default to 180 seconds. (default: :obj:None)
  • max_retries (int, optional): Maximum number of retries for API calls. (default: :obj:3)

init

token_counter

Returns: BaseTokenCounter: The token counter following the model’s tokenization style.

_escape_string

Wrap string values in <escape> tags for FunctionGemma format. Parameters:
  • s (str): The string to escape.
Returns: str: The escaped string.

_unescape_string

Remove <escape> tags from string values. Parameters:
  • s (str): The string to unescape.
Returns: str: The unescaped string.

_type_to_function_gemma

Convert JSON schema type to FunctionGemma type (uppercase). Parameters:
  • json_type (Union[str, List[str]]): The JSON schema type. Can be a string like “string” or a list like [“string”, “null”] for optional parameters.
Returns: str: The FunctionGemma type.

_format_parameter_properties

Format parameter properties for FunctionGemma declaration. Parameters:
  • properties (Dict[str, Any]): The properties dictionary.
  • required (List[str]): List of required parameter names.
Returns: str: Formatted properties string.

_convert_tool_to_function_gemma

Convert OpenAI tool schema to FunctionGemma declaration format. Parameters:
  • tool (Dict[str, Any]): The OpenAI tool schema.
Returns: str: The FunctionGemma declaration string.

_format_developer_turn

Format the developer/system turn with function declarations. Parameters:
  • content (str): The system message content.
  • tools (Optional[List[Dict[str, Any]]]): List of tool schemas.
Returns: str: Formatted developer turn.

_format_user_turn

Format a user message turn. Parameters:
  • content (str): The user message content.
Returns: str: Formatted user turn.

_format_model_turn

Format an assistant/model message turn. Parameters:
  • message (OpenAIMessage): The assistant message.
Returns: str: Formatted model turn.

_format_tool_response

Format a tool response message. Parameters:
  • message (OpenAIMessage): The tool response message.
Returns: str: Formatted tool response.

_format_messages

Format all messages into a FunctionGemma prompt string. Parameters:
  • messages (List[OpenAIMessage]): List of messages in OpenAI format.
  • tools (Optional[List[Dict[str, Any]]]): List of tool schemas.
Returns: str: Complete formatted prompt.

_extract_function_calls

Extract function calls from model output. Parameters:
  • text (str): The model output text.
  • tools (Optional[List[Dict[str, Any]]]): Available tools to infer function names when the model outputs malformed calls.
Returns: Tuple[str, List[Dict[str, Any]]]: Tuple of (remaining_content, list_of_tool_calls).

_infer_function_name

Infer the function name from available tools. Parameters:
  • args_str (str): The arguments string from the model output.
  • tools (Optional[List[Dict[str, Any]]]): Available tools.
Returns: Optional[str]: The inferred function name, or None if not found.

_parse_function_args

Parse function arguments from FunctionGemma format. Parameters:
  • args_str (str): The arguments string (e.g., “a:15,b:27”).
Returns: Dict[str, Any]: Parsed arguments dictionary.

_parse_value

Parse a value string to appropriate Python type. Parameters:
  • value (str): The value string.
Returns: Any: Parsed value (int, float, bool, or str).

_to_chat_completion

Convert parsed response to OpenAI ChatCompletion format. Parameters:
  • response_text (str): The model response text.
  • model (str): The model name.
  • tools (Optional[List[Dict[str, Any]]]): Available tools for function name inference.
Returns: ChatCompletion: OpenAI-compatible ChatCompletion object.

_call_ollama_generate

Call Ollama’s /api/generate endpoint with raw prompt. Parameters:
  • prompt (str): The formatted prompt string.
Returns: str: The model response text.

_run

Run inference using FunctionGemma via Ollama. Parameters:
  • messages (List[OpenAIMessage]): Message list with the chat history in OpenAI API format.
  • response_format (Optional[Type[BaseModel]]): Not supported for FunctionGemma. (default: :obj:None)
  • tools (Optional[List[Dict[str, Any]]]): The schema of the tools to use for the request.
Returns: ChatCompletion: The model response in OpenAI ChatCompletion format.

stream

Returns: bool: Always False for FunctionGemma.