-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "57b2cde6", | ||
"id": "63111054", | ||
"metadata": {}, | ||
"source": [ | ||
"Anthropic provides an [interesting example](https://github.com/anthropics/anthropic-cookbook/blob/main/tool_use/customer_service_agent.ipynb) of using tools to mock up a hypothetical ordering system. We're going to take it a step further, and show how we can dramatically simplify the process, whilst completing more complex tasks.\n", | ||
|
@@ -62,7 +62,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "56b6c3df", | ||
"id": "2d8eed7b", | ||
"metadata": {}, | ||
"source": [ | ||
"We can now define the same functions from the original example -- but note that we don't need to manually create the large JSON schema, since Claudette handles all that for us automatically from the functions directly. We'll add some extra functionality to update order details when cancelling too." | ||
|
@@ -71,7 +71,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f02a8fb7", | ||
"id": "2778dde0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -101,7 +101,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3593869d", | ||
"id": "a1278535", | ||
"metadata": {}, | ||
"source": [ | ||
"We're now ready to start our chat." | ||
|
@@ -110,7 +110,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9032540c", | ||
"id": "a4231dff", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -120,7 +120,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e166ad45", | ||
"id": "41db970a", | ||
"metadata": {}, | ||
"source": [ | ||
"We'll start with the same request as Anthropic showed:" | ||
|
@@ -129,7 +129,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "d4907bf6", | ||
"id": "97cdbf58", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
|
@@ -142,7 +142,7 @@ | |
{ | ||
"data": { | ||
"text/plain": [ | ||
"[ToolUseBlock(id='toolu_01GaTymQyJjjtZuH5QaTUpYV', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]" | ||
"[ToolUseBlock(id='toolu_01UTF8GiVV5BgdiHZFazp1p6', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]" | ||
] | ||
}, | ||
"execution_count": null, | ||
|
@@ -158,7 +158,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "42874d6a", | ||
"id": "3f6d3ae8", | ||
"metadata": {}, | ||
"source": [ | ||
"Claude asks us to use a tool. Claudette handles that automatically by just passing back the message:" | ||
|
@@ -167,7 +167,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a873f6e2", | ||
"id": "09196e94", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
|
@@ -195,7 +195,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e1b7be56", | ||
"id": "26fe5edb", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's consider a more complex case than in the original example -- what happens if a customer wants to cancel all of their orders?" | ||
|
@@ -204,7 +204,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f2fb1012", | ||
"id": "6aa531d0", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
|
@@ -217,8 +217,8 @@ | |
{ | ||
"data": { | ||
"text/plain": [ | ||
"[TextBlock(text=\"Okay, let's cancel all orders for customer C1.\", type='text'),\n", | ||
" ToolUseBlock(id='toolu_018HSZGj5THYQB7Vzmk5XadR', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]" | ||
"[TextBlock(text=\"Okay, let's cancel all orders for customer C1:\", type='text'),\n", | ||
" ToolUseBlock(id='toolu_01DXvWH5LstcutuCeHjK6vjq', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')]" | ||
] | ||
}, | ||
"execution_count": null, | ||
|
@@ -235,7 +235,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8175a1e6", | ||
"id": "5a2432a1", | ||
"metadata": {}, | ||
"source": [ | ||
"This is the start of a multi-stage tool use process. Doing it manually step by step is inconvenient, so let's write a function to handle this for us:" | ||
|
@@ -244,7 +244,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "3fed3e1e", | ||
"id": "3457eeb6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
|
@@ -260,18 +260,17 @@ | |
" **kw):\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", | ||
" i=0\n", | ||
" while r.stop_reason=='tool_use' and i<max_steps:\n", | ||
" for i in range(max_steps):\n", | ||
" if r.stop_reason!='tool_use': break\n", | ||
" if show_trace: print(r)\n", | ||
" r = self(r, temp=temp, maxtok=maxtok, stop=stop, **kw)\n", | ||
" i += 1\n", | ||
" if show_trace: print(r)\n", | ||
" return r" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "43e8251b", | ||
"id": "f83a0859", | ||
"metadata": {}, | ||
"source": [ | ||
"We'll start by re-running our previous request - we shouldn't have to manually pass back the `tool_use` message any more:" | ||
|
@@ -280,7 +279,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "5180d4db", | ||
"id": "52fa6634", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
|
@@ -297,7 +296,7 @@ | |
"\n", | ||
"<details>\n", | ||
"\n", | ||
"- id: msg_018ZS1oLcBnqXjnCjcCWcvWX\n", | ||
"- id: msg_01XMz1SxEoXQkzspHg7F5A9g\n", | ||
"- content: [{'text': 'The email address for customer C1 is [email protected].', 'type': 'text'}]\n", | ||
"- model: claude-3-haiku-20240307\n", | ||
"- role: assistant\n", | ||
|
@@ -309,7 +308,7 @@ | |
"</details>" | ||
], | ||
"text/plain": [ | ||
"ToolsBetaMessage(id='msg_018ZS1oLcBnqXjnCjcCWcvWX', 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_01XMz1SxEoXQkzspHg7F5A9g', 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, | ||
|
@@ -325,7 +324,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "31f4a57b", | ||
"id": "db6131f0", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's see if it can handle the multi-stage process now -- we'll add `show_trace` to see each stage of the process:" | ||
|
@@ -334,41 +333,42 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "da5c99b0", | ||
"id": "a45d3eb0", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"ToolsBetaMessage(id='msg_01DTMHbL3ieoESGypnpJXwWK', content=[TextBlock(text=\"Okay, let's cancel all orders for customer C1.\", type='text'), ToolUseBlock(id='toolu_01Ewn88rpxxgTmeo2NdpLB2j', 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_016Lt1Xicy86KuYLEsircSuL', content=[TextBlock(text=\"Okay, let's cancel all orders for customer C1:\", type='text'), ToolUseBlock(id='toolu_019kvnDqgyy1VpMTuEeRkVww', 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", | ||
"- Retrieving customer C1\n", | ||
"ToolsBetaMessage(id='msg_018Bwa5Kmtr1pi72qp9cDJoP', content=[TextBlock(text=\"Based on the customer information, it looks like there are 2 orders for customer C1:\\n- Order O1 for Widget A (status: Shipped)\\n- Order O2 for Gadget B (status: Processing)\\n\\nLet's cancel both of these orders:\", type='text'), ToolUseBlock(id='toolu_01V117om2ibdDr9EUSR4eBJq', input={'order_id': 'O1'}, name='cancel_order', type='tool_use'), ToolUseBlock(id='toolu_018XmD9KZkNSmFEQijyoLkjX', 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: 745; Out: 157; Total: 902)\n", | ||
"ToolsBetaMessage(id='msg_01PKHb5CtdNhvpLSfz3T4FoB', 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 both of these orders:\", type='text'), ToolUseBlock(id='toolu_01YUCv13QLFqDgKt3uJZxS17', 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 O1\n", | ||
"ToolsBetaMessage(id='msg_01T222bARXHYP2YeZc6TXkbq', content=[ToolUseBlock(id='toolu_01BBy11iX2SfHKqvUctYgWs3', 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", | ||
"- Cancelling order O2\n", | ||
"ToolsBetaMessage(id='msg_018NSi4aB2jn1cPnQrZ8T5Pk', content=[TextBlock(text='The cancellation of both orders was successful. I have now cancelled all orders for customer C1.', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 966; Out: 24; Total: 990)\n" | ||
"ToolsBetaMessage(id='msg_018UXRiRM2KkjmSQmkpkBv9f', content=[TextBlock(text='Both order cancellations were successful. I have now cancelled all orders for customer C1.', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 23; Total: 956)\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/markdown": [ | ||
"The cancellation of both orders was successful. I have now cancelled all orders for customer C1.\n", | ||
"Both order cancellations were successful. I have now cancelled all orders for customer C1.\n", | ||
"\n", | ||
"<details>\n", | ||
"\n", | ||
"- id: msg_018NSi4aB2jn1cPnQrZ8T5Pk\n", | ||
"- content: [{'text': 'The cancellation of both orders was successful. I have now cancelled all orders for customer C1.', 'type': 'text'}]\n", | ||
"- id: msg_018UXRiRM2KkjmSQmkpkBv9f\n", | ||
"- content: [{'text': 'Both order cancellations were successful. I have now cancelled all orders for customer C1.', 'type': 'text'}]\n", | ||
"- model: claude-3-haiku-20240307\n", | ||
"- role: assistant\n", | ||
"- stop_reason: end_turn\n", | ||
"- stop_sequence: None\n", | ||
"- type: message\n", | ||
"- usage: {'input_tokens': 966, 'output_tokens': 24}\n", | ||
"- usage: {'input_tokens': 933, 'output_tokens': 23}\n", | ||
"\n", | ||
"</details>" | ||
], | ||
"text/plain": [ | ||
"ToolsBetaMessage(id='msg_018NSi4aB2jn1cPnQrZ8T5Pk', content=[TextBlock(text='The cancellation of both orders was successful. I have now cancelled all orders for customer C1.', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 966; Out: 24; Total: 990)" | ||
"ToolsBetaMessage(id='msg_018UXRiRM2KkjmSQmkpkBv9f', content=[TextBlock(text='Both order cancellations were successful. I have now cancelled all orders for customer C1.', type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 933; Out: 23; Total: 956)" | ||
] | ||
}, | ||
"execution_count": null, | ||
|
@@ -384,7 +384,7 @@ | |
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "46e1f121", | ||
"id": "a7a8c9fc", | ||
"metadata": {}, | ||
"source": [ | ||
"OK Claude thinks the orders were cancelled -- let's check one:" | ||
|
@@ -393,7 +393,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "650c2650", | ||
"id": "592e9084", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
|
@@ -406,23 +406,23 @@ | |
{ | ||
"data": { | ||
"text/markdown": [ | ||
"The status of order O2 is now 'Cancelled'. This matches the result we got when we successfully cancelled the order earlier.\n", | ||
"The status of order O2 is now 'Cancelled' since I successfully cancelled that order earlier.\n", | ||
"\n", | ||
"<details>\n", | ||
"\n", | ||
"- id: msg_01VQikjVpyUK4D4CUqVeKbzt\n", | ||
"- content: [{'text': \"The status of order O2 is now 'Cancelled'. This matches the result we got when we successfully cancelled the order earlier.\", 'type': 'text'}]\n", | ||
"- id: msg_015LKdAkYwJTYGabfJHJBE1t\n", | ||
"- content: [{'text': \"The status of order O2 is now 'Cancelled' since I successfully cancelled that order earlier.\", 'type': 'text'}]\n", | ||
"- model: claude-3-haiku-20240307\n", | ||
"- role: assistant\n", | ||
"- stop_reason: end_turn\n", | ||
"- stop_sequence: None\n", | ||
"- type: message\n", | ||
"- usage: {'input_tokens': 1129, 'output_tokens': 32}\n", | ||
"- usage: {'input_tokens': 1095, 'output_tokens': 26}\n", | ||
"\n", | ||
"</details>" | ||
], | ||
"text/plain": [ | ||
"ToolsBetaMessage(id='msg_01VQikjVpyUK4D4CUqVeKbzt', content=[TextBlock(text=\"The status of order O2 is now 'Cancelled'. This matches the result we got when we successfully cancelled the order earlier.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1129; Out: 32; Total: 1161)" | ||
"ToolsBetaMessage(id='msg_015LKdAkYwJTYGabfJHJBE1t', content=[TextBlock(text=\"The status of order O2 is now 'Cancelled' since I successfully cancelled that order earlier.\", type='text')], model='claude-3-haiku-20240307', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1095; Out: 26; Total: 1121)" | ||
] | ||
}, | ||
"execution_count": null, | ||
|
@@ -458,7 +458,7 @@ | |
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8a134f44", | ||
"id": "c627b799", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.