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
Implements streaming action
elijahbenizzy25bbc9fe9750
diff --git a/burr/core/action.py b/burr/core/action.pyindex eb7fc693..9ab06f8d 100644--- a/burr/core/action.py+++ b/burr/core/action.py@@ -1,9 +1,23 @@import abcimport ast+import collectionsimport copyimport inspect+import sysimport types-from typing import Any, Callable, Dict, List, Optional, Protocol, Tuple, TypeVar, Union+from typing import (+ Any,+ Callable,+ Dict,+ Generator,+ Iterator,+ List,+ Optional,+ Protocol,+ Tuple,+ TypeVar,+ Union,+)from burr.core.state import State@@ -128,6 +142,10 @@ class Action(Function, Reducer, abc.ABC):def single_step(self) -> bool:return False+ @property+ def streaming(self) -> bool:+ return False+def __repr__(self):read_repr = ", ".join(self.reads) if self.reads else "{}"write_repr = ", ".join(self.writes) if self.writes else "{}"@@ -347,12 +365,35 @@ class SingleStepAction(Action, abc.ABC):return inspect.iscoroutinefunction(self.run_and_update)+# 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]:+ sig = inspect.signature(fn)+ out = []+ 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+++FunctionBasedActionType = TypeVar(+ "FunctionBasedActionType", bound=Union["FunctionBasedAction", "FunctionBasedStreamingAction"]+)+++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[..., Tuple[dict, State]],+ fn: Callable,reads: List[str],writes: List[str],bound_params: dict = None,@@ -384,13 +425,7 @@ class FunctionBasedAction(SingleStepAction):@propertydef inputs(self) -> list[str]:- sig = inspect.signature(self._fn)- out = []- for param_name, param in sig.parameters.items():- if param_name != "state" and param_name not in self._bound_params:- if param.default is inspect.Parameter.empty:- out.append(param_name)- return out+ return _get_inputs(self._bound_params, self._fn)def with_params(self, **kwargs: Any) -> "FunctionBasedAction":"""Binds parameters to the function.@@ -401,9 +436,7 @@ class FunctionBasedAction(SingleStepAction)::param kwargs::return:"""- new_action = copy.copy(self)- new_action._bound_params = {**self._bound_params, **kwargs}- return new_action+ return _with_params(self, **kwargs)def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:return self._fn(state, **self._bound_params, **run_kwargs)@@ -412,6 +445,230 @@ class FunctionBasedAction(SingleStepAction):return inspect.iscoroutinefunction(self._fn)+class StreamingAction(Action, abc.ABC):+ @abc.abstractmethod+ def stream_run(self, state: State, **run_kwargs) -> Generator[dict, None, dict]:+ """Streaming action ``stream_run`` is different than standard action run. It:+ 1. streams in a result (the dict output)+ 2. Returns the final result++ Note that the user, in this case, is responsible for joining the result.++ For instance, you could have:++ .. code-block:: python++ def stream_run(state: State) -> Generator[dict, None, dict]:+ buffer = [] # you might want to be more efficient than simple strcat+ for token in query(state['prompt']):+ yield {'response' : token}+ buffer.append(token)+ return {'response' : "".join(buffer)}++ This would utilize a simple string buffer (implemented by a list) to store the results+ and then join them at the end. We return the final result.++ :param state: State to run the action on+ :param run_kwargs: parameters passed to the run function -- these are specified by `inputs`+ :return: A generator that streams in a result and returns the final result+ """+ pass++ def run(self, state: State, **run_kwargs) -> dict:+ gen = self.stream_run(state, **run_kwargs)+ while True:+ try:+ next(gen) # if we just run through, we do nothing with the result+ except StopIteration as e:+ return e.value++ @property+ def streaming(self) -> bool:+ return True+++# TODO -- documentation for this+class StreamingResultContainer(Iterator[dict]):+ """Container for a streaming result. This allows you to:+ 1. Iterate over the result as it comes in+ 2. Get the final result/state at the end++ If you're familiar with generators/iterators in python, this is effectively an+ iterator that caches the final result after calling it. This is meant to be used+ exclusively with the streaming action calls in `Application`. Note that you will+ never instantiate this class directly, but you will use it in the API when it is returned+ by :py:meth:`stream_result <burr.core.application.Application.stream_result>`.+ For reference, here's how you would use it:++ .. code-block:: python++ streaming_result_container = application.stream_result(...)+ action_we_just_ran = streaming_result_container.get()+ print(f"getting streaming results for action={action_we_just_ran.name}")++ for result_component in streaming_result_container:+ print(result_component['response']) # this assumes you have a response key in your result++ final_state, final_result = streaming_result_container.get()+ """++ @staticmethod+ def pass_through(results: dict, final_state: State) -> "StreamingResultContainer":+ """Instantiates a streaming result container that just passes through the given results+ This is to be used internally -- it allows us to wrap non-streaming action results in a streaming+ result container."""++ def empty_generator() -> Generator[dict, None, Tuple[dict, State]]:+ yield from ()+ return results, final_state++ return StreamingResultContainer(+ empty_generator(),+ final_state,+ lambda result, state: (result, state),+ lambda result, state, exc: None,+ )++ def __next__(self):+ 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],+ ):+ """Initializes a ``StreamingResultContainer``. This is meant to be used internally++ :param streaming_result_generator: The generator that produces the streaming result+ :param initial_state: The initial state+ :param process_result: Function to process the result -- this gets called after the generator is exhausted, prior to returning the final result+ :param callback: Callback to call at the very end. This will only get called *once*, and will be called during the finally block of the generator+ """+ self.streaming_result_generator = streaming_result_generator+ self._action = action+ self._callback = callback+ self._process_result = process_result+ self._initial_state = initial_state+ self._result = None+ self._callback_realized = False++ def __iter__(self):+ return self.generator()++ def generator(self):+ try:+ if self._result is not None:+ # this means we're already thorugh it, we can just call it a day+ return+ result = yield from self.streaming_result_generator+ self._result = self._process_result(*result)+ finally:+ if self._result is None:+ # This way we always have something after we exhaust the generator+ self._result = None, self._initial_state+ if not self._callback_realized:+ exc = sys.exc_info()[1]+ # For now this will not be the right exception type (Generator close),+ # but its OK -- the exception is outside of our control fllow+ self._callback_realized = True+ self._callback(*self._result, exc)++ def get(self) -> Tuple[Optional[dict], State]:+ """Blocking call to get the final result of the streaming action. This will+ run through the entire generator (or until an exception is raised) and return+ the final result.++ :return: A tuple of the result and the new state+ """+ collections.deque(+ self, maxlen=0+ ) # exhausts the generator, producing the `_result` variable+ return self._result+++class SingleStepStreamingAction(SingleStepAction, abc.ABC):+ """Class to represent a "single-step" streaming action. This is meant to+ work with the functional API. Note this is not user-facing -- the user will+ only interact with this by using the ``@streaming_action`` decorator.+ """++ @abc.abstractmethod+ def stream_run_and_update(+ self, state: State, **run_kwargs+ ) -> Generator[dict, None, Tuple[dict, State]]:+ """Streaming version of the run and update function. This+ return type is a generator that streams in a result, has no "send"+ value, and returns the final result (new result + state).+ """+ pass++ def run_and_update(self, state: State, **run_kwargs) -> Tuple[dict, State]:+ """Runs the action and returns the final result. This allows us to run this as a+ single step action. This is helpful for when the streaming result needs to be+ run as an intermediate.+ """+ gen = self.stream_run_and_update(state, **run_kwargs)+ while True:+ try:+ next(gen) # if we just run through, we do nothing with the result+ except StopIteration as e:+ return e.value++ @property+ def streaming(self) -> bool:+ return True+++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,+ ):+ """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).++ :param fn: Function to use+ :param reads:+ :param writes:+ """+ super(FunctionBasedStreamingAction, self).__init__()+ self._fn = fn+ self._reads = reads+ self._writes = writes+ self._bound_params = bound_params if bound_params is not None else {}++ def stream_run_and_update(+ self, state: State, **run_kwargs+ ) -> Generator[dict, None, Tuple[dict, State]]:+ return (yield from self._fn(state, **self._bound_params, **run_kwargs))++ @property+ def reads(self) -> list[str]:+ return self._reads++ @property+ def writes(self) -> list[str]:+ return self._writes++ @property+ def streaming(self) -> bool:+ return True++ def with_params(self, **kwargs: Any) -> "FunctionBasedStreamingAction":+ return _with_params(self, **kwargs)++ @property+ def inputs(self) -> list[str]:+ return _get_inputs(self._bound_params, self._fn)++def _validate_action_function(fn: Callable):"""Validates that an action has the signature: (state: State) -> Tuple[dict, State]@@ -445,7 +702,7 @@ C = TypeVar("C", bound=Callable) # placeholder for any Callableclass FunctionRepresentingAction(Protocol[C]):- action_function: FunctionBasedAction+ action_function: FunctionBasedActionType__call__: Cdef bind(self, **kwargs: Any):@@ -496,6 +753,54 @@ def action(reads: List[str], writes: List[str]) -> Callable[[Callable], Functionreturn decorator+def streaming_action(+ reads: List[str], writes: List[str]+) -> Callable[[Callable], FunctionRepresentingAction]:+ """Decorator to create a streaming function-based action. This is user-facing.++ If parameters are not bound, they will be interpreted as inputs and must be passed in at runtime.++ See the following example for how to use this decorator -- this reads ``prompt`` from the state and writes+ ``response`` back out, yielding all intermediate chunks.++ Note that this *must* return a value. If it does not, we will not know how to update the state, and+ we will error out.++ .. code-block:: python++ @streaming_action(reads=["prompt"], writes=['response'])+ def streaming_response(state: State) -> Generator[dict, None, Tuple[dict, State]]:+ response = client.chat.completions.create(+ model='gpt-3.5-turbo',+ messages=[{+ 'role': 'user',+ 'content': state["prompt"]+ }],+ temperature=0,+ )+ buffer = []+ for chunk in response:+ delta = chunk.choices[0].delta.content+ buffer.append(delta)+ # yield partial results+ yield {'response': delta}+ full_response = ''.join(buffer)+ # return the final result+ return {'response': full_response}, state.update(response=full_response)+ """++ # TODO -- see if we want to consolidate this in some way with the above+ # Currently this is our only other way to use a decorator to implement actions, so it is OK to duplicate+ def decorator(fn) -> FunctionRepresentingAction:+ setattr(+ fn, FunctionBasedAction.ACTION_FUNCTION, FunctionBasedStreamingAction(fn, reads, writes)+ )+ setattr(fn, "bind", types.MethodType(bind, fn))+ return fn++ return decorator++def create_action(action_: Union[Callable, Action], name: str) -> Action:"""Factory function to create an action. This is meant to be called bythe ApplicationBuilder, and not by the user. The internal API may change.@@ -508,6 +813,6 @@ def create_action(action_: Union[Callable, Action], name: str) -> Action:action_ = getattr(action_, FunctionBasedAction.ACTION_FUNCTION)elif not isinstance(action_, Action):raise ValueError(- f"Object {action_} is not a valid action. Have you decorated it with @action?"+ f"Object {action_} is not a valid action. Have you decorated it with @action or @streaming_action?")return action_.with_name(name)diff --git a/burr/core/application.py b/burr/core/application.pyindex 1f67619c..746b23b3 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -15,6 +15,7 @@ from typing import (Set,Tuple,Union,+ cast,)from burr import visibility@@ -24,6 +25,9 @@ from burr.core.action import (Function,Reducer,SingleStepAction,+ SingleStepStreamingAction,+ StreamingAction,+ StreamingResultContainer,create_action,default,)@@ -182,6 +186,24 @@ def _run_single_step_action(return out+def _run_single_step_streaming_action(+ action: SingleStepStreamingAction, state: State, inputs: Optional[Dict[str, Any]]+) -> Generator[dict, None, Tuple[dict, State]]:+ action.validate_inputs(inputs)+ generator = action.stream_run_and_update(state, **inputs)+ return (yield from generator)+++def _run_multi_step_streaming_action(+ action: StreamingAction, state: State, inputs: Optional[Dict[str, Any]]+) -> Generator[dict, None, Tuple[dict, State]]:+ action.validate_inputs(inputs)+ generator = action.stream_run(state, **inputs)+ result = yield from generator+ new_state = _run_reducer(action, state, result, action.name)+ return result, _state_update(state, new_state)++async def _arun_single_step_action(action: SingleStepAction, state: State, inputs: Optional[Dict[str, Any]]) -> Tuple[dict, State]:@@ -254,7 +276,7 @@ class Application:self._increment_sequence_id()def _step(- self, inputs: Optional[Dict[str, Any]] = None, _run_hooks: bool = True+ self, inputs: Optional[Dict[str, Any]], _run_hooks: bool = True) -> Optional[Tuple[Action, dict, State]]:"""Internal-facing version of step. This is the same as step, but with an additionalparameter to hide hook execution so async can leverage it."""@@ -282,7 +304,7 @@ class Application:result = _run_function(next_action, self._state, inputs)new_state = _run_reducer(next_action, self._state, result, next_action.name)- new_state = self.update_internal_state_value(new_state, next_action)+ new_state = self._update_internal_state_value(new_state, next_action)self._set_state(new_state)except Exception as e:exc = e@@ -300,7 +322,7 @@ class Application:)return next_action, result, new_state- def update_internal_state_value(self, new_state: State, next_action: Action) -> State:+ def _update_internal_state_value(self, new_state: State, next_action: Action) -> State:"""Updates the internal state values of the new state."""new_state = new_state.update(**{@@ -377,7 +399,7 @@ class Application:else:result = await _arun_function(next_action, self._state, inputs=inputs)new_state = _run_reducer(next_action, self._state, result, next_action.name)- new_state = self.update_internal_state_value(new_state, next_action)+ new_state = self._update_internal_state_value(new_state, next_action)self._set_state(new_state)except Exception as e:exc = e@@ -512,7 +534,7 @@ class Application:halt_before: list[str] = None,halt_after: list[str] = None,inputs: Optional[Dict[str, Any]] = None,- ) -> AsyncGenerator[Tuple[Action, dict, State], Tuple[Action, Optional[dict], State]]:+ ) -> AsyncGenerator[Tuple[Action, dict, State], None]:"""Returns a generator that calls step() in a row, enabling you to see the stateof the system as it updates. This is the asynchronous version so it has no capability of t@@ -584,6 +606,224 @@ class Application:passreturn self._return_value_iterate(halt_before, halt_after, prior_action, result)+ def _validate_streaming_inputs(self, halt_after: list[str]):+ missing_actions = set(halt_after) - set([action.name for action in self._actions])+ # TODO -- implement this check elsewhere as well, break out into further utility functions+ if len(missing_actions) > 0:+ raise ValueError(+ f"Actions {missing_actions} were passed in as halt_after conditions, but not found in the actions list! "+ f"Actions found: {[action.name for action in self._actions]}"+ )++ def stream_result(+ self,+ halt_after: list[str],+ halt_before: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,+ ) -> Tuple[Action, StreamingResultContainer]:+ """Streams a result out.++ :param halt_after: The list of actions to halt after execution of. It will halt on the first one.+ :param halt_before: The list of actions to halt before execution of. It will halt on the first one. Note that+ if this is met, the streaming result container will be empty (and return None) for the result, having an empty generator.+ :param inputs: Inputs to the action -- this is if this action requires an input that is passed in from the outside world+ :return: A streaming result container, which is a generator that will yield results as they come in, as wel as cache/give you the final result, and update state accordingly.++ This is meant to be used with streaming actions -- :py:meth:`streaming_action <burr.core.action.streaming_action>`+ or :py:class:`StreamingAction <burr.core.action.StreamingAction>` It returns a+ :py:class:`StreamingResultContainer <burr.core.action.StreamingResultContainer>`, which has two capabilities:++ 1. It is a generator that streams out the intermediate results of the action+ 2. It has a ``.get()`` method that returns the final result of the action, and the final state.++ If ``.get()`` is called before the generator is exhausted, it will block until the generator is exhausted.++ While this container is meant to work with streaming actions, it can also be used with non-streaming actions. In this case,+ the generator will be empty, and the ``.get()`` method will return the final result and state.++ The rules for halt_before and halt_after are the same as for :py:meth:`iterate <burr.core.application.Application.iterate>`,+ and :py:meth:`run <burr.core.application.Application.run>`. In this case, `halt_before` will indicate a *non* streaming action,+ which will be empty. Thus ``halt_after`` takes precedence -- if it is met, the streaming result container will contain the result of the+ halt_after condition.++ The :py:class:`StreamingResultContainer <burr.core.action.StreamingResultContainer>` is meant as a convenience -- specifically this allows for+ hooks, callbacks, etc... so you can take the control flow and still have state updated afterwards. Hooks/state update will be called after an exception+ is thrown during streaming, or the stream is completed. Note that it is undefined behavior to attempt to execute another action while a stream is in progress.+++ To see how this works, let's take the following action (simplified as a single-node workflow) as an example:++ .. code-block:: python++ @streaming_action(reads=[], writes=['response'])+ def streaming_response(state: State, prompt: str) -> Generator[dict, None, Tuple[dict, State]]:+ response = client.chat.completions.create(+ model='gpt-3.5-turbo',+ messages=[{+ 'role': 'user',+ 'content': prompt+ }],+ temperature=0,+ )+ buffer = []+ for chunk in response:+ delta = chunk.choices[0].delta.content+ buffer.append(delta)+ # yield partial results+ yield {'response': delta}+ full_response = ''.join(buffer)+ # return the final result+ return {'response': full_response}, state.update(response=full_response)++ To use streaming_result, you pass in names of streaming actions (such as the one above) to the halt_after+ parameter:++ .. code-block:: python++ application = ApplicationBuilder().with_actions(streaming_response=streaming_response)...build()+ prompt = "Count to 100, with a comma between each number and no newlines. E.g., 1, 2, 3, ..."+ action, streaming_result = application.stream_result(halt_after='streaming_response', inputs={"prompt": prompt})+ for result in streaming_result:+ print(result['response']) # one by one++ result, state = streaming_result.get()+ print(result) # all at once++ Note that if you have multiple halt_after conditions, you can use the ``.action`` attribute to get the action that+ was run.++ .. code-block:: python++ application = ApplicationBuilder().with_actions(+ streaming_response=streaming_response,+ error=error # another function that outputs an error, streaming+ )...build()+ prompt = "Count to 100, with a comma between each number and no newlines. E.g., 1, 2, 3, ..."+ action, streaming_result = application.stream_result(halt_after='streaming_response', inputs={"prompt": prompt})+ color = "red" if action.name == "error" else "green"+ for result in streaming_result:+ print(format(result['response'], color)) # assumes that error and streaming_response both have the same output shape++ .. code-block:: python++ application = ApplicationBuilder().with_actions(+ streaming_response=streaming_response,+ error=non_streaming_error # a non-streaming function that outputs an error+ )...build()+ prompt = "Count to 100, with a comma between each number and no newlines. E.g., 1, 2, 3, ..."+ action, streaming_result = application.stream_result(halt_after='streaming_response', inputs={"prompt": prompt})+ color = "red" if action.name == "error" else "green"+ if action.name == "streaming_response": # can also use the ``.streaming`` attribute of action+ for result in output:+ print(format(result['response'], color)) # assumes that error and streaming_response both have the same output shape+ else:+ result, state = output.get()+ print(format(result['response'], color))+ """+ halt_before, halt_after, inputs = self._clean_iterate_params(+ halt_before, halt_after, inputs+ )++ self._validate_streaming_inputs(halt_after)+ next_action = self.get_next_action()+ if next_action is None:+ raise ValueError(+ f"Cannot stream result -- no next action found! Prior action was: {self._state[PRIOR_STEP]}"+ )++ if next_action.name not in halt_after:+ # fast forward until we get to the action+ next_action, results, state = self.run(+ halt_before=halt_after + halt_before, inputs=inputs+ )+ # In this case, we are ready to halt and return an empty generator+ # The results will be None, and the state will be the final state+ # For context, this is specifically for the case in which you want to have+ # multiple terminal points with a unified API, where some are streaming, and some are not.+ if next_action.name in halt_before and next_action.name not in halt_after:+ return next_action, StreamingResultContainer.pass_through(+ results=results, final_state=state+ )+ inputs = {} # inputs always go to the first action, we want to wipe them afterwards+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "pre_run_step",+ action=next_action,+ state=self._state,+ inputs=inputs,+ sequence_id=self.sequence_id,+ )+ # we need to track if there's any exceptions that occur during this+ try:++ def process_result(result: dict, state: State) -> Tuple[Dict[str, Any], State]:+ new_state = self._update_internal_state_value(state, next_action)+ self._set_state(new_state)+ return result, new_state++ def callback(+ result: Optional[dict],+ state: State,+ exc: Optional[Exception] = None,+ seq_id=self.sequence_id,+ ):+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "post_run_step",+ action=next_action,+ state=state,+ result=result,+ sequence_id=seq_id,+ exception=exc,+ )+ # we want to increment regardless of failure+ self._increment_sequence_id()++ if not next_action.streaming:+ # In this case we are halting at a non-streaming condition+ # This is allowed as we want to maintain a more consistent API+ action, result, state = self._step(inputs=inputs, _run_hooks=False)+ return action, StreamingResultContainer.pass_through(+ results=result, final_state=state+ )++ if next_action.single_step:+ next_action = cast(SingleStepStreamingAction, next_action)+ generator = _run_single_step_streaming_action(next_action, self._state, inputs)+ return next_action, StreamingResultContainer(+ generator, self._state, process_result, callback+ )+ else:+ next_action = cast(StreamingAction, next_action)+ generator = _run_multi_step_streaming_action(next_action, self._state, inputs)+ except Exception as e:+ # We only want to raise this in the case of an exception+ # otherwise, this will get delegated to the finally+ # block of the streaming result container+ self._adapter_set.call_all_lifecycle_hooks_sync(+ "post_run_step",+ action=next_action,+ state=self._state,+ result=None,+ sequence_id=self.sequence_id,+ exception=e,+ )+ self._increment_sequence_id()+ raise+ return next_action, StreamingResultContainer(+ generator, self._state, process_result, callback+ )++ async def astream_result(+ self,+ halt_after: list[str],+ halt_before: list[str] = None,+ inputs: Optional[Dict[str, Any]] = None,+ ) -> Tuple[Action, ...]:+ """Placeholder for the async version of stream_result. This is not yet implemented."""+ raise NotImplementedError(+ "This has not yet been implemented! See the github issue: https://github.com/DAGWorks-Inc/burr/issues/64"+ " for details. Please comment or vote to get it implemented quickly!"+ )+def visualize(self,output_file_path: Optional[str],diff --git a/tests/core/test_action.py b/tests/core/test_action.pyindex 45b9be73..27961644 100644--- a/tests/core/test_action.py+++ b/tests/core/test_action.py@@ -1,5 +1,5 @@import asyncio-from typing import Tuple+from typing import Generator, Tupleimport pytest@@ -11,10 +11,12 @@ from burr.core.action import (Input,Result,SingleStepAction,+ SingleStepStreamingAction,_validate_action_function,action,create_action,default,+ streaming_action,)@@ -307,6 +309,36 @@ def test_create_action_fn_api_with_bind():}+def test_create_action_streaming_fn_api_with_bind():+ @streaming_action(reads=["input_variable"], writes=["output_variable"])+ def test_action(state: State, prefix: str) -> Generator[dict, None, Tuple[dict, State]]:+ buffer = []+ for c in prefix + state["input_variable"]:+ buffer.append(c)+ yield {"output_variable": c} # intermediate results+ result = {"output_variable": "".join(buffer)}+ return result, state.update(output_variable=result["output_variable"])++ created_action = create_action(test_action.bind(prefix="prefix_"), name="my_action")+ assert created_action.streaming+ assert isinstance(created_action, SingleStepStreamingAction)+ assert created_action.name == "my_action"+ assert created_action.reads == ["input_variable"]+ assert created_action.writes == ["output_variable"]+ assert created_action.single_step+ gen = created_action.stream_run_and_update(State({"input_variable": "foo"}))+ out = []+ while True:+ try:+ out.append(next(gen)["output_variable"])+ except StopIteration as e:+ result, state = e.value+ assert result == {"output_variable": "prefix_foo"}+ assert state.get_all() == {"input_variable": "foo", "output_variable": "prefix_foo"}+ break+ assert out == list("prefix_foo")++def test_create_action_undecorated_function():def test_action(state: State) -> Tuple[dict, State]:result = {"output_variable": state["input_variable"]}