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
Added load to return typeddict and saving an error
Stefan Krawczykd03b8eb96aed
diff --git a/burr/core/application.py b/burr/core/application.pyindex 2acc2b1e..5f56d4ff 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -748,7 +748,7 @@ class Application:raise ValueError(f"Cannot stream result -- no next action found! Prior action was: {self._state[PRIOR_STEP]}")-+ self._increment_sequence_id() # TODO: is this in the right place?if next_action.name not in halt_after:# fast forward until we get to the actionnext_action, results, state = self.run(@@ -842,7 +842,6 @@ class Application:sequence_id=self.sequence_id,exception=e,)- self._increment_sequence_id()raisereturn next_action, StreamingResultContainer(generator, self._state, process_result, callback@@ -1236,7 +1235,7 @@ class ApplicationBuilder:if persister not in self.lifecycle_adapters:self.lifecycle_adapters.append(persister)else:- # TODO: wrap in one+ # TODO: pull this out more formallyclass persisterWrapper(PostRunStepHook):def post_run_step(self,@@ -1250,7 +1249,13 @@ class ApplicationBuilder:**future_kwargs: Any,):if exception is None:- persister.save(partition_key, app_id, sequence_id, action.name, state)+ persister.save(+ partition_key, app_id, sequence_id, action.name, state, "completed"+ )+ else:+ persister.save(+ partition_key, app_id, sequence_id, action.name, state, "failed"+ )self.lifecycle_adapters.append(persisterWrapper())return self@@ -1259,13 +1264,14 @@ class ApplicationBuilder:def build(self) -> Application:"""Builds the application.+ This function is a bit messy as we iron out the exact logic and rigor we want around things.+:return: The application object"""_validate_actions(self.actions)actions_by_name = {action.name: action for action in self.actions}all_actions = set(actions_by_name.keys())_validate_transitions(self.transitions, all_actions)- _validate_start(self.start, all_actions)_validate_app_id(self.app_id)loaded_sequence_id = None@@ -1275,9 +1281,21 @@ class ApplicationBuilder:if load_result is None:self.state = self.state.update(**self.default_state)else:- last_position, self.state, loaded_sequence_id = load_result+ last_position = load_result["position"]+ self.state = load_result["state"]+ loaded_sequence_id = load_result["sequence_id"]+ status = load_result["status"]if self.resume_at_next_action:- self.state = self.state.update(**{PRIOR_STEP: last_position})+ if status == "completed":+ self.state = self.state.update(**{PRIOR_STEP: last_position})+ else:+ # if failed we just start at that node+ self.start = last_position+ self.state = self.state.wipe(delete=[PRIOR_STEP])+ else:+ # self.start is already set to the default. We don't need to do anything.+ pass+ _validate_start(self.start, all_actions)if self.sequence_id is None:self.sequence_id = loaded_sequence_id if loaded_sequence_id else 0diff --git a/burr/core/state.py b/burr/core/state.pyindex 8dc3db84..6aab1e1a 100644--- a/burr/core/state.py+++ b/burr/core/state.py@@ -4,7 +4,7 @@ import dataclassesimport jsonimport loggingimport sqlite3-from typing import Any, Dict, Iterator, Mapping, Optional, Tuple+from typing import Any, Dict, Iterator, Literal, Mapping, Optional, TypedDictlogger = logging.getLogger(__name__)@@ -121,6 +121,7 @@ class DeleteField(StateDelta):def apply_mutate(self, inputs: dict):for key in self.keys:inputs.pop(key, None)+ print(inputs)class State(Mapping):@@ -200,6 +201,16 @@ class State(Mapping):return self.get_all().__repr__() # quick hack+class PersistenceDict(TypedDict):+ partition_key: str+ app_id: str+ sequence_id: int+ position: str+ state: State+ created_at: str+ status: str++class BasicStatePersistence(abc.ABC):"""Basic Interface for state persistence.@@ -218,7 +229,7 @@ class BasicStatePersistence(abc.ABC):@abc.abstractmethoddef load(self, partition_key: str, app_id: Optional[str], sequence_id: Optional[int] = None- ) -> Optional[Tuple[str, State, int]]:+ ) -> Optional[PersistenceDict]:"""Loads the state for a given app_id:param partition_key: the partition key. Note this could be None, but it's up to the persistor to whether@@ -238,6 +249,7 @@ class BasicStatePersistence(abc.ABC):sequence_id: int,position: str,state: State,+ status: Literal["completed", "failed"],):"""Saves the state for a given app_id, sequence_id, position@@ -251,6 +263,7 @@ class BasicStatePersistence(abc.ABC)::param sequence_id::param position::param state:+ :param status:"""pass@@ -280,9 +293,10 @@ class SQLLitePersistence(BasicStatePersistence):app_id TEXT NOT NULL,sequence_id INTEGER NOT NULL,position TEXT NOT NULL,+ status TEXT NOT NULL,state TEXT NOT NULL,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,- PRIMARY KEY (partition_key, app_id, sequence_id, position)+ PRIMARY KEY (partition_key, app_id, sequence_id, position, state))""")cursor.execute(@@ -313,7 +327,7 @@ class SQLLitePersistence(BasicStatePersistence):def load(self, partition_key: str, app_id: str, sequence_id: int = None- ) -> Optional[Tuple[str, State, int]]:+ ) -> Optional[PersistenceDict]:"""Loads state for a given partition id.Depending on the parameters, this will return the last thing written, the last thing written for a given app_id,@@ -330,21 +344,21 @@ class SQLLitePersistence(BasicStatePersistence):if app_id is None:# get latest for all app_idscursor.execute(- f"SELECT position, state, sequence_id, app_id, created_at FROM {self.table_name} "+ f"SELECT position, state, sequence_id, app_id, created_at, status FROM {self.table_name} "f"WHERE partition_key = ? "f"ORDER BY CREATED_AT DESC LIMIT 1",(partition_key,),)elif sequence_id is None:cursor.execute(- f"SELECT position, state, sequence_id, created_at FROM {self.table_name} "+ f"SELECT position, state, sequence_id, app_id, created_at, status FROM {self.table_name} "f"WHERE partition_key = ? AND app_id = ? "f"ORDER BY sequence_id DESC LIMIT 1",(partition_key, app_id),)else:cursor.execute(- f"SELECT position, state, seqeuence_id, created_at FROM {self.table_name} "+ f"SELECT position, state, seqeuence_id, app_id, created_at, status FROM {self.table_name} "f"WHERE partition_key = ? AND app_id = ? AND sequence_id = ?",(partition_key, app_id, sequence_id),)@@ -353,9 +367,25 @@ class SQLLitePersistence(BasicStatePersistence):if row is None:return None_state = State(json.loads(row[1])["_state"])- return row[0], _state, row[2]+ return {+ "partition_key": partition_key,+ "app_id": row[3],+ "sequence_id": row[2],+ "position": row[0],+ "state": _state,+ "created_at": row[4],+ "status": row[5],+ }- def save(self, partition_key: str, app_id: str, sequence_id: int, position: str, state: State):+ def save(+ self,+ partition_key: str,+ app_id: str,+ sequence_id: int,+ position: str,+ state: State,+ status: Literal["completed", "failed"],+ ):"""Saves the state for a given app_id, sequence_id, and position.@@ -369,18 +399,26 @@ class SQLLitePersistence(BasicStatePersistence)::param sequence_id: The state corresponding to a specific point in time.:param position: The position in the sequence of states.:param state: The state to be saved, an instance of the State class.+ :param status: The status of this state, either "completed" or "failed". If "failed" the state is what it was+ before the action was applied.:return: None"""logger.debug(- "saving %s, %s, %s, %s, %s", partition_key, app_id, sequence_id, position, state+ "saving %s, %s, %s, %s, %s, %s",+ partition_key,+ app_id,+ sequence_id,+ position,+ state,+ status,)conn = sqlite3.connect(self.db_path)cursor = conn.cursor()json_state = json.dumps(state.__dict__)cursor.execute(- f"INSERT INTO {self.table_name} (partition_key, app_id, sequence_id, position, state) "- f"VALUES (?, ?, ?, ?, ?)",- (partition_key, app_id, sequence_id, position, json_state),+ f"INSERT INTO {self.table_name} (partition_key, app_id, sequence_id, position, state, status) "+ f"VALUES (?, ?, ?, ?, ?, ?)",+ (partition_key, app_id, sequence_id, position, json_state, status),)conn.commit()conn.close()