Skip to content

Commit

Permalink
Allow the user to limit autorun and fix a bug in reply index
Browse files Browse the repository at this point in the history
  • Loading branch information
xl0 committed Aug 18, 2024
1 parent 9974db8 commit 078d47b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 55 deletions.
19 changes: 13 additions & 6 deletions friendlly/magic_cell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_magic_cell.ipynb.

# %% auto 0
__all__ = ['env', 'config', 'chat', 'cells_to_messages', 'CellChat', 'NBClassicChat', 'NoJSChat', 'fr_cell']
__all__ = ['env', 'config', 'autorun_count', 'chat', 'cells_to_messages', 'CellChat', 'NBClassicChat', 'NoJSChat', 'fr_cell']

# %% ../nbs/01_magic_cell.ipynb 6
system_prompt="""
Expand All @@ -12,7 +12,7 @@
When appropriate, use Jupyter notebook. It has python 3.10+ and many packages installed.
Use <code> to add a new code cell at the end of your message. You will receive all its output on the next turn.
Prefer short cells that can be tested quickly.
Unless the task is trivial, use short cells and test them one by one
You can use the usual display(), print(), or place the value on the last line of the cell.
Make sure to not add anything after that value, as it won't be returned if it's not on the last line.
Expand Down Expand Up @@ -212,7 +212,7 @@ def combine_md_reply(body, thought, tail=""):
self.update_code(code_reply.strip())

if code_reply.strip():
prefix = f"%%fr +{num_above + 2 if self.added_md_cell else 1} reply\n"
prefix = f"%%fr +{num_above + (2 if self.added_md_cell else 1)} reply\n"
self.update_code_final(prefix + code_reply.strip())

if self.client.stop_reason == "stop_sequence":
Expand Down Expand Up @@ -322,17 +322,20 @@ def update_code_final(self, code):
config = nict(
environment=env,
autorun=(env == "nbclassic"),
md_cells=(env == "nbclassic"),
autorun_max=3,
md_cells=False,
comment_after_run=True,
api_key=None,
model="claude-3-5-sonnet-20240620",
system_prompt=system_prompt
)

autorun_count = 0
chat = None
def fr_cell(line=None, cell=None):
global chat
global config
global autorun_count

if config.environment == "nbclassic":
chat = NBClassicChat(config=config)
Expand Down Expand Up @@ -425,8 +428,12 @@ def fr_cell(line=None, cell=None):
if (config.autorun
and idx is not None
and code_idx is not None
and code_idx != idx):
nbclassic_execute_cell(code_idx)
and code_idx != idx
and autorun_count < config.autorun_max):
autorun_count += 1;
nbclassic_execute_cell(code_idx)
else:
autorun_count = 0;

# We might have created some invisible JS display outputs. Unfortunaely I don't think there is a way
# to clear them without removing everying.
Expand Down
101 changes: 52 additions & 49 deletions nbs/01_magic_cell.ipynb

Large diffs are not rendered by default.

0 comments on commit 078d47b

Please sign in to comment.