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.

Fixes up result validation for single step actions

elijahbenizzy89a5ec4b5a6e
diff --git a/burr/core/application.py b/burr/core/application.py
index 62f33557..552fa267 100644
--- a/burr/core/application.py
+++ b/burr/core/application.py
@@ -77,17 +77,27 @@ def _validate_result(result: dict, name: str) -> None:
)
-def _adjust_single_step_output(output: Union[State, Tuple[dict, State]]):
- """Adjusts the output of a single step action to be a tuple of (result, state)"""
+def _raise_fn_return_validation_error(output: Any, action_name: str):
+ raise ValueError(
+ ERROR_MESSAGE
+ + f"Single step action: {action_name} must return either *just* the newly updated State or a tuple of (result: dict, state: State). "
+ f"Got: {output} of type {type(output)} instead, which is not valid."
+ )
+
+
+def _adjust_single_step_output(output: Union[State, Tuple[dict, State]], action_name: str):
+ """Adjusts the output of a single step action to be a tuple of (result, state) or just state"""
+
if isinstance(output, tuple):
+ if not len(output) == 2:
+ _raise_fn_return_validation_error(output, action_name)
+ _validate_result(output[0], action_name)
+ if not isinstance(output[1], State):
+ _raise_fn_return_validation_error(output, action_name)
return output
if isinstance(output, State):
return {}, output
- raise ValueError(
- ERROR_MESSAGE
- + "Single step action must return either *just* the state update or a tuple of (result, state). "
- f"Got: {output} of type {type(output)} instead."
- )
+ _raise_fn_return_validation_error(output, action_name)
def _run_function(function: Function, state: State, inputs: Dict[str, Any], name: str) -> dict:
@@ -236,7 +246,9 @@ def _run_single_step_action(
"""
# TODO -- guard all reads/writes with a subset of the state
action.validate_inputs(inputs)
- result, new_state = _adjust_single_step_output(action.run_and_update(state, **inputs))
+ result, new_state = _adjust_single_step_output(
+ action.run_and_update(state, **inputs), action.name
+ )
_validate_result(result, action.name)
out = result, _state_update(state, new_state)
_validate_result(result, action.name)
@@ -359,7 +371,7 @@ async def _arun_single_step_action(
state_to_use = state
action.validate_inputs(inputs)
result, new_state = _adjust_single_step_output(
- await action.run_and_update(state_to_use, **inputs)
+ await action.run_and_update(state_to_use, **inputs), action.name
)
_validate_result(result, action.name)
_validate_reducer_writes(action, new_state, action.name)
diff --git a/examples/multi-modal-chatbot/application.py b/examples/multi-modal-chatbot/application.py
index d037a41c..e8a0f0f5 100644
--- a/examples/multi-modal-chatbot/application.py
+++ b/examples/multi-modal-chatbot/application.py
@@ -18,7 +18,7 @@ MODES = {
@action(reads=[], writes=["chat_history", "prompt"])
-def process_prompt(state: State, prompt: str):
+def process_prompt(state: State, prompt: str) -> State:
result = {"chat_item": {"role": "user", "content": prompt, "type": "text"}}
return (
state.wipe(keep=["prompt", "chat_history"])
@@ -28,13 +28,13 @@ def process_prompt(state: State, prompt: str):
@action(reads=[], writes=["has_openai_key"])
-def check_openai_key(state: State):
+def check_openai_key(state: State) -> State:
result = {"has_openai_key": "OPENAI_API_KEY" in os.environ}
return state.update(**result)
@action(reads=["prompt"], writes=["safe"])
-def check_safety(state: State):
+def check_safety(state: State) -> State:
result = {"safe": "unsafe" not in state["prompt"]} # quick hack to demonstrate
return state.update(safe=result["safe"])
@@ -44,7 +44,7 @@ def _get_openai_client():
@action(reads=["prompt"], writes=["mode"])
-def choose_mode(state: State):
+def choose_mode(state: State) -> State:
prompt = (
f"You are a chatbot. You've been prompted this: {state['prompt']}. "
f"You have the capability of responding in the following modes: {', '.join(MODES)}. "
@@ -70,7 +70,7 @@ def choose_mode(state: State):
@action(reads=["prompt", "chat_history"], writes=["response"])
-def prompt_for_more(state: State):
+def prompt_for_more(state: State) -> State:
result = {
"response": {
"content": "None of the response modes I support apply to your question. Please clarify?",
@@ -84,7 +84,7 @@ def prompt_for_more(state: State):
@action(reads=["prompt", "chat_history", "mode"], writes=["response"])
def chat_response(
state: State, prepend_prompt: str, display_type: str = "text", model: str = "gpt-3.5-turbo"
-):
+) -> State:
chat_history = copy.deepcopy(state["chat_history"])
chat_history[-1]["content"] = f"{prepend_prompt}: {chat_history[-1]['content']}"
chat_history_api_format = [
@@ -105,7 +105,7 @@ def chat_response(
@action(reads=["prompt", "chat_history", "mode"], writes=["response"])
-def image_response(state: State, model: str = "dall-e-2"):
+def image_response(state: State, model: str = "dall-e-2") -> State:
"""Generates an image response to the prompt. Optional save function to save the image to a URL."""
client = _get_openai_client()
result = client.images.generate(
@@ -117,7 +117,7 @@ def image_response(state: State, model: str = "dall-e-2"):
@action(reads=["response", "mode", "safe", "has_openai_key"], writes=["chat_history"])
-def response(state: State):
+def response(state: State) -> State:
if not state["has_openai_key"]:
result = {
"chat_item": {
diff --git a/tests/core/test_application.py b/tests/core/test_application.py
index e55dd0b4..31faa15d 100644
--- a/tests/core/test_application.py
+++ b/tests/core/test_application.py
@@ -27,6 +27,7 @@ from burr.core.application import (
ApplicationBuilder,
ApplicationContext,
Transition,
+ _adjust_single_step_output,
_arun_function,
_arun_multi_step_streaming_action,
_arun_single_step_action,
@@ -1918,7 +1919,31 @@ def test__validate_actions_empty():
_validate_actions([])
-def test__asset_set():
+def test__adjust_single_step_output_result_and_state():
+ state = State({"count": 1})
+ result = {"count": 1}
+ assert _adjust_single_step_output((result, state), "test_action") == (result, state)
+
+
+def test__adjust_single_step_output_just_state():
+ state = State({"count": 1})
+ assert _adjust_single_step_output(state, "test_action") == ({}, state)
+
+
+def test__adjust_single_step_output_errors_incorrect_type():
+ state = "foo"
+ with pytest.raises(ValueError, match="must return either"):
+ _adjust_single_step_output(state, "test_action")
+
+
+def test__adjust_single_step_output_errors_incorrect_result_type():
+ state = State()
+ result = "bar"
+ with pytest.raises(ValueError, match="non-dict"):
+ _adjust_single_step_output((state, result), "test_action")
+
+
+def test__assert_set():
_assert_set("foo", "foo", "bar")