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 cowsay example
elijahbenizzya5567f418a9d
diff --git a/examples/cowsay/application.py b/examples/cowsay/application.pynew file mode 100644index 00000000..7f7f014d--- /dev/null+++ b/examples/cowsay/application.py@@ -0,0 +1,103 @@+import random+import time+from typing import List, Optional++import cowsay++from burr.core import Action, Application, ApplicationBuilder, State, default, expr+from burr.lifecycle import PostRunStepHook+++class CowSay(Action):+ def __init__(self, say_what: List[Optional[str]]):+ super(CowSay, self).__init__()+ self.say_what = say_what++ @property+ def reads(self) -> list[str]:+ return []++ def run(self, state: State) -> dict:+ say_what = random.choice(self.say_what)+ return {+ "cow_said": cowsay.get_output_string("cow", say_what) if say_what is not None else None+ }++ @property+ def writes(self) -> list[str]:+ return ["cow_said"]++ def update(self, result: dict, state: State) -> State:+ return state.update(**result)+++class CowShouldSay(Action):+ @property+ def reads(self) -> list[str]:+ return []++ def run(self, state: State) -> dict:+ if not random.randint(0, 3):+ return {"cow_should_speak": True}+ return {"cow_should_speak": False}++ @property+ def writes(self) -> list[str]:+ return ["cow_should_speak"]++ def update(self, result: dict, state: State) -> State:+ return state.update(**result)+++class PrintWhatTheCowSaid(PostRunStepHook):+ def post_run_step(self, *, state: "State", action: "Action", **future_kwargs):+ if action.name != "cow_should_say" and state["cow_said"] is not None:+ print(state["cow_said"])+++class CowCantSpeakFast(PostRunStepHook):+ def __init__(self, sleep_time: float):+ super(PostRunStepHook, self).__init__()+ self.sleep_time = sleep_time++ def post_run_step(self, *, state: "State", action: "Action", **future_kwargs):+ if action.name != "cow_should_say": # no need to print if we're not saying anything+ time.sleep(self.sleep_time)+++def application(in_terminal: bool = False) -> Application:+ hooks = (+ [+ PrintWhatTheCowSaid(),+ CowCantSpeakFast(sleep_time=2.0),+ ]+ if in_terminal+ else []+ )+ return (+ ApplicationBuilder()+ .with_state(+ cow_said=None,+ )+ .with_actions(+ say_nothing=CowSay([None]),+ say_hello=CowSay(["Hello world!", "What's up?", "Are you Aaron Burr, sir?"]),+ cow_should_say=CowShouldSay(),+ )+ .with_transitions(+ ("cow_should_say", "say_hello", expr("cow_should_speak")),+ ("say_hello", "cow_should_say", default),+ ("cow_should_say", "say_nothing", expr("not cow_should_speak")),+ ("say_nothing", "cow_should_say", default),+ )+ .with_entrypoint("cow_should_say")+ .with_hooks(*hooks)+ .build()+ )+++if __name__ == "__main__":+ app = application(in_terminal=True)+ app.visualize(output_file_path="cowsay.png", include_conditions=True, view=True)+ while True:+ state, result, action = app.step()diff --git a/examples/cowsay/requirements.txt b/examples/cowsay/requirements.txtnew file mode 100644index 00000000..3155b767--- /dev/null+++ b/examples/cowsay/requirements.txt@@ -0,0 +1,2 @@+cowsay+dw-burr[streamlit]diff --git a/examples/cowsay/streamlit_app.py b/examples/cowsay/streamlit_app.pynew file mode 100644index 00000000..8db01336--- /dev/null+++ b/examples/cowsay/streamlit_app.py@@ -0,0 +1,69 @@+import application as cowsay_application+import streamlit as st++from burr.integrations.streamlit import (+ AppState,+ Record,+ get_state,+ render_explorer,+ set_slider_to_current,+ update_state,+)+++def cow_say_view(app_state: AppState):+ application = app_state.app+ button = st.button("Cow say?", use_container_width=True)+ if button:+ step_output = application.step()+ action, result, state = step_output+ app_state.history.append(Record(state.get_all(), action.name, result))+ set_slider_to_current()+++def render_cow_said(record: Record):+ cow_said = record.state.get("cow_said")+ cow_should_speak = record.state.get("cow_should_speak")+ with st.chat_message("cow", avatar="🐮" if cow_should_speak and cow_said else "💭"):+ grey = "#A9A9A9"+ if record.action == "cow_should_say":+ if cow_should_speak:+ st.markdown(+ f"<p style='color: {grey};'>Cow will not speak.</p>", unsafe_allow_html=True+ )+ else:+ st.markdown(+ f"<p style='color: {grey};'>Cow will not speak.</p>", unsafe_allow_html=True+ )+ else:+ if cow_said is None:+ st.markdown(f"<p style='color: {grey};'>...</p>", unsafe_allow_html=True)+ if cow_said is not None:+ st.code(cow_said, language="plaintext")+++def retrieve_state():+ if "burr_state" not in st.session_state:+ state = AppState.from_empty(app=cowsay_application.application())+ else:+ state = get_state()+ return state+++def main():+ st.set_page_config(layout="wide")+ st.title("Talking cows 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]:+ cow_say_view(app_state)+ with st.container(height=800):+ for item in app_state.history:+ render_cow_said(item)+ with columns[1]:+ render_explorer(app_state)+ update_state(app_state) # update so the next iteration knows what to do+++if __name__ == "__main__":+ main()