Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create nb_client for load_history. #1166

Open
wants to merge 5 commits into
base: code_interpreter
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion metagpt/utils/recovery_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from pathlib import Path

import nbformat
from nbclient import NotebookClient
from nbformat.notebooknode import NotebookNode

from metagpt.const import DATA_PATH
from metagpt.roles.role import Role
Expand All @@ -29,7 +31,8 @@ def load_history(save_dir: str = ""):
nb_path = Path(save_dir) / "history_nb" / "code.ipynb"
plan = read_json_file(plan_path)
nb = nbformat.read(open(nb_path, "r", encoding="utf-8"), as_version=nbformat.NO_CONVERT)
return plan, nb
nb_client = NotebookClient(process_cells(nb), timeout=600)
return plan, nb, nb_client


def save_history(role: Role, save_dir: str = ""):
Expand All @@ -56,3 +59,23 @@ def save_history(role: Role, save_dir: str = ""):

save_code_file(name=Path(record_time) / "history_nb", code_context=role.execute_code.nb, file_format="ipynb")
return save_path


def is_cell_to_delete(cell: NotebookNode) -> bool:
if "outputs" in cell:
for output in cell["outputs"]:
if output and "traceback" in output:
return True
return False


def process_cells(nb: NotebookNode) -> NotebookNode:
new_cells = []
i = 1
for cell in nb["cells"]:
if cell["cell_type"] == "code" and not is_cell_to_delete(cell):
cell["execution_count"] = i
new_cells.append(cell)
i = i + 1
nb["cells"] = new_cells
return nb
Loading