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 basic counter example
elijahbenizzy77fc73e4df5e
diff --git a/examples/counter/application.py b/examples/counter/application.pynew file mode 100644index 00000000..9fe3db26--- /dev/null+++ b/examples/counter/application.py@@ -0,0 +1,45 @@+import burr.core+from burr.core import Action, Result, State, default, expr+from burr.lifecycle import StateAndResultsFullLogger+++class CounterAction(Action):+ @property+ def reads(self) -> list[str]:+ return ["counter"]++ def run(self, state: State) -> dict:+ return {"counter": state["counter"] + 1}++ @property+ def writes(self) -> list[str]:+ return ["counter"]++ def update(self, result: dict, state: State) -> State:+ return state.update(**result)+++def application(count_up_to: int = 10, log_file: str = None):+ return (+ burr.core.ApplicationBuilder()+ .with_state(+ counter=0,+ )+ .with_actions(counter=CounterAction(), result=Result(["counter"]))+ .with_transitions(+ ("counter", "counter", expr(f"counter < {count_up_to}")),+ ("counter", "result", default),+ ("result", "counter", expr("counter == 0")), # when we've reset, manually+ )+ .with_entrypoint("counter")+ .with_hooks(*[StateAndResultsFullLogger(log_file)] if log_file else [])+ .build()+ )+++if __name__ == "__main__":+ app = application(log_file="counter.jsonl")+ state, result = app.run(until=["result"])+ app.visualize(output_file_path="counter.png", include_conditions=True, view=True)+ assert state["counter"] == 10+ print(state["counter"])diff --git a/examples/counter/requirements.txt b/examples/counter/requirements.txtnew file mode 100644index 00000000..1670c9d8--- /dev/null+++ b/examples/counter/requirements.txt@@ -0,0 +1 @@+dw-burr[streamlit]diff --git a/examples/counter/streamlit_app.py b/examples/counter/streamlit_app.pynew file mode 100644index 00000000..6d9a7002--- /dev/null+++ b/examples/counter/streamlit_app.py@@ -0,0 +1,64 @@+import counter+import streamlit as st++from burr.integrations.streamlit import (+ AppState,+ Record,+ get_state,+ render_explorer,+ set_slider_to_current,+ update_state,+)+++def counter_view(app_state: AppState):+ application = app_state.app+ button = st.button("Forward", use_container_width=True)+ if button:+ step_output = application.step()+ if step_output is not None:+ action, result, state = step_output+ app_state.history.append(Record(state.get_all(), action.name, result))+ set_slider_to_current()+ else:+ application.update_state(application.state.update(counter=0))+ action, result, state = application.step()+ app_state.history.append(Record(state.get_all(), action.name, result))+++def retrieve_state():+ if "burr_state" not in st.session_state:+ state = AppState.from_empty(app=counter.application())+ else:+ state = get_state()+ return state+++def main():+ st.set_page_config(layout="wide")+ st.title("Counting numbers with Burr")+ app_state = retrieve_state() # retrieve first so we can use for the ret of the step+ columns = st.columns(2)+ with columns[0]:+ st.write(+ "This is a simple counter app. It counts to 10, then loops back to 0. You can reset it at any time. "+ "While we know that this is easy to do with a simple loop + streamlit, it highlights the state that Burr manages."+ "Use the slider to rewind/see what happened in the past, and the visualizations to understand how we navigate "+ "through the state machine!"+ )+ counter_view(app_state)+ with st.container(height=800):+ md_lines = []+ for item in app_state.history:+ if item.action == "counter":+ md_lines.append(f"Counted to {item.state['counter']}!")+ else:+ md_lines.append("Looping back! ")+ st.code("\n".join(md_lines), language=None)+ with columns[1]:+ render_explorer(app_state)+ update_state(app_state) # update so the next iteration knows what to do+++if __name__ == "__main__":+ main()