base_action

Module Contents

Classes

ToolMeta

Metaclass of tools

BaseAction

Base class for all actions.

Functions

tool_api([func, explode_return, returns_named_value])

Turn functions into tools. It will parse typehints as well as docstrings

Attributes

TOOL_REGISTRY

lagent.actions.base_action.TOOL_REGISTRY
lagent.actions.base_action.tool_api(func=None, *, explode_return=False, returns_named_value=False, **kwargs)

Turn functions into tools. It will parse typehints as well as docstrings to build the tool description and attach it to functions via an attribute api_description.

Examples

# typehints has higher priority than docstrings
from typing import Annotated

@tool_api
def add(a: Annotated[int, 'augend'], b: Annotated[int, 'addend'] = 1):
    '''Add operation

    Args:
        x (int): a
        y (int): b
    '''
    return a + b

print(add.api_description)
Parameters:
  • func (Optional[Callable]) – function to decorate. Defaults to None.

  • explode_return (bool) –

    whether to flatten the dictionary or tuple return as the return_data field. When enabled, it is recommended to annotate the member in docstrings. Defaults to False.

    @tool_api(explode_return=True)
    def foo(a, b):
        '''A simple function
    
        Args:
            a (int): a
            b (int): b
    
        Returns:
            dict: information of inputs
                * x: value of a
                * y: value of b
        '''
        return {'x': a, 'y': b}
    
    print(foo.api_description)
    

  • returns_named_value (bool) – whether to parse thing: Description in returns sections as a name and description, rather than a type and description. When true, type must be wrapped in parentheses: (int): Description. When false, parentheses are optional but the items cannot be named: int: Description. Defaults to False.

Returns:

wrapped function or partial decorator

Return type:

Callable

Important

return_data field will be added to api_description only when explode_return or returns_named_value is enabled.

class lagent.actions.base_action.ToolMeta

Bases: abc.ABCMeta

Metaclass of tools

class lagent.actions.base_action.BaseAction(description=None, parser=JsonParser, enable=True)

Base class for all actions.

Parameters:
  • description (Optional[dict]) – The description of the action. Defaults to None.

  • parser (Type[BaseParser]) – The parser class to process the action’s inputs and outputs. Defaults to JsonParser.

  • enable (bool) – Whether the action is enabled. Defaults to True.

Examples

  • simple tool

class Bold(BaseAction):
    '''Make text bold'''

    def run(self, text: str):
        '''
        Args:
            text (str): input text

        Returns:
            str: bold text
        '''
        return '**' + text + '**'

action = Bold()
  • toolkit with multiple APIs

class Calculator(BaseAction):
    '''Calculator'''

    @tool_api
    def add(self, a, b):
        '''Add operation

        Args:
            a (int): augend
            b (int): addend

        Returns:
            int: sum
        '''
        return a + b

    @tool_api
    def sub(self, a, b):
        '''Subtraction operation

        Args:
            a (int): minuend
            b (int): subtrahend

        Returns:
            int: difference
        '''
        return a - b

action = Calculator()
property name
property enable
property is_toolkit
property description: dict

Description of the tool

Return type:

dict