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
WIP -- adds to tracker
elijahbenizzy673da06e37bf
diff --git a/burr/core/application.py b/burr/core/application.pyindex 930eda71..46a29b22 100644--- a/burr/core/application.py+++ b/burr/core/application.py@@ -280,7 +280,7 @@ def _run_single_step_streaming_action(item_index=count,stream_initialize_time=stream_initialize_time,first_stream_item_start_time=first_stream_start_time,- action=action,+ action=action.name,app_id=app_id,partition_key=partition_key,sequence_id=sequence_id,@@ -332,7 +332,7 @@ async def _arun_single_step_streaming_action(item_index=count,stream_initialize_time=stream_initialize_time,first_stream_item_start_time=first_stream_start_time,- action=action,+ action=action.name,app_id=app_id,partition_key=partition_key,sequence_id=sequence_id,@@ -387,7 +387,7 @@ def _run_multi_step_streaming_action(item_index=count,stream_initialize_time=stream_initialize_time,first_stream_item_start_time=first_stream_start_time,- action=action,+ action=action.name,app_id=app_id,partition_key=partition_key,sequence_id=sequence_id,@@ -431,7 +431,7 @@ async def _arun_multi_step_streaming_action(stream_initialize_time=stream_initialize_time,item_index=count,first_stream_item_start_time=first_stream_start_time,- action=action,+ action=action.name,app_id=app_id,partition_key=partition_key,sequence_id=sequence_id,@@ -1358,7 +1358,7 @@ class Application:"post_run_step",app_id=self._uid,partition_key=self._partition_key,- action=next_action,+ action=next_action.name,state=self._state,result=result,sequence_id=self.sequence_id,@@ -1380,7 +1380,7 @@ class Application:if next_action.single_step:next_action = cast(SingleStepStreamingAction, next_action)generator = _run_single_step_streaming_action(- action=next_action,+ action=next_action.name,state=self._state,inputs=action_inputs,sequence_id=self.sequence_id,@@ -1394,7 +1394,7 @@ class Application:else:next_action = cast(StreamingAction, next_action)generator = _run_multi_step_streaming_action(- action=next_action,+ action=next_action.name,state=self._state,inputs=action_inputs,sequence_id=self.sequence_id,diff --git a/burr/tracking/client.py b/burr/tracking/client.pyindex 02b2039c..0a093a14 100644--- a/burr/tracking/client.py+++ b/burr/tracking/client.py@@ -1,7 +1,13 @@import abc+import dataclassesimport datetime-from burr.lifecycle.base import DoLogAttributeHook+from burr.lifecycle.base import (+ DoLogAttributeHook,+ PostEndStreamHook,+ PostStreamItemHook,+ PreStartStreamHook,+)# this is a quick hack to get it to work on windows# we'll have to implement a proper lock later@@ -25,7 +31,7 @@ import osimport reimport tracebackfrom abc import ABC-from typing import Any, Dict, Optional+from typing import Any, Dict, Optional, Tupletry:from typing import Self@@ -53,6 +59,9 @@ from burr.tracking.common.models import (ChildApplicationModel,EndEntryModel,EndSpanModel,+ EndStreamModel,+ FirstItemStreamModel,+ InitializeStreamModel,PointerModel,)from burr.visibility import ActionSpan@@ -91,6 +100,15 @@ def _allowed_project_name(project_name: str, on_windows: bool) -> bool:return bool(re.match(pattern, project_name))+class StreamState:+ stream_init_time: datetime.datetime+ count: Optional[int]+++StateKey = Tuple[str, str, Optional[str]]++class SyncTrackingClient(PostApplicationCreateHook,PreRunStepHook,@@ -98,6 +116,9 @@ class SyncTrackingClient(PreStartSpanHook,PostEndSpanHook,DoLogAttributeHook,+ PreStartStreamHook,+ PostStreamItemHook,+ PostEndStreamHook,ABC,):@abc.abstractmethod@@ -159,6 +180,8 @@ class LocalTrackingClient(self.storage_dir = LocalTrackingClient.get_storage_path(project, storage_dir)self.project_id = projectself.serde_kwargs = serde_kwargs or {}+ # app_id, action, partition_key -> stream data so we can track+ self.stream_state: Dict[StateKey, StreamState] = dict()def _log_child_relationships(self,@@ -477,6 +500,73 @@ class LocalTrackingClient()self._append_write_line(attribute_model)+ def pre_start_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ self._append_write_line(+ InitializeStreamModel(+ action_sequence_id=sequence_id,+ span_id=None,+ stream_init_time=system.now(),+ )+ )+ self.stream_state[app_id, action, partition_key] = StreamState(+ stream_init_time=system.now(),+ count=0,+ )++ def post_stream_item(+ self,+ *,+ item: Any,+ item_index: int,+ stream_initialize_time: datetime.datetime,+ first_stream_item_start_time: datetime.datetime,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ stream_state = self.stream_state[app_id, action, partition_key]+ if stream_state.count == 0:+ stream_state.count += 1+ self._append_write_line(+ FirstItemStreamModel(+ action_sequence_id=sequence_id,+ span_id=None,+ first_item_time=system.now(),+ )+ )+ else:+ stream_state.count += 1++ def post_end_stream(+ self,+ *,+ action: str,+ sequence_id: int,+ app_id: str,+ partition_key: Optional[str],+ **future_kwargs: Any,+ ):+ stream_state = self.stream_state[app_id, action, partition_key]+ self._append_write_line(+ EndStreamModel(+ action_sequence_id=sequence_id,+ span_id=None,+ end_time=system.now(),+ items_streamed=stream_state.count,+ )+ )+ del self.stream_state[app_id, action, partition_key]+def __del__(self):if self.f is not None:self.f.close()diff --git a/burr/tracking/common/models.py b/burr/tracking/common/models.pyindex 357ef0d2..e73ab00d 100644--- a/burr/tracking/common/models.py+++ b/burr/tracking/common/models.py@@ -210,3 +210,49 @@ class AttributeModel(IdentifyingModel):def sequence_id(self) -> int:# Ditto with the abovereturn self.action_sequence_id+++class InitializeStreamModel(IdentifyingModel):+ """Pydantic model that represents an entry for the beginning of a stream"""++ action_sequence_id: int+ span_id: Optional[+ str+ ] # It doesn't have to relate to a span, but if it was started in a span, this is the span_id+ stream_init_time: datetime.datetime+ type: str = "begin_stream"++ @property+ def sequence_id(self) -> int:+ return self.action_sequence_id+++class FirstItemStreamModel(IdentifyingModel):+ """Pydantic model that represents an entry for the first item of a stream"""++ action_sequence_id: int+ span_id: Optional[+ str+ ] # It doesn't have to relate to a span, but if it was started in a span, this is the span_id+ first_item_time: datetime.datetime+ type: str = "first_item_stream"++ @property+ def sequence_id(self) -> int:+ return self.action_sequence_id+++class EndStreamModel(IdentifyingModel):+ """Pydantic model that represents an entry for the first item of a stream"""++ action_sequence_id: int+ span_id: Optional[+ str+ ] # It doesn't have to relate to a span, but if it was started in a span, this is the span_id+ end_time: datetime.datetime+ items_streamed: int+ type: str = "end_stream"++ @property+ def sequence_id(self) -> int:+ return self.action_sequence_iddiff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.pyindex 4e59f06f..c1f02bcb 100644--- a/burr/tracking/server/backend.py+++ b/burr/tracking/server/backend.py@@ -289,7 +289,6 @@ class LocalBackend(BackendBase):)async with aiofiles.open(graph_file) as f:str_graph = await f.read()- # TODO -- use the Step.from_logs methodcollections.defaultdict(list)if os.path.exists(log_file):async with aiofiles.open(log_file, "rb") as f:diff --git a/burr/tracking/server/schema.py b/burr/tracking/server/schema.pyindex 6db8b141..09057550 100644--- a/burr/tracking/server/schema.py+++ b/burr/tracking/server/schema.py@@ -1,6 +1,6 @@import collectionsimport datetime-from typing import Any, Dict, List, Optional+from typing import Any, Dict, List, Optional, Unionimport pydanticfrom pydantic import fields@@ -13,6 +13,9 @@ from burr.tracking.common.models import (ChildApplicationModel,EndEntryModel,EndSpanModel,+ EndStreamModel,+ FirstItemStreamModel,+ InitializeStreamModel,PointerModel,)from burr.tracking.utils import safe_json_load@@ -61,6 +64,9 @@ class PartialStep(pydantic.BaseModel):step_start_log: Optional[BeginEntryModel] = fields.Field(default_factory=lambda: None)step_end_log: Optional[EndEntryModel] = fields.Field(default_factory=lambda: None)spans: List[Span] = fields.Field(default_factory=list)+ streaming_events: List[+ Union[InitializeStreamModel, FirstItemStreamModel, EndStreamModel]+ ] = fields.Field(default_factory=list)class Step(pydantic.BaseModel):@@ -70,6 +76,7 @@ class Step(pydantic.BaseModel):step_end_log: Optional[EndEntryModel]spans: List[Span]attributes: List[AttributeModel]+ streaming_events: List[Union[InitializeStreamModel, FirstItemStreamModel, EndStreamModel]]@staticmethoddef from_logs(log_lines: List[bytes]) -> List["Step"]:@@ -98,6 +105,15 @@ class Step(pydantic.BaseModel):elif json_line["type"] == "attribute":attribute = AttributeModel.parse_obj(json_line)attributes_by_step[attribute.action_sequence_id].append(attribute)+ elif json_line["type"] in ["begin_stream", "first_item_stream", "end_stream"]:+ streaming_event = {+ "begin_stream": InitializeStreamModel,+ "first_item_stream": FirstItemStreamModel,+ "end_stream": EndStreamModel,+ }[json_line["type"]].parse_obj(json_line)+ steps_by_sequence_id[streaming_event.sequence_id].streaming_events.append(+ streaming_event+ )for span in spans_by_id.values():sequence_id = (span.begin_entry.action_sequence_id@@ -121,6 +137,7 @@ class Step(pydantic.BaseModel):step_end_log=value.step_end_log,spans=[Span(**span.dict()) for span in value.spans if span.begin_entry is not None],attributes=attributes_by_step[key],+ streaming_events=value.streaming_events,)for key, value in sorted(steps_by_sequence_id.items())if value.step_start_log is not None