Skip to content

Commit

Permalink
refactor prefill
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 22, 2024
1 parent 0225b4d commit 58fec2e
Show file tree
Hide file tree
Showing 7 changed files with 368 additions and 301 deletions.
346 changes: 222 additions & 124 deletions 00_core.ipynb

Large diffs are not rendered by default.

35 changes: 17 additions & 18 deletions 01_toolloop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"#| export\n",
"from claudette.core import *\n",
"from fastcore.utils import *\n",
"from fastcore.meta import delegates\n",
"\n",
"from anthropic.types import TextBlock, Message\n",
"from anthropic.types.beta.tools import ToolsBetaMessage, tool_use_block"
Expand Down Expand Up @@ -146,7 +147,7 @@
{
"data": {
"text/plain": [
"[ToolUseBlock(id='toolu_01WV89XqdENgfTWX77tBZNVH', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]"
"[ToolUseBlock(id='toolu_015fso38UFhm481BhMLVjxsM', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]"
]
},
"execution_count": null,
Expand Down Expand Up @@ -216,7 +217,7 @@
"data": {
"text/plain": [
"[TextBlock(text=\"Okay, let's cancel all orders for customer C1:\", type='text'),\n",
" ToolUseBlock(id='toolu_01Au3Jkr3vxwrxHxZpqkdQUE', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]"
" ToolUseBlock(id='toolu_01QQxikpYemEuspxByvjHV9w', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]"
]
},
"execution_count": null,
Expand Down Expand Up @@ -248,21 +249,19 @@
"source": [
"#| exports\n",
"@patch\n",
"@delegates(Chat.__call__)\n",
"def toolloop(self:Chat,\n",
" pr, # Prompt to pass to Claude\n",
" max_steps=10, # Maximum number of tool requests to loop through\n",
" temp=0, # Temperature\n",
" maxtok=4096, # Maximum tokens\n",
" stop:Optional[list[str]]=None, # Stop sequences\n",
" trace_func:Optional[callable]=None, # Function to trace tool use steps (e.g `print`)\n",
" cont_func:Optional[callable]=noop, # Function that stops loop if returns False\n",
" **kw):\n",
" **kwargs):\n",
" \"Add prompt `pr` to dialog and get a response from Claude, automatically following up with `tool_use` messages\"\n",
" r = self(pr, temp=temp, maxtok=maxtok, stop=stop, **kw)\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",
" r = self(temp=temp, maxtok=maxtok, stop=stop, **kw)\n",
" r = self(**kwargs)\n",
" if not (cont_func or noop)(self.h[-2]['content']): break\n",
" if trace_func: trace_func(r)\n",
" return r"
Expand Down Expand Up @@ -296,7 +295,7 @@
"\n",
"<details>\n",
"\n",
"- id: msg_01JpTpsMwmpzRdBPGY8vTBbF\n",
"- id: msg_01Xq1XXL4RiafgmboJjMqNEw\n",
"- content: [{'text': 'The email address for customer C1 is [email protected].', 'type': 'text'}]\n",
"- model: claude-3-haiku-20240307\n",
"- role: assistant\n",
Expand All @@ -308,7 +307,7 @@
"</details>"
],
"text/plain": [
"ToolsBetaMessage(id='msg_01JpTpsMwmpzRdBPGY8vTBbF', content=[TextBlock(text='The email address for customer C1 is [email protected].', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 732; Out: 19; Total: 751)"
"ToolsBetaMessage(id='msg_01Xq1XXL4RiafgmboJjMqNEw', content=[TextBlock(text='The email address for customer C1 is [email protected].', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 732; Out: 19; Total: 751)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -341,12 +340,12 @@
"output_type": "stream",
"text": [
"- Retrieving customer C1\n",
"ToolsBetaMessage(id='msg_01N8ZAxrGhW5jjYhWtmVnJvv', content=[TextBlock(text=\"Okay, let's cancel all orders for customer C1:\", type='text'), ToolUseBlock(id='toolu_01GTrzjsjegiWY9pAe4zPcKD', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 537; Out: 72; Total: 609)\n",
"ToolsBetaMessage(id='msg_018DfSqzw5JPwxwm1YQPmQDp', content=[TextBlock(text=\"Okay, let's cancel all orders for customer C1:\", type='text'), ToolUseBlock(id='toolu_01GYGNZTTPBSk4HatA1cZU7X', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 537; Out: 72; Total: 609)\n",
"- Cancelling order O1\n",
"ToolsBetaMessage(id='msg_01WgSvBXbdgDfqtUHGYJ4nUZ', content=[TextBlock(text=\"Based on the customer information, it looks like there are 2 orders for customer C1:\\n- Order O1 for Widget A\\n- Order O2 for Gadget B\\n\\nLet's cancel each of these orders:\", type='text'), ToolUseBlock(id='toolu_01FWHSZKUsZbMC1SqPyfdLv4', input={'order_id': 'O1'}, name='cancel_order', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 745; Out: 107; Total: 852)\n",
"ToolsBetaMessage(id='msg_018BSpN4F6JthNBxcdeGcTZC', content=[TextBlock(text=\"Based on the customer information, it looks like there are 2 orders for customer C1:\\n- Order O1 for Widget A\\n- Order O2 for Gadget B\\n\\nLet's cancel each of these orders:\", type='text'), ToolUseBlock(id='toolu_01MY9eSsNpGYDKJfCo5PKGhW', input={'order_id': 'O1'}, name='cancel_order', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 745; Out: 107; Total: 852)\n",
"- Cancelling order O2\n",
"ToolsBetaMessage(id='msg_016KhCjJW8V7csuBfSiENSBM', content=[ToolUseBlock(id='toolu_01BgC2eRqtnniEt87Qq4MTJc', input={'order_id': 'O2'}, name='cancel_order', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 864; Out: 57; Total: 921)\n",
"ToolsBetaMessage(id='msg_01CkomDnPVaWAzfJpVzvUs9s', content=[TextBlock(text='The cancellation of both orders O1 and O2 for customer C1 was successful. Please let me know if you need anything else!', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 35; Total: 968)\n"
"ToolsBetaMessage(id='msg_01BmratbTZX6vp5Grh7YXRb1', content=[ToolUseBlock(id='toolu_01UffBAia21dGEwkdgZmaPxE', input={'order_id': 'O2'}, name='cancel_order', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 864; Out: 57; Total: 921)\n",
"ToolsBetaMessage(id='msg_01TQctZzxaypA8W3kWjsmq8z', content=[TextBlock(text='The cancellation of both orders O1 and O2 for customer C1 was successful. Please let me know if you need anything else!', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 35; Total: 968)\n"
]
},
{
Expand All @@ -356,7 +355,7 @@
"\n",
"<details>\n",
"\n",
"- id: msg_01CkomDnPVaWAzfJpVzvUs9s\n",
"- id: msg_01TQctZzxaypA8W3kWjsmq8z\n",
"- content: [{'text': 'The cancellation of both orders O1 and O2 for customer C1 was successful. Please let me know if you need anything else!', 'type': 'text'}]\n",
"- model: claude-3-haiku-20240307\n",
"- role: assistant\n",
Expand All @@ -368,7 +367,7 @@
"</details>"
],
"text/plain": [
"ToolsBetaMessage(id='msg_01CkomDnPVaWAzfJpVzvUs9s', content=[TextBlock(text='The cancellation of both orders O1 and O2 for customer C1 was successful. Please let me know if you need anything else!', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 35; Total: 968)"
"ToolsBetaMessage(id='msg_01TQctZzxaypA8W3kWjsmq8z', content=[TextBlock(text='The cancellation of both orders O1 and O2 for customer C1 was successful. Please let me know if you need anything else!', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 35; Total: 968)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -410,7 +409,7 @@
"\n",
"<details>\n",
"\n",
"- id: msg_01MsurGXcoVYvnMR5gEwaqFv\n",
"- id: msg_0124BwguqqdtbP1EwQig2bFP\n",
"- content: [{'text': \"The status of order O2 is now 'Cancelled' since we successfully cancelled it earlier.\", 'type': 'text'}]\n",
"- model: claude-3-haiku-20240307\n",
"- role: assistant\n",
Expand All @@ -422,7 +421,7 @@
"</details>"
],
"text/plain": [
"ToolsBetaMessage(id='msg_01MsurGXcoVYvnMR5gEwaqFv', content=[TextBlock(text=\"The status of order O2 is now 'Cancelled' since we successfully cancelled it earlier.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1107; Out: 25; Total: 1132)"
"ToolsBetaMessage(id='msg_0124BwguqqdtbP1EwQig2bFP', content=[TextBlock(text=\"The status of order O2 is now 'Cancelled' since we successfully cancelled it earlier.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1107; Out: 25; Total: 1132)"
]
},
"execution_count": null,
Expand Down
Loading

0 comments on commit 58fec2e

Please sign in to comment.