base_action
Module Contents
Classes
Metaclass of tools |
|
Base class for all actions. |
Functions
|
Turn functions into tools. It will parse typehints as well as docstrings |
Attributes
- 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_datafield. When enabled, it is recommended to annotate the member in docstrings. Defaults toFalse.@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: Descriptionin 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 toFalse.
- Returns:
wrapped function or partial decorator
- Return type:
Callable
Important
return_datafield will be added toapi_descriptiononly whenexplode_returnorreturns_named_valueis enabled.
- class lagent.actions.base_action.ToolMeta
Bases:
abc.ABCMetaMetaclass 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 toNone.parser (
Type[BaseParser]) – The parser class to process the action’s inputs and outputs. Defaults toJsonParser.enable (
bool) – Whether the action is enabled. Defaults toTrue.
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