Skip to content

Commit

Permalink
Shows prototype how to load from tracked state
Browse files Browse the repository at this point in the history
This will be useful for debugging. E.g. in the UI have
the code to replay from this point int state. Then boom
 you can go and try to debug what was going on more
easily.
  • Loading branch information
skrawcz committed Feb 26, 2024
1 parent f3fa939 commit febd6a7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
3 changes: 3 additions & 0 deletions burr/tracking/server/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ async def list_apps(
raise fastapi.HTTPException(status_code=404, detail=f"Project: {project_id} not found")
out = []
for entry in await aiofilesos.listdir(project_filepath):
if entry.startswith("."):
# skip hidden files/directories
continue
full_path = os.path.join(project_filepath, entry)
log_path = os.path.join(full_path, "log.jsonl")
if os.path.isdir(full_path):
Expand Down
56 changes: 45 additions & 11 deletions examples/multi-agent-collaboration/application.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
import os.path

import func_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_experimental.utilities import PythonREPL
Expand Down Expand Up @@ -123,16 +126,46 @@ def post_run_step(self, *, state: "State", action: "Action", **future_kwargs):
print("state======\n", state)


def main():
def initialize_state_from_logs(tracker_name: str, app_id: str) -> tuple[dict, str]:
"""Initialize the state to debug from an exception
:param tracker_name:
:param app_id:
:return:
"""
# open ~/.burr/{tracker_name}/{app_id}/log.jsonl
# find the first entry with an exception -- and pull state from it.
with open(f"{os.path.expanduser('~/')}/.burr/{tracker_name}/{app_id}/log.jsonl", "r") as f:
lines = f.readlines()
for line in lines:
line = json.loads(line)
if "exception" in line:
state = line["state"]
entry_point = line["action"]
return state, entry_point
raise ValueError(f"No exception found in logs for {tracker_name}/{app_id}")


def default_state_and_entry_point() -> tuple[dict, str]:
return {
"messages": [],
"query": "Fetch the UK's GDP over the past 5 years,"
" then draw a line graph of it."
" Once you code it up, finish.",
"next_hop": "",
}, "researcher"


def main(app_instance_id: str = None):
tracker_name = "hamilton-multi-agent"
if app_instance_id:
state, entry_point = initialize_state_from_logs(tracker_name, app_instance_id)
else:
state, entry_point = default_state_and_entry_point()

app = (
ApplicationBuilder()
.with_state(
messages=[],
query="Fetch the UK's GDP over the past 5 years,"
" then draw a line graph of it."
" Once you code it up, finish.",
next_hop="",
)
.with_state(**state)
.with_actions(
researcher=researcher,
chart_generator=chart_generator,
Expand All @@ -154,17 +187,18 @@ def main():
core.expr("next_hop == 'complete'"),
), # core.expr("'FINAL ANSWER' in messages[-1]['content']")),
)
.with_entrypoint("researcher")
.with_entrypoint(entry_point)
.with_hooks(PrintStepHook())
.with_tracker("hamilton-multi-agent")
.with_tracker(tracker_name)
.build()
)
app.run(halt_after=["terminal"])
# return app


if __name__ == "__main__":
main()
_app_id = "app_4d1618d2-79d1-4d89-8e3f-70c216c71e63"
main(_app_id)
import sys

sys.exit(0)
Expand Down

0 comments on commit febd6a7

Please sign in to comment.