Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds user-specified inputs to application

elijahbenizzye4e16890377d
diff --git a/burr/core/action.py b/burr/core/action.py
index 7d2dc0e7..856a52a3 100644
--- a/burr/core/action.py
+++ b/burr/core/action.py
@@ -3,7 +3,7 @@ import ast
import copy
import inspect
import types
-from typing import Any, Callable, List, Protocol, Tuple, TypeVar, Union
+from typing import Any, Callable, Dict, List, Optional, Protocol, Tuple, TypeVar, Union
from burr.core.state import State
@@ -21,13 +21,47 @@ class Function(abc.ABC):
pass
@abc.abstractmethod
- def run(self, state: State) -> dict:
+ def run(self, state: State, **run_kwargs) -> dict:
"""Runs the function on the given state and returns the result.
The result is jsut a key/value dictionary.
:param state: State to run the function on
+ :param run_kwargs: Additional arguments to the function passed at runtime.
:return: Result of the function
"""
+ pass
+
+ @property
+ def inputs(self) -> list[str]:
+ """Represents inputs that are required for this to run.
+ These correspond to the `**function_kwargs` in `run` above.
+
+ :return:
+ """
+ return []
+
+ def validate_inputs(self, inputs: Optional[Dict[str, Any]]) -> None:
+ """Validates the inputs to the function. This is a convenience method
+ to allow for validation of inputs before running the function.
+
+ :param inputs: Inputs to validate
+ :raises ValueError: If the inputs are invalid
+ """
+ if inputs is None:
+ inputs = {}
+ required_inputs = set(self.inputs)
+ given_inputs = set(inputs.keys())
+ missing_inputs = required_inputs - given_inputs
+ additional_inputs = given_inputs - required_inputs
+ if missing_inputs or additional_inputs:
+ raise ValueError(
+ f"Inputs to function {self} are invalid. "
+ f"Missing the following inputs: {', '.join(missing_inputs)}."
+ if missing_inputs
+ else "" f"Additional inputs: {','.join(additional_inputs)}."
+ if additional_inputs
+ else ""
+ )
def is_async(self) -> bool:
"""Convenience method to check if the function is async or not.
@@ -148,7 +182,7 @@ class Condition(Function):
return Condition(keys, condition_func, name=expr)
- def run(self, state: State) -> dict:
+ def run(self, state: State, **kwargs) -> dict:
return {Condition.KEY: self._resolver(state)}
@property
@@ -197,12 +231,12 @@ expr = Condition.expr
class Result(Action):
- def __init__(self, fields: list[str]):
+ def __init__(self, *fields: str):
"""Represents a result action. This is purely a convenience class to
pull data from state and give it out to the result. It does nothing to
the state itself.
- :param fields: Fields to pull from the state
+ :param fields: Fields to pull from the state and put into results
"""
super(Result, self).__init__()
self._fields = fields
@@ -215,13 +249,43 @@ class Result(Action):
@property
def reads(self) -> list[str]:
- return self._fields
+ return list(self._fields)
@property
def writes(self) -> list[str]:
return []
+class Input(Action):
+ def __init__(self, *fields: str):
+ """Represents an input action -- this reads something from an input
+ then writes that directly to state. This is a convenience class for when you don't
+ need to process the input and just want to put it in state for later use.
+
+ :param fields: Fields to pull from the inputs and put into state
+ """
+ super(Input, self).__init__()
+ self._fields = fields
+
+ @property
+ def reads(self) -> list[str]:
+ return [] # nothing from state
+
+ def run(self, state: State, **run_kwargs) -> dict:
+ return {key: run_kwargs[key] for key in self._fields}
+
+ @property
+ def writes(self) -> list[str]:
+ return list(self._fields)
+
+ @property
+ def inputs(self) -> list[str]:
+ return list(self._fields)
+
+ def update(self, result: dict, state: State) -> State:
+ return state.update(**result)
+
+
class SingleStepAction(Action, abc.ABC):
"""Internal representation of a "single-step" action. While most actions will have
a run and an update, this is a convenience class for actions that return them both at the same time.
@@ -241,11 +305,12 @@ class SingleStepAction(Action, abc.ABC):
return True
@abc.abstractmethod
- def run_and_update(self, state: State) -> Tuple[dict, State]:
+ def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:
"""Performs a run/update at the same time.
- :param state:
- :return:
+ :param state: State to run the action on
+ :param run_kwargs: Additional arguments to the function passed at runtime.
+ :return: Result of the action and the new state
"""
pass
@@ -268,6 +333,15 @@ class SingleStepAction(Action, abc.ABC):
"SingleStepAction.update should never be called independently -- use run_and_update instead."
)
+ def is_async(self) -> bool:
+ """Convenience method to check if the function is async or not.
+ We'll want to clean up the class hierarchy, but this is all internal.
+ See note on ``run`` and ``update`` above
+
+ :return: True if the function is async, False otherwise
+ """
+ return inspect.iscoroutinefunction(self.run_and_update)
+
class FunctionBasedAction(SingleStepAction):
ACTION_FUNCTION = "action_function"
@@ -304,6 +378,14 @@ class FunctionBasedAction(SingleStepAction):
def writes(self) -> list[str]:
return self._writes
+ @property
+ def inputs(self) -> list[str]:
+ return [
+ param
+ for param in inspect.signature(self._fn).parameters
+ if param != "state" and param not in self._bound_params
+ ]
+
def with_params(self, **kwargs: Any) -> "FunctionBasedAction":
"""Binds parameters to the function.
Note that there is no reason to call this by the user. This *could*
@@ -317,8 +399,8 @@ class FunctionBasedAction(SingleStepAction):
new_action._bound_params = {**self._bound_params, **kwargs}
return new_action
- def run_and_update(self, state: State) -> Tuple[dict, State]:
- return self._fn(state, **self._bound_params)
+ def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:
+ return self._fn(state, **self._bound_params, **run_kwargs)
def _validate_action_function(fn: Callable):
diff --git a/burr/core/application.py b/burr/core/application.py
index d9e84ab5..c5d19d61 100644
--- a/burr/core/application.py
+++ b/burr/core/application.py
@@ -46,13 +46,14 @@ TerminationCondition = Literal["any_complete", "all_complete"]
PRIOR_STEP = "__PRIOR_STEP"
-def _run_function(function: Function, state: State) -> dict:
+def _run_function(function: Function, state: State, inputs: Dict[str, Any]) -> dict:
"""Runs a function, returning the result of running the function.
Note this restricts the keys in the state to only those that the
function reads.
- :param function:
- :param state:
+ :param function: Function to run
+ :param state: State at time of execution
+ :param inputs: Inputs to the function
:return:
"""
if function.is_async():
@@ -62,19 +63,16 @@ def _run_function(function: Function, state: State) -> dict:
"instead...)"
)
state_to_use = state.subset(*function.reads)
- return function.run(state_to_use)
+ function.validate_inputs(inputs)
+ return function.run(state_to_use, **inputs)
-async def _arun_function(function: Function, state: State) -> dict:
+async def _arun_function(function: Function, state: State, inputs: Dict[str, Any]) -> dict:
"""Runs a function, returning the result of running the function.
- Async version of the above.
-
- :param function:
- :param state:
- :return:
- """
+ Async version of the above."""
state_to_use = state.subset(*function.reads)
- return await function.run(state_to_use)
+ function.validate_inputs(inputs)
+ return await function.run(state_to_use, **inputs)
def _run_reducer(reducer: Reducer, state: State, result: dict, name: str) -> State:
@@ -122,38 +120,45 @@ def _create_dict_string(kwargs: dict) -> str:
return input_string
-def _format_error_message(action: Action, input_state: State) -> str:
+def _format_error_message(action: Action, input_state: State, inputs: dict) -> str:
"""Formats the error string, given that we're inside an action"""
message = f"> Action: {action.name} encountered an error!"
padding = " " * (80 - len(message) - 1)
message += padding + "<"
- input_string = _create_dict_string(input_state.get_all())
- message += "\n> State (at time of action):\n" + input_string
+ message += "\n> State (at time of action):\n" + _create_dict_string(input_state.get_all())
+ message += "\n> Inputs (at time of action):\n" + _create_dict_string(inputs)
border = "*" * 80
return "\n" + border + "\n" + message + "\n" + border
-def _run_single_step_action(action: SingleStepAction, state: State) -> Tuple[dict, State]:
+def _run_single_step_action(
+ action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]
+) -> Tuple[Dict[str, Any], State]:
"""Runs a single step action. This API is internal-facing and a bit in flux, but
it corresponds to the SingleStepAction class.
:param action: Action to run
:param state: State to run with
+ :param inputs: Inputs to pass directly to the action
:return: The result of running the action, and the new state
"""
state_to_use = state.subset(
*action.reads, *action.writes
) # TODO -- specify some as required and some as not
- result, new_state = action.run_and_update(state_to_use)
+ action.validate_inputs(inputs)
+ result, new_state = action.run_and_update(state_to_use, **inputs)
return result, state.merge(new_state.subset(*action.writes)) # we just want the writes action
-async def _arun_single_step_action(action: SingleStepAction, state: State) -> Tuple[dict, State]:
+async def _arun_single_step_action(
+ action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]
+) -> Tuple[dict, State]:
"""Runs a single step action in async. See the synchronous version for more details."""
state_to_use = state.subset(
*action.reads, *action.writes
) # TODO -- specify some as required and some as not
- result, new_state = await action.run_and_update(state_to_use)
+ action.validate_inputs(inputs)
+ result, new_state = await action.run_and_update(state_to_use, **inputs)
return result, state.merge(new_state.subset(*action.writes)) # we just want the writes action
@@ -192,7 +197,7 @@ class Application:
"post_application_create", state=self._state, application_graph=self._graph
)
- def step(self) -> Optional[Tuple[Action, dict, State]]:
+ def step(self, inputs: Optional[Dict[str, Any]] = None) -> Optional[Tuple[Action, dict, State]]:
"""Performs a single step, advancing the state machine along.
This returns a tuple of the action that was run, the result of running
the action, and the new state.
@@ -202,9 +207,12 @@ class Application:
the method you want -- you'll want iterate() (if you want to see the state/
results along the way), or run() (if you just want the final state/results).
+ :param inputs: Inputs to the action -- this is if this action requires an input that is passed in from the outside world
:return: Tuple[Function, dict, State] -- the function that was just ran, the result of running it, and the new state
"""
next_action = self.get_next_action()
+ if inputs is None:
+ inputs = {}
self._adapter_set.call_all_lifecycle_hooks_sync(
"pre_run_step", action=next_action, state=self._state
)
@@ -215,15 +223,15 @@ class Application:
new_state = self._state
try:
if next_action.single_step:
- result, new_state = _run_single_step_action(next_action, self._state)
+ result, new_state = _run_single_step_action(next_action, self._state, inputs)
else:
- result = _run_function(next_action, self._state)
+ result = _run_function(next_action, self._state, inputs)
new_state = _run_reducer(next_action, self._state, result, next_action.name)
new_state = new_state.update(**{PRIOR_STEP: next_action.name})
self._set_state(new_state)
except Exception as e:
exc = e
- logger.exception(_format_error_message(next_action, self._state))
+ logger.exception(_format_error_message(next_action, self._state, inputs))
raise e
finally:
self._adapter_set.call_all_lifecycle_hooks_sync(
@@ -231,12 +239,17 @@ class Application:
)
return next_action, result, new_state
- async def astep(self) -> Optional[Tuple[Action, dict, State]]:
+ async def astep(self, inputs: Dict[str, Any] = None) -> Optional[Tuple[Action, dict, State]]:
"""Asynchronous version of step.
- :return:
+ :param inputs: Inputs to the action -- this is if this action
+ requires an input that is passed in from the outside world
+
+ :return: Tuple[Function, dict, State] -- the action that was just ran, the result of running it, and the new state
"""
next_action = self.get_next_action()
+ if inputs is None:
+ inputs = {}
if next_action is None:
return None
await self._adapter_set.call_all_lifecycle_hooks_async(
@@ -251,17 +264,19 @@ class Application:
# but that's safer than assuming its OK to launch a thread
# TODO -- add an option/configuration to launch a thread (yikes, not super safe, but for a pure function
# which this is supposed to be its OK).
- # this delegatees hooks to the synchronous version, so we'll call all of them as well
- return self.step()
+ # this delegates hooks to the synchronous version, so we'll call all of them as well
+ return self.step(inputs=inputs)
if next_action.single_step:
- result, new_state = await _arun_single_step_action(next_action, self._state)
+ result, new_state = await _arun_single_step_action(
+ next_action, self._state, inputs=inputs
+ )
else:
- result = await _arun_function(next_action, self._state)
+ result = await _arun_function(next_action, self._state, inputs=inputs)
new_state = _run_reducer(next_action, self._state, result, next_action.name)
new_state = new_state.update(**{PRIOR_STEP: next_action.name})
except Exception as e:
exc = e
- logger.exception(_format_error_message(next_action, self._state))
+ logger.exception(_format_error_message(next_action, self._state, inputs))
raise e
finally:
await self._adapter_set.call_all_lifecycle_hooks_sync_and_async(
@@ -271,7 +286,11 @@ class Application:
return next_action, result, new_state
def iterate(
- self, *, until: list[str], gate: TerminationCondition = "any_complete"
+ self,
+ *,
+ until: list[str],
+ gate: TerminationCondition = "any_complete",
+ inputs: Optional[Dict[str, Any]] = None,
) -> Generator[Tuple[Action, dict, State], None, Tuple[State, List[dict]]]:
"""Returns a generator that calls step() in a row, enabling you to see the state
of the system as it updates. Note this returns a generator, and also the final result
@@ -280,9 +299,13 @@ class Application:
:param until: The list of actions to run until -- these must be strings
that match the names of the actions.
:param gate: The gate to run until. This can be "any_complete" or "all_complete"
+ :param inputs: Inputs to the action -- this is if this action requires an input that is passed in from the outside world.
+ Note that this is only used for the first iteration -- subsequent iterations will not use this.
:return: Each iteration returns the result of running `step`
The final result is the current state + results in the order that they were specified.
"""
+ if inputs is None:
+ inputs = {}
if gate != "any_complete":
raise NotImplementedError(
@@ -301,7 +324,8 @@ class Application:
)
while not condition():
- state_output = self.step()
+ state_output = self.step(inputs=inputs)
+ inputs = {} # only pass inputs in the first time
if state_output is None:
break
action, result, state = state_output
@@ -312,7 +336,11 @@ class Application:
return self._state, results
async def aiterate(
- self, *, until: list[str], gate: TerminationCondition = "any_complete"
+ self,
+ *,
+ until: list[str],
+ gate: TerminationCondition = "any_complete",
+ inputs: Optional[Dict[str, Any]] = None,
) -> AsyncGenerator[Tuple[Action, dict, State], Tuple[State, List[dict]]]:
"""Returns a generator that calls step() in a row, enabling you to see the state
of the system as it updates. This is the asynchronous version so it has no capability of t
@@ -320,6 +348,8 @@ class Application:
:param until: The list of actions to run until -- these must be strings
that match the names of the action.
:param gate: The gate to run until. This can be "any_complete" or "all_complete"
+ :param inputs: Inputs to the action -- this is if this action requires an input that is passed in from the outside world.
+ Note that this is only used for the first iteration -- subsequent iterations will not use this.
:return: Each iteration returns the result of running `step`
The final result is the current state + results in the order that they were specified.
"""
@@ -330,9 +360,12 @@ class Application:
if gate == "any_complete"
else lambda: len(seen_results) == len(until)
)
+ if inputs is None:
+ inputs = {}
while not condition():
- result = await self.astep()
+ result = await self.astep(inputs=inputs)
+ inputs = {} # only pass inputs in the first time
if result is None:
break
action, result, state = result
@@ -345,14 +378,17 @@ class Application:
*,
until: list[str],
gate: TerminationCondition = "any_complete",
+ inputs: Optional[Dict[str, Any]] = None,
) -> Tuple[State, List[dict]]:
- """
+ """Runs your application through until completion. Does
+ not give access to the state along the way -- if you want that, use iterate().
- :param gate:
- :param until:
- :return:
+ :param until: The list of actions to run until -- these must be strings
+ :param inputs: Inputs to the action -- this is if this action requires an input that is passed in from the outside world
+ :param gate: Gate to run until -- only accepts "any_complete" for now
+ :return: The final state, and the results of running the actions in the order that they were specified.
"""
- gen = self.iterate(until=until, gate=gate)
+ gen = self.iterate(until=until, gate=gate, inputs=inputs)
while True:
try:
next(gen)
@@ -364,17 +400,20 @@ class Application:
*,
until: list[str],
gate: TerminationCondition = "any_complete",
+ inputs: Optional[Dict[str, Any]] = None,
):
- """Asynchronous version of run.
+ """Runs your application through until completion, using async. Does
+ not give access to the state along the way -- if you want that, use iterate().
- :param gate:
- :param until:
- :return:
+ :param until: The list of actions to run until -- these must be strings
+ :param inputs: Inputs to the action -- this is if this action requires an input that is passed in from the outside world
+ :param gate: Gate to run until -- only accepts "any_complete" for now
+ :return: The final state, and the results of running the actions in the order that they were specified.
"""
state = self._state
results: List[Optional[dict]] = [None for _ in until]
index_by_name = {name: index for index, name in enumerate(until)}
- async for action, result, state in self.aiterate(until=until, gate=gate):
+ async for action, result, state in self.aiterate(until=until, gate=gate, inputs=inputs):
if action.name in until:
results[index_by_name[action.name]] = result
return state, results
@@ -430,6 +469,10 @@ 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:
+ input_name = f"input__{input_}"
+ digraph.node(input_name, shape="oval", style="dashed", label=f"input: {input_}")
+ digraph.edge(input_name, action.name)
for transition in self._transitions:
condition = transition.condition
digraph.edge(
diff --git a/examples/counter/application.py b/examples/counter/application.py
index 82e2eb62..f8dabc26 100644
--- a/examples/counter/application.py
+++ b/examples/counter/application.py
@@ -16,7 +16,7 @@ def application(count_up_to: int = 10, log_file: str = None):
return (
burr.core.ApplicationBuilder()
.with_state(counter=0)
- .with_actions(counter=counter, result=Result(["counter"]))
+ .with_actions(counter=counter, result=Result("counter"))
.with_transitions(
("counter", "counter", expr(f"counter < {count_up_to}")),
("counter", "result", default),
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
index c9539134..cee43c2c 100644
--- a/tests/core/test_action.py
+++ b/tests/core/test_action.py
@@ -7,7 +7,9 @@ from burr.core.action import (
Action,
Condition,
Function,
+ Input,
Result,
+ SingleStepAction,
_validate_action_function,
action,
create_action,
@@ -17,10 +19,15 @@ from burr.core.action import (
def test_is_async_true():
class AsyncFunction(Function):
+ @property
+ def inputs(self) -> list[str]:
+ return []
+
+ @property
def reads(self) -> list[str]:
return []
- async def run(self, state: State) -> dict:
+ async def run(self, state: State, **run_kwargs) -> dict:
return {}
func = AsyncFunction()
@@ -32,14 +39,14 @@ def test_is_async_false():
def reads(self) -> list[str]:
return []
- def run(self, state: State) -> dict:
+ def run(self, state: State, **run_kwargs) -> dict:
return {}
func = SyncFunction()
assert not func.is_async()
-class _BasicAction(Action):
+class BasicAction(Action):
@property
def reads(self) -> list[str]:
return ["input_variable"]
@@ -56,7 +63,7 @@ class _BasicAction(Action):
def test_with_name():
- action = _BasicAction()
+ action = BasicAction()
assert action.name is None # Nothing set initially
with_name = action.with_name("my_action")
assert with_name.name == "my_action" # Name set on copy
@@ -116,7 +123,7 @@ def test_condition_expr_complex():
def test_result():
- result = Result(fields=["foo", "bar"])
+ result = Result("foo", "bar")
assert result.run(State({"foo": "baz", "bar": "qux", "baz": "quux"})) == {
"foo": "baz",
"bar": "qux",
@@ -131,6 +138,18 @@ def test_result():
) # no impact
+def test_input():
+ input_action = Input("foo", "bar")
+ assert input_action.reads == []
+ assert input_action.writes == ["foo", "bar"]
+ assert input_action.inputs == ["foo", "bar"]
+ assert (result := input_action.run(State({}), foo="baz", bar="qux")) == {
+ "foo": "baz",
+ "bar": "qux",
+ }
+ assert input_action.update(result, State({})).get_all() == {"foo": "baz", "bar": "qux"}
+
+
def test_function_based_action():
@action(reads=["input_variable"], writes=["output_variable"])
def my_action(state: State) -> Tuple[dict, State]:
@@ -148,8 +167,23 @@ def test_function_based_action():
assert state.get_all() == {"input_variable": "foo", "output_variable": "foo"}
+def test_function_based_action_with_inputs():
+ @action(reads=["input_variable"], writes=["output_variable"])
+ def my_action(state: State, bound_input: int, unbound_input: int) -> Tuple[dict, State]:
+ res = state["input_variable"] + bound_input + unbound_input
+ return {"output_variable": res}, state.update(output_variable=res)
+
+ fn_based_action: SingleStepAction = create_action(
+ my_action.bind(bound_input=10), name="my_action"
+ )
+ 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}
+
+
def test_create_action_class_api():
- raw_action = _BasicAction()
+ raw_action = BasicAction()
created_action = create_action(raw_action, name="my_action")
assert created_action.name == "my_action"
assert created_action.reads == raw_action.reads
diff --git a/tests/core/test_application.py b/tests/core/test_application.py
index d1b111a7..edd05782 100644
--- a/tests/core/test_application.py
+++ b/tests/core/test_application.py
@@ -35,17 +35,23 @@ class PassedInAction(Action):
self,
reads: list[str],
writes: list[str],
- fn: Callable[[State], dict],
+ fn: Callable[..., dict],
update_fn: Callable[[dict, State], State],
+ inputs: list[str],
):
super(PassedInAction, self).__init__()
self._reads = reads
self._writes = writes
self._fn = fn
self._update_fn = update_fn
+ self._inputs = inputs
- def run(self, state: State) -> dict:
- return self._fn(state)
+ def run(self, state: State, **run_kwargs) -> dict:
+ return self._fn(state, **run_kwargs)
+
+ @property
+ def inputs(self) -> list[str]:
+ return self._inputs
def update(self, result: dict, state: State) -> State:
return self._update_fn(result, state)
@@ -64,13 +70,14 @@ class PassedInActionAsync(PassedInAction):
self,
reads: list[str],
writes: list[str],
- fn: Callable[[State], Awaitable[dict]],
+ fn: Callable[..., Awaitable[dict]],
update_fn: Callable[[dict, State], State],
+ inputs: list[str],
):
- super().__init__(reads=reads, writes=writes, fn=fn, update_fn=update_fn) # type: ignore
+ super().__init__(reads=reads, writes=writes, fn=fn, update_fn=update_fn, inputs=inputs) # type: ignore
- async def run(self, state: State) -> dict:
- return await self._fn(state)
+ async def run(self, state: State, **run_kwargs) -> dict:
+ return await self._fn(state, **run_kwargs)
base_counter_action = PassedInAction(
@@ -78,13 +85,24 @@ base_counter_action = PassedInAction(
writes=["counter"],
fn=lambda state: {"counter": state.get("counter", 0) + 1},
update_fn=lambda result, state: state.update(**result),
+ inputs=[],
+)
+
+base_counter_action_with_inputs = PassedInAction(
+ reads=["counter"],
+ writes=["counter"],
+ fn=lambda state, additional_increment: {
+ "counter": state.get("counter", 0) + 1 + additional_increment
+ },
+ update_fn=lambda result, state: state.update(**result),
+ inputs=["additional_increment"],
)
-async def _counter_update_async(state: State) -> dict:
+async def _counter_update_async(state: State, additional_increment: int = 0) -> dict:
await asyncio.sleep(0.0001) # just so we can make this *truly* async
# does not matter, but more accurately simulates an async function
- return {"counter": state.get("counter", 0) + 1}
+ return {"counter": state.get("counter", 0) + 1 + additional_increment}
base_counter_action_async = PassedInActionAsync(
@@ -92,6 +110,17 @@ base_counter_action_async = PassedInActionAsync(
writes=["counter"],
fn=_counter_update_async,
update_fn=lambda result, state: state.update(**result),
+ inputs=[],
+)
+
+base_counter_action_with_inputs_async = PassedInActionAsync(
+ reads=["counter"],
+ writes=["counter"],
+ fn=lambda state, additional_increment: _counter_update_async(
+ state, additional_increment=additional_increment
+ ),
+ update_fn=lambda result, state: state.update(**result),
+ inputs=["additional_increment"],
)
@@ -104,6 +133,7 @@ base_broken_action = PassedInAction(
writes=[],
fn=lambda x: exec("raise(BrokenStepException(x))"),
update_fn=lambda result, state: state,
+ inputs=[],
)
base_broken_action_async = PassedInActionAsync(
@@ -111,6 +141,7 @@ base_broken_action_async = PassedInActionAsync(
writes=[],
fn=lambda x: exec("raise(BrokenStepException(x))"),
update_fn=lambda result, state: state,
+ inputs=[],
)
@@ -118,29 +149,45 @@ def test__run_function():
"""Tests that we can run a function"""
action = base_counter_action
state = State({})
- result = _run_function(action, state)
+ result = _run_function(action, state, inputs={})
assert result == {"counter": 1}
+def test__run_function_with_inputs():
+ """Tests that we can run a function"""
+ action = base_counter_action_with_inputs
+ state = State({})
+ result = _run_function(action, state, inputs={"additional_increment": 1})
+ assert result == {"counter": 2}
+
+
def test__run_function_cant_run_async():
"""Tests that we can't run an async function"""
action = base_counter_action_async
state = State({})
with pytest.raises(ValueError, match="async"):
- _run_function(action, state)
+ _run_function(action, state, inputs={})
-async def test__a_run_function():
+async def test__arun_function():
"""Tests that we can run an async function"""
action = base_counter_action_async
state = State({})
- result = await _arun_function(action, state)
+ result = await _arun_function(action, state, inputs={})
assert result == {"counter": 1}
+async def test__arun_function_with_inputs():
+ """Tests that we can run an async function"""
+ action = base_counter_action_with_inputs_async
+ state = State({})
+ result = await _arun_function(action, state, inputs={"additional_increment": 1})
+ assert result == {"counter": 2}
+
+
class SingleStepCounter(SingleStepAction):
- def run_and_update(self, state: State) -> Tuple[dict, State]:
- result = {"count": state["count"] + 1}
+ def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:
+ result = {"count": state["count"] + 1 + sum([0] + list(run_kwargs.values()))}
return result, state.update(**result).append(tracker=result["count"])
@property
@@ -152,10 +199,16 @@ class SingleStepCounter(SingleStepAction):
return ["count", "tracker"]
+class SingleStepCounterWithInputs(SingleStepCounter):
+ @property
+ def inputs(self) -> list[str]:
+ return ["additional_increment"]
+
+
class SingleStepCounterAsync(SingleStepCounter):
- async def run_and_update(self, state: State) -> Tuple[dict, State]:
+ async def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:
await asyncio.sleep(0.0001) # just so we can make this *truly* async
- return super(SingleStepCounterAsync, self).run_and_update(state)
+ return super(SingleStepCounterAsync, self).run_and_update(state, **run_kwargs)
@property
def reads(self) -> list[str]:
@@ -166,32 +219,66 @@ class SingleStepCounterAsync(SingleStepCounter):
return ["count", "tracker"]
+class SingleStepCounterWithInputsAsync(SingleStepCounterAsync):
+ @property
+ def inputs(self) -> list[str]:
+ return ["additional_increment"]
+
+
base_single_step_counter = SingleStepCounter()
base_single_step_counter_async = SingleStepCounterAsync()
+base_single_step_counter_with_inputs = SingleStepCounterWithInputs()
+base_single_step_counter_with_inputs_async = SingleStepCounterWithInputsAsync()
def test__run_single_step_action():
action = base_single_step_counter.with_name("counter")
state = State({"count": 0, "tracker": []})
- result, state = _run_single_step_action(action, state)
+ result, state = _run_single_step_action(action, state, inputs={})
assert result == {"count": 1}
assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}
- result, state = _run_single_step_action(action, state)
+ result, state = _run_single_step_action(action, state, inputs={})
assert result == {"count": 2}
assert state.subset("count", "tracker").get_all() == {"count": 2, "tracker": [1, 2]}
+def test__run_single_step_action_with_inputs():
+ action = base_single_step_counter_with_inputs.with_name("counter")
+ state = State({"count": 0, "tracker": []})
+ result, state = _run_single_step_action(action, state, inputs={"additional_increment": 1})
+ assert result == {"count": 2}
+ assert state.subset("count", "tracker").get_all() == {"count": 2, "tracker": [2]}
+ result, state = _run_single_step_action(action, state, inputs={"additional_increment": 1})
+ assert result == {"count": 4}
+ assert state.subset("count", "tracker").get_all() == {"count": 4, "tracker": [2, 4]}
+
+
async def test__arun_single_step_action():
action = base_single_step_counter_async.with_name("counter")
state = State({"count": 0, "tracker": []})
- result, state = await _arun_single_step_action(action, state)
+ result, state = await _arun_single_step_action(action, state, inputs={})
assert result == {"count": 1}
assert state.subset("count", "tracker").get_all() == {"count": 1, "tracker": [1]}
- result, state = await _arun_single_step_action(action, state)
+ result, state = await _arun_single_step_action(action, state, inputs={})
assert result == {"count": 2}
assert state.subset("count", "tracker").get_all() == {"count": 2, "tracker": [1, 2]}
+async def test__arun_single_step_action_with_inputs():
+ action = base_single_step_counter_with_inputs_async.with_name("counter")
+ state = State({"count": 0, "tracker": []})
+ result, state = await _arun_single_step_action(
+ action, state, inputs={"additional_increment": 1}
+ )
+ assert result == {"count": 2}
+ assert state.subset("count", "tracker").get_all() == {"count": 2, "tracker": [2]}
+ result, state = await _arun_single_step_action(
+ action, state, inputs={"additional_increment": 1}
+ )
+ assert result == {"count": 4}
+ assert state.subset("count", "tracker").get_all() == {"count": 4, "tracker": [2, 4]}
+
+
def test_app_step():
"""Tests that we can run a step in an app"""
counter_action = base_counter_action.with_name("counter")
@@ -207,6 +294,34 @@ def test_app_step():
assert state[PRIOR_STEP] == "counter" # internal contract, not part of the public API
+def test_app_step_with_inputs():
+ """Tests that we can run a step in an app"""
+ counter_action = base_single_step_counter_with_inputs.with_name("counter")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({"count": 0, "tracker": []}),
+ initial_step="counter",
+ )
+ action, result, state = app.step(inputs={"additional_increment": 1})
+ assert action.name == "counter"
+ assert result == {"count": 2}
+ assert state.subset("count", "tracker").get_all() == {"count": 2, "tracker": [2]}
+
+
+def test_app_step_with_inputs_missing():
+ """Tests that we can run a step in an app"""
+ counter_action = base_single_step_counter_with_inputs.with_name("counter")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({"count": 0, "tracker": []}),
+ initial_step="counter",
+ )
+ with pytest.raises(ValueError, match="Missing inputs"):
+ app.step(inputs={})
+
+
def test_app_step_broken(caplog):
"""Tests that we can run a step in an app"""
broken_action = base_broken_action.with_name("broken_action_unique_name")
@@ -247,6 +362,34 @@ async def test_app_astep():
assert state[PRIOR_STEP] == "counter_async" # internal contract, not part of the public API
+async def test_app_astep_with_inputs():
+ """Tests that we can run an async step in an app"""
+ counter_action = base_single_step_counter_with_inputs_async.with_name("counter_async")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({"count": 0, "tracker": []}),
+ initial_step="counter_async",
+ )
+ action, result, state = await app.astep(inputs={"additional_increment": 1})
+ assert action.name == "counter_async"
+ assert result == {"count": 2}
+ assert state.subset("count", "tracker").get_all() == {"count": 2, "tracker": [2]}
+
+
+async def test_app_astep_with_inputs_missing():
+ """Tests that we can run an async step in an app"""
+ counter_action = base_single_step_counter_with_inputs_async.with_name("counter_async")
+ app = Application(
+ actions=[counter_action],
+ transitions=[Transition(counter_action, counter_action, default)],
+ state=State({"count": 0, "tracker": []}),
+ initial_step="counter_async",
+ )
+ with pytest.raises(ValueError, match="Missing inputs"):
+ await app.astep(inputs={})
+
+
async def test_app_astep_broken(caplog):
"""Tests that we can run a step in an app"""
broken_action = base_broken_action_async.with_name("broken_action_unique_name")
@@ -303,8 +446,8 @@ async def test_app_many_a_steps():
assert result == {"counter": 100}
-def test_app_iterate():
- result_action = Result(fields=["counter"]).with_name("result")
+def test_iterate():
+ result_action = Result("counter").with_name("result")
counter_action = base_counter_action.with_name("counter")
app = Application(
actions=[counter_action, result_action],
@@ -339,8 +482,31 @@ def test_app_iterate():
assert result["counter"] == 10
-async def test_app_a_iterate():
- result_action = Result(fields=["counter"]).with_name("result")
+def test_iterate_with_inputs():
+ result_action = Result("counter").with_name("result")
+ counter_action = base_counter_action_with_inputs.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 2")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ gen = app.iterate(
+ until=["result"], inputs={"additional_increment": 10}
+ ) # make it go quicly to the end
+ while True:
+ try:
+ action, result, state = next(gen)
+ except StopIteration as e:
+ assert e.value[0]["counter"] == 11 # 1 + 10, for the first one
+ break
+
+
+async def test_aiterate():
+ result_action = Result("counter").with_name("result")
counter_action = base_counter_action_async.with_name("counter")
app = Application(
actions=[counter_action, result_action],
@@ -358,17 +524,35 @@ async def test_app_a_iterate():
# return anything (async generators are not allowed to).
async for action, result, state in gen:
if action.name == "counter":
- assert state["counter"] == counter + 1
- assert result["counter"] == state["counter"]
+ assert state["counter"] == state["counter"] == counter + 1
counter = result["counter"]
else:
res.append(result)
- assert state["counter"] == 10
- assert result["counter"] == 10
+ assert state["counter"] == result["counter"] == 10
+
+
+async def test_app_aiterate_with_inputs():
+ result_action = Result("counter").with_name("result")
+ counter_action = base_counter_action_with_inputs_async.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ gen = app.aiterate(until=["result"], inputs={"additional_increment": 10})
+ async for action, result, state in gen:
+ if action.name == "counter":
+ assert result["counter"] == state["counter"] == 11
+ else:
+ assert state["counter"] == result["counter"] == 11
-def test_app_run():
- result_action = Result(fields=["counter"]).with_name("result")
+def test_run():
+ result_action = Result("counter").with_name("result")
counter_action = base_counter_action.with_name("counter")
app = Application(
actions=[counter_action, result_action],
@@ -385,8 +569,25 @@ def test_app_run():
assert results[0]["counter"] == 10
-async def test_app_a_run():
- result_action = Result(fields=["counter"]).with_name("result")
+def test_run_with_inputs():
+ result_action = Result("counter").with_name("result")
+ counter_action = base_counter_action_with_inputs.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ state, results = app.run(until=["result"], inputs={"additional_increment": 10})
+ assert state["counter"] == results[0]["counter"] == 11
+ assert len(results) == 1
+
+
+async def test_arun():
+ result_action = Result("counter").with_name("result")
counter_action = base_counter_action_async.with_name("counter")
app = Application(
actions=[counter_action, result_action],
@@ -398,14 +599,29 @@ async def test_app_a_run():
initial_step="counter",
)
state, results = await app.arun(until=["result"])
- assert state["counter"] == 10
+ assert state["counter"] == results[0]["counter"] == 10
+ assert len(results) == 1
+
+
+async def test_arun_with_inputs():
+ result_action = Result("counter").with_name("result")
+ counter_action = base_counter_action_with_inputs_async.with_name("counter")
+ app = Application(
+ actions=[counter_action, result_action],
+ transitions=[
+ Transition(counter_action, counter_action, Condition.expr("counter < 10")),
+ Transition(counter_action, result_action, default),
+ ],
+ state=State({}),
+ initial_step="counter",
+ )
+ state, results = await app.arun(until=["result"], inputs={"additional_increment": 10})
+ assert state["counter"] == results[0]["counter"] == 11
assert len(results) == 1
- (result,) = results
- assert result["counter"] == 10
async def test_app_a_run_async_and_sync():
- result_action = Result(fields=["counter"]).with_name("result")
+ result_action = Result("counter").with_name("result")
counter_action_sync = base_counter_action_async.with_name("counter_sync")
counter_action_async = base_counter_action_async.with_name("counter_async")
app = Application(
@@ -472,7 +688,7 @@ def test_application_builder_complete():
app = (
ApplicationBuilder()
.with_state(counter=0)
- .with_actions(counter=base_counter_action, result=Result(fields=["counter"]))
+ .with_actions(counter=base_counter_action, result=Result("counter"))
.with_transitions(
("counter", "counter", Condition.expr("counter < 10")), ("counter", "result")
)
@@ -524,7 +740,7 @@ def test__validate_start_not_found():
def test__validate_actions_valid():
- _validate_actions([Result(["test"])])
+ _validate_actions([Result("test")])
def test__validate_actions_empty():
@@ -560,7 +776,7 @@ def test_application_run_step_hooks_sync():
tracker = ActionTracker()
counter_action = base_counter_action.with_name("counter")
- result_action = Result(fields=["counter"]).with_name("result")
+ result_action = Result("counter").with_name("result")
app = Application(
actions=[counter_action, result_action],
transitions=[
@@ -594,7 +810,7 @@ async def test_application_run_step_hooks_async():
tracker = ActionTrackerAsync()
counter_action = base_counter_action.with_name("counter")
- result_action = Result(fields=["counter"]).with_name("result")
+ result_action = Result("counter").with_name("result")
app = Application(
actions=[counter_action, result_action],
transitions=[
@@ -624,7 +840,7 @@ def test_application_post_application_create_hook():
tracker = PostApplicationCreateTracker()
counter_action = base_counter_action.with_name("counter")
- result_action = Result(fields=["counter"]).with_name("result")
+ result_action = Result("counter").with_name("result")
Application(
actions=[counter_action, result_action],
transitions=[
@@ -642,7 +858,7 @@ def test_application_post_application_create_hook():
async def test_application_gives_graph():
counter_action = base_counter_action.with_name("counter")
- result_action = Result(fields=["counter"]).with_name("result")
+ result_action = Result("counter").with_name("result")
app = Application(
actions=[counter_action, result_action],
transitions=[
diff --git a/tests/tracking/test_local_tracking_client.py b/tests/tracking/test_local_tracking_client.py
index 10bf6aad..46eaf1d0 100644
--- a/tests/tracking/test_local_tracking_client.py
+++ b/tests/tracking/test_local_tracking_client.py
@@ -23,7 +23,7 @@ def sample_application(project_name: str, log_dir: str, app_id: str, broken: boo
return (
burr.core.ApplicationBuilder()
.with_state(counter=0, break_at=2 if broken else -1)
- .with_actions(counter=counter, result=Result(["counter"]))
+ .with_actions(counter=counter, result=Result("counter"))
.with_transitions(
("counter", "counter", expr("counter < 2")), # just count to two for testing
("counter", "result", default),