Spaces:
Running
Running
File size: 3,491 Bytes
e679d69 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
from typing import Optional
from lagent.actions.base_action import BaseAction, tool_api
from lagent.actions.parser import BaseParser
from lagent.schema import ActionReturn, ActionStatusCode, ActionValidCode
class InvalidAction(BaseAction):
"""This is a invalid action class, which is used to return error message
when the action is invalid.
Args:
err_msg (str): The error message. Defaults to 'The action is invalid,
please check the action name'.
Returns:
ActionReturn: The action return.
"""
def __init__(self,
err_msg:
str = 'The action is invalid, please check the action name.',
description: Optional[dict] = None,
parser=BaseParser) -> None:
super().__init__(description, parser)
self._err_msg = err_msg
@tool_api
def run(self, err_msg: Optional[str] = None) -> ActionReturn:
"""Return the error message.
Args:
err_msg (str, optional): The error message. If err_msg is not None,
it will be returned, otherwise the default error message will
be returned. Defaults to None.
"""
action_return = ActionReturn(
url=None,
args=dict(text=err_msg),
errmsg=err_msg or self._err_msg,
type=self.name,
valid=ActionValidCode.INVALID,
state=ActionStatusCode.API_ERROR)
return action_return
class NoAction(BaseAction):
"""This is a no action class, which is used to return error message when
the response does not follow the format.
Args:
err_msg (str): The error message. Defaults to
'Please follow the format'.
"""
def __init__(self,
err_msg: str = 'Please follow the format',
description: Optional[dict] = None,
parser=BaseParser):
super().__init__(description, parser)
self._err_msg = err_msg
@tool_api
def run(self, err_msg: Optional[str] = None) -> ActionReturn:
"""Return the error message.
Args:
err_msg (str, optional): The error message. If err_msg is not None,
it will be returned, otherwise the default error message will
be returned. Defaults to None.
Returns:
ActionReturn: The action return.
"""
action_return = ActionReturn(
url=None,
args=dict(text=err_msg),
type=self.name,
errmsg=err_msg or self._err_msg,
valid=ActionValidCode.INVALID,
state=ActionStatusCode.API_ERROR)
return action_return
class FinishAction(BaseAction):
"""This is a finish action class, which is used to return the final
result."""
def __init__(self, description: Optional[dict] = None, parser=BaseParser):
super().__init__(description, parser)
@tool_api
def run(self, response: str) -> ActionReturn:
"""Return the final result.
Args:
response (str): The final result.
Returns:
ActionReturn: The action return.
"""
action_return = ActionReturn(
url=None,
args=dict(text=response),
result=[dict(type='text', content=response)],
type=self.name,
valid=ActionValidCode.FINISH,
state=ActionStatusCode.SUCCESS)
return action_return
|