Skip to content

Commit

Permalink
Merge pull request #39 from AnswerDotAI/toolloop-trace-history
Browse files Browse the repository at this point in the history
trace history instead of chat response in toolloop
  • Loading branch information
jph00 authored Oct 24, 2024
2 parents 6f41119 + 07f8e15 commit 3c9c452
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
18 changes: 10 additions & 8 deletions 01_toolloop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,14 @@
" cont_func:Optional[callable]=noop, # Function that stops loop if returns False\n",
" **kwargs):\n",
" \"Add prompt `pr` to dialog and get a response from Claude, automatically following up with `tool_use` messages\"\n",
" n_msgs = len(self.h)\n",
" r = self(pr, **kwargs)\n",
" for i in range(max_steps):\n",
" if r.stop_reason!='tool_use': break\n",
" if trace_func: trace_func(r)\n",
" if trace_func: trace_func(self.h[n_msgs:]); n_msgs = len(self.h)\n",
" r = self(**kwargs)\n",
" if not (cont_func or noop)(self.h[-2]): break\n",
" if trace_func: trace_func(r)\n",
" if trace_func: trace_func(self.h[n_msgs:])\n",
" return r"
]
},
Expand Down Expand Up @@ -631,12 +632,13 @@
"metadata": {},
"outputs": [],
"source": [
"def _show_cts(r):\n",
" for o in r.content:\n",
" if hasattr(o,'text'): print(o.text)\n",
" nm = getattr(o, 'name', None)\n",
" if nm=='run_cell': print(o.input['code'])\n",
" elif nm: print(f'{o.name}({o.input})')"
"def _show_cts(h):\n",
" for r in h:\n",
" for o in r.get('content'):\n",
" if hasattr(o,'text'): print(o.text)\n",
" nm = getattr(o, 'name', None)\n",
" if nm=='run_cell': print(o.input['code'])\n",
" elif nm: print(f'{o.name}({o.input})')"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions claudette/toolloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ def toolloop(self:Chat,
cont_func:Optional[callable]=noop, # Function that stops loop if returns False
**kwargs):
"Add prompt `pr` to dialog and get a response from Claude, automatically following up with `tool_use` messages"
n_msgs = len(self.h)
r = self(pr, **kwargs)
for i in range(max_steps):
if r.stop_reason!='tool_use': break
if trace_func: trace_func(r)
if trace_func: trace_func(self.h[n_msgs:]); n_msgs = len(self.h)
r = self(**kwargs)
if not (cont_func or noop)(self.h[-2]): break
if trace_func: trace_func(r)
if trace_func: trace_func(self.h[n_msgs:])
return r

0 comments on commit 3c9c452

Please sign in to comment.