Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Adds the ability for inputs to be default
elijahbenizzy018507c7629e
diff --git a/burr/core/action.py b/burr/core/action.pyindex 6022f570..7cbeff56 100644--- a/burr/core/action.py+++ b/burr/core/action.py@@ -46,14 +46,30 @@ class Function(abc.ABC):pass@property- def inputs(self) -> list[str]:- """Represents inputs that are required for this to run.+ def inputs(self) -> Union[list[str], tuple[list[str], list[str]]]:+ """Represents inputs that are used for this to run.These correspond to the `**function_kwargs` in `run` above.+ Note that this has two possible return values:+ 1. A list of strings -- these are the keys that are required to run the function+ 2. A tuple of two lists of strings -- the first list is the required keys, the second is the optional keys+:return:"""return []+ @property+ def optional_and_required_inputs(self) -> Tuple[set[str], set[str]]:+ """Returns a tuple of two lists of strings -- the first list is the required keys, the second is the optional keys.+ This is internal and not meant to override.++ :return: Tuple of required keys and optional keys+ """+ inputs = self.inputs+ if isinstance(inputs, tuple):+ return set(inputs[0]), set(inputs[1])+ return set(inputs), set()+def validate_inputs(self, inputs: Optional[Dict[str, Any]]) -> None:"""Validates the inputs to the function. This is a convenience methodto allow for validation of inputs before running the function.@@ -63,10 +79,10 @@ class Function(abc.ABC):"""if inputs is None:inputs = {}- required_inputs = set(self.inputs)+ required_inputs, optional_inputs = self.optional_and_required_inputsgiven_inputs = set(inputs.keys())missing_inputs = required_inputs - given_inputs- additional_inputs = given_inputs - required_inputs+ additional_inputs = given_inputs - required_inputs - optional_inputsif missing_inputs or additional_inputs:raise ValueError(f"Inputs to function {self} are invalid. "@@ -367,14 +383,18 @@ class SingleStepAction(Action, abc.ABC):# the following exist to share implementation between FunctionBasedStreamingAction and FunctionBasedAction# TODO -- think through the class hierarchy to simplify, for now this is OK-def _get_inputs(bound_params: dict, fn: Callable) -> list[str]:+def _get_inputs(bound_params: dict, fn: Callable) -> tuple[list[str], list[str]]:sig = inspect.signature(fn)- out = []+ required_inputs, optional_inputs = [], []for param_name, param in sig.parameters.items():if param_name != "state" and param_name not in bound_params:if param.default is inspect.Parameter.empty:- out.append(param_name)- return out+ # has no default means its required+ required_inputs.append(param_name)+ else:+ # has a default means its optional+ optional_inputs.append(param_name)+ return required_inputs, optional_inputsFunctionBasedActionType = TypeVar(@@ -382,21 +402,15 @@ FunctionBasedActionType = TypeVar()-def _with_params(action: FunctionBasedActionType, **kwargs: Any) -> FunctionBasedActionType:- new_action = copy.copy(action)- new_action._bound_params = {**action._bound_params, **kwargs}- return new_action--class FunctionBasedAction(SingleStepAction):ACTION_FUNCTION = "action_function"def __init__(- self,- fn: Callable,- reads: List[str],- writes: List[str],- bound_params: dict = None,+ self,+ fn: Callable,+ reads: List[str],+ writes: List[str],+ bound_params: dict = None,):"""Instantiates a function-based action with the given function, reads, and writes.The function must take in a state and return a tuple of (result, new_state).@@ -410,6 +424,7 @@ class FunctionBasedAction(SingleStepAction):self._reads = readsself._writes = writesself._bound_params = bound_params if bound_params is not None else {}+ self._inputs = _get_inputs(self._bound_params, self._fn)@propertydef fn(self) -> Callable:@@ -424,8 +439,8 @@ class FunctionBasedAction(SingleStepAction):return self._writes@property- def inputs(self) -> list[str]:- return _get_inputs(self._bound_params, self._fn)+ def inputs(self) -> tuple[list[str], list[str]]:+ return self._inputsdef with_params(self, **kwargs: Any) -> "FunctionBasedAction":"""Binds parameters to the function.@@ -436,7 +451,9 @@ class FunctionBasedAction(SingleStepAction)::param kwargs::return:"""- return _with_params(self, **kwargs)+ return FunctionBasedAction(+ self._fn, self._reads, self._writes, {**self._bound_params, **kwargs}+ )def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:return self._fn(state, **self._bound_params, **run_kwargs)@@ -533,11 +550,11 @@ class StreamingResultContainer(Iterator[dict]):return next(self.generator())def __init__(- self,- streaming_result_generator: Generator[dict, None, Tuple[dict, State]],- initial_state: State,- process_result: Callable[[dict, State], Tuple[dict, State]],- callback: Callable[[Optional[dict], State, Optional[Exception]], None],+ self,+ streaming_result_generator: Generator[dict, None, Tuple[dict, State]],+ initial_state: State,+ process_result: Callable[[dict, State], Tuple[dict, State]],+ callback: Callable[[Optional[dict], State, Optional[Exception]], None],):"""Initializes a ``StreamingResultContainer``. This is meant to be used internally@@ -596,7 +613,7 @@ class SingleStepStreamingAction(SingleStepAction, abc.ABC):@abc.abstractmethoddef stream_run_and_update(- self, state: State, **run_kwargs+ self, state: State, **run_kwargs) -> Generator[dict, None, Tuple[dict, State]]:"""Streaming version of the run and update function. Thisreturn type is a generator that streams in a result, has no "send"@@ -625,11 +642,11 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):_fn: Callable[..., Generator[dict, None, Tuple[dict, State]]]def __init__(- self,- fn: Callable[..., Generator[dict, None, Tuple[dict, State]]],- reads: List[str],- writes: List[str],- bound_params: dict = None,+ self,+ fn: Callable[..., Generator[dict, None, Tuple[dict, State]]],+ reads: List[str],+ writes: List[str],+ bound_params: dict = None,):"""Instantiates a function-based streaming action with the given function, reads, and writes.The function must take in a state (and inputs) and return a generator of (result, new_state).@@ -645,7 +662,7 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):self._bound_params = bound_params if bound_params is not None else {}def stream_run_and_update(- self, state: State, **run_kwargs+ self, state: State, **run_kwargs) -> Generator[dict, None, Tuple[dict, State]]:return (yield from self._fn(state, **self._bound_params, **run_kwargs))@@ -662,10 +679,18 @@ class FunctionBasedStreamingAction(SingleStepStreamingAction):return Truedef with_params(self, **kwargs: Any) -> "FunctionBasedStreamingAction":- return _with_params(self, **kwargs)+ """Binds parameters to the function. This is not user-facing -- this is+ meant to be used internally by the API.++ :param kwargs:+ :return:+ """+ return FunctionBasedStreamingAction(+ self._fn, self._reads, self._writes, {**self._bound_params, **kwargs}+ )@property- def inputs(self) -> list[str]:+ def inputs(self) -> tuple[list[str], list[str]]:return _get_inputs(self._bound_params, self._fn)@property@@ -758,7 +783,7 @@ def action(reads: List[str], writes: List[str]) -> Callable[[Callable], Functiondef streaming_action(- reads: List[str], writes: List[str]+ reads: List[str], writes: List[str]) -> Callable[[Callable], FunctionRepresentingAction]:"""Decorator to create a streaming function-based action. This is user-facing.diff --git a/burr/core/application.py b/burr/core/application.pyindex 21c4c4ea..01def373 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -403,17 +403,18 @@ class Application:def _process_inputs(self, inputs: Dict[str, Any], action: Action) -> Dict[str, Any]:inputs = inputs.copy()processed_inputs = {}+ required_inputs, optional_inputs = action.optional_and_required_inputsfor key in list(inputs.keys()):- if key in action.inputs:+ if key in required_inputs or key in optional_inputs:processed_inputs[key] = inputs.pop(key)if len(inputs) > 0 and logger.isEnabledFor(logging.DEBUG):logger.debug(f"Keys {inputs.keys()} were passed in as inputs to action "f"{action.name}, but not declared by the action as an input. "- f"Action only needs: {action.inputs} so we're just letting you know "- f"some inputs are being skipped."+ f"Action only needs: {required_inputs} (optionally: {optional_inputs}) "+ f"so we're just letting you know some inputs are being skipped.")- missing_inputs = set(action.inputs) - set(processed_inputs.keys())+ missing_inputs = required_inputs - set(processed_inputs.keys())for required_input in list(missing_inputs):# if we can find it in the dependency factory, we'll use thatif required_input in self.dependency_factory:@@ -986,7 +987,8 @@ class Application:else f"{action.name}({', '.join(action.reads)}): {', '.join(action.writes)}")digraph.node(action.name, label=label, shape="box", style="rounded")- for input_ in action.inputs:+ required_inputs, optional_inputs = action.optional_and_required_inputs+ for input_ in required_inputs | optional_inputs:if input_.startswith("__"):# These are internally injected by the frameworkcontinuediff --git a/burr/tracking/common/models.py b/burr/tracking/common/models.pyindex 8ff760a9..75a275a3 100644--- a/burr/tracking/common/models.py+++ b/burr/tracking/common/models.py@@ -50,12 +50,13 @@ class ActionModel(IdentifyingModel):code = inspect.getsource(action.fn)else:code = inspect.getsource(action.__class__)+ optional_inputs, required_inputs = action.optional_and_required_inputsreturn ActionModel(name=action.name,reads=list(action.reads),writes=list(action.writes),code=code,- inputs=action.inputs,+ inputs=list(required_inputs | optional_inputs),)diff --git a/tests/core/test_action.py b/tests/core/test_action.pyindex 58ae510b..652b0c79 100644--- a/tests/core/test_action.py+++ b/tests/core/test_action.py@@ -181,7 +181,7 @@ def test_function_based_action_with_inputs():fn_based_action: SingleStepAction = create_action(my_action.bind(bound_input=10), name="my_action")- assert fn_based_action.inputs == ["unbound_input"]+ assert fn_based_action.inputs == (["unbound_input"], [])result, state = fn_based_action.run_and_update(State({"input_variable": 1}), unbound_input=100)assert state.get_all() == {"input_variable": 1, "output_variable": 111}assert result == {"output_variable": 111}@@ -198,12 +198,33 @@ def test_function_based_action_with_defaults():fn_based_action: SingleStepAction = create_action(my_action.bind(bound_input=10), name="my_action")- assert fn_based_action.inputs == ["unbound_input"]+ assert fn_based_action.inputs == (["unbound_input"], ["unbound_default_input"])result, state = fn_based_action.run_and_update(State({"input_variable": 1}), unbound_input=100)assert state.get_all() == {"input_variable": 1, "output_variable": 1111}assert result == {"output_variable": 1111}+def test_function_based_action_with_defaults_unbound():+ # inputs can have defaults -- this tests that we treat them propertly+ @action(reads=["input_variable"], writes=["output_variable"])+ def my_action(+ state: State, unbound_input_1: int, unbound_input_2: int, unbound_default_input: int = 1000+ ) -> Tuple[dict, State]:+ res = state["input_variable"] + unbound_input_1 + unbound_input_2 + unbound_default_input+ return {"output_variable": res}, state.update(output_variable=res)++ fn_based_action: SingleStepAction = create_action(my_action, name="my_action")+ assert fn_based_action.inputs == (+ ["unbound_input_1", "unbound_input_2"],+ ["unbound_default_input"],+ )+ result, state = fn_based_action.run_and_update(+ State({"input_variable": 1}), unbound_input_1=10, unbound_input_2=100+ )+ assert state.get_all() == {"input_variable": 1, "output_variable": 1111}+ assert result == {"output_variable": 1111}++async def test_function_based_action_async():@action(reads=["input_variable"], writes=["output_variable"])async def my_action(state: State) -> Tuple[dict, State]:@@ -234,7 +255,7 @@ async def test_function_based_action_with_inputs_async():my_action.bind(bound_input=10), name="my_action")assert fn_based_action.is_async()- assert fn_based_action.inputs == ["unbound_input"]+ assert fn_based_action.inputs == (["unbound_input"], [])result, state = await fn_based_action.run_and_update(State({"input_variable": 1}), unbound_input=100)@@ -254,7 +275,7 @@ async def test_function_based_action_with_defaults_async():fn_based_action: SingleStepAction = create_action(my_action.bind(bound_input=10), name="my_action")- assert fn_based_action.inputs == ["unbound_input"]+ assert fn_based_action.inputs == (["unbound_input"], ["unbound_default_input"])result, state = await fn_based_action.run_and_update(State({"input_variable": 1}), unbound_input=100)