mistral_tools.tool_register

Registering and binding commands (tools)

The classes that follow are intended to automate usage of the Mistral tool/function calling api (see https://docs.mistral.ai/capabilities/function_calling/)

It includes

  • Automatic generation of the json representation of the tools

  • Binding of parameters (to handle local state.

    For example in this codebase, the git repository is passed as a bound parameter)

Classes

BoundCommandRegister(command_register, ...)

A command register with bound parameters.

Command(name, function, parameters, ...)

A command that can be called by the LLM

CommandRegister(bindable_parameters)

The main class for registering commands / tools that can be called by the LLM

Parameter(name, type, description, optional)

A parameter of a command

ParameterError(message)

Returned by the tool calls if a parameter is incorrect

ReturnableError(error_type, message)

An error message that can be returned by a function

class mistral_tools.tool_register.ReturnableError(error_type: str, message: str)[source]

An error message that can be returned by a function

and converted to a string (to be sent to the LLM)

class mistral_tools.tool_register.ParameterError(message: str)[source]

Returned by the tool calls if a parameter is incorrect

class mistral_tools.tool_register.Parameter(name: str, type: type, description: str, optional: bool)[source]

A parameter of a command

to_json()[source]

Convert the parameter to Mistral api json representation

class mistral_tools.tool_register.Command(name: str, function: Callable, parameters: dict[str, Parameter], bindable_parameters: set[str], description: str)[source]

A command that can be called by the LLM

parameters_to_json()[source]

Convert the parameters to Mistral api json representation

to_json()[source]

Convert the command to Mistral api json representation

class mistral_tools.tool_register.CommandRegister(bindable_parameters)[source]

The main class for registering commands / tools that can be called by the LLM

To add a command, use the register decorator.

To generate the json representation of the commands, use the to_json method. It will ignore the bindable parameters.

Parameters:

bindable_parameters (set[str]) – The parameters that can be bound to a value locally (as opposed to being passed by the LLM)

register(description='', parameter_descriptions=None)[source]

Decorator to register a command

All parameters of the function should be decorated with a type among str, int, float, bool (except for the bindable parameters). This function will use the type annotations to generate the json representation of the command.

Parameters:
  • description (str, optional) – The description of the command. Defaults to “”.

  • parameter_descriptions (dict[str, str], optional) – The descriptions of

  • command (the parameters. Defaults to "" for every)

:param : :param but you should really change that.:

static parameter_of_inspected(p: Parameter, descriptions: dict[str, str]) Parameter[source]

Generate a parameter from inspecting a functions’s parameters

to_json() ToolTypedDict[source]

Generate the json representation of the commands.

This can be passed directly as the tools parameters to Mistral.chat.complete()

bind(**bound_parameters)[source]

Bind the parameters to the commands.

Parameters:

**bound_parameters – The parameters to bind

Returns:

A bound version of the command register.

Return type:

BoundCommandRegister

class mistral_tools.tool_register.BoundCommandRegister(command_register: CommandRegister, bound_parameters: dict[str, object])[source]

A command register with bound parameters.

This object behaves like the BoundCommandRegister, except bound commands can be accessed with the getitem operator like so:

commands = CommandRegister(bindable_parameters=("df",))

@commands.register(
        description="Get payment status of a transaction",
        parameter_descriptions={"transaction_id": "The transaction id.",})
def retrieve_payment_status(*, df: data, transaction_id: str) -> str:
    ...

bound_commands = commands.bind(df=df)

bound_commands["retrieve_payment_status"](transaction_id="1234")
bind_command(command: Command)[source]

Returns the bound version of the command

Returns the underlying function of the command, with - the bound parameters already filled in - ensuring that the returnable errors are changed into strings

In general, this is called by the :func:__getitem__ method, you should not have to call it directly.

bind_parameters(bindable_parameters, kwargs)[source]

Bind the parameters to the command, and return the bound parameters

check_parameters(command: Command, kwargs)[source]

Check that the parameters are correct for the command

to_json()[source]

Generate the json representation of the commands.

This command simply calls the to_json method of the underlying command register.

handle_returnable_error(error: ReturnableError)[source]

Convert a returnable error to a string

This can be overriden in a subclass to handle the errors differently