You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can then make use of your ToolUser by calling its `use_tools()` method and passing in your desired prompt. Setting execution mode to "automatic" makes it execute the function; in the default "manual" mode it returns the function arguments back to the client to be executed there.
82
82
```python
83
-
messages = [{'role': 'human', 'content': 'What time is it in Los Angeles?'}]
83
+
messages = [{'role': 'user', 'content': 'What time is it in Los Angeles?'}]
NOTE: If using bedrock, this SDK only supports claude 2.1 (anthropic.claude-v2:1).
91
92
92
93
Notice that new `messages` format instead of passing in a simple prompt string? Never seen it before? Don't worry, we are about to walk through it.
93
94
94
-
### New Prompt Format
95
-
Historically, interacting with Claude required that you directly pass it a string (including any required Human/Assistant formatting), and get back a string. This was already somewhat error prone from both an input and parsing standpoint and became more cumbersome to deal with in the context of tool use. The result is that anthropic-tools uses a new, *structured* prompt input and output format, coming as a list of messages. Let's take a quick tour of how to work with this list.
95
+
### Prompt Format
96
+
Anthropic-tools uses a *structured* prompt input and output format, coming as a list of messages, intending to mimic our Messages API format. Let's take a quick tour of how to work with this list.
96
97
97
98
`messages` is a python list of message dictionaries. A single message dictionary object can contain these fields but will never contain all of them (see the field comments below for more detail on what this means):
98
99
```python
99
100
{
100
-
"role": str, # The role of the message. 'human' for a message from the human, 'assistant' for a message from the assistant, 'tool_inputs' for a request from the assistant to use tools, 'tool_outputs' for a response to a tool_inputs message containing the results of using the specified tools in the specified ways.
101
-
"content": str, # The (non tool use) content of the message, which must be present for messages where role=(human, assistant, tool_inputs) and can not be present for messages where role=tool_outputs.
101
+
"role": str, # The role of the message. 'user' for a message from the user, 'assistant' for a message from the assistant, 'tool_inputs' for a request from the assistant to use tools, 'tool_outputs' for a response to a tool_inputs message containing the results of using the specified tools in the specified ways.
102
+
"content": str, # The (non tool use) content of the message, which must be present for messages where role=(user, assistant, tool_inputs) and can not be present for messages where role=tool_outputs.
102
103
"tool_inputs": list[dict], # A list of dictionaries (see below). Must be specified in messages where role=tool_inputs.
103
104
"tool_outputs": list[dict], # A list of tool_output dictionaries (see below). One of tool_outputs or tool_error must be specified in messages where role=tool_outputs, but the other must be specified as None.
104
105
"tool_error": str# A tool error message corresponding to the first tool that errored to help Claude understand what it did wrong. One of tool_error or tool_outputs must be specified when role=tool_outputs, but the other must be specified as None.
@@ -164,21 +165,21 @@ Sometimes when Claude responds with a `tool_inputs` message it makes a mistake a
164
165
```
165
166
166
167
So, what might `messages` look like in practice?
167
-
Here is a human message:
168
+
Here is a user message:
168
169
```python
169
-
human_message= {'role': 'human', 'content': 'Hi Claude, what US states start with C?'}
170
-
messages = [human_message]
170
+
user_message= {'role': 'user', 'content': 'Hi Claude, what US states start with C?'}
171
+
messages = [user_message]
171
172
```
172
-
Here is a human message and an assistant response, with no tool use involved.
173
+
Here is a user message and an assistant response, with no tool use involved.
173
174
```python
174
-
human_message= {'role': 'human', 'content': 'Hi Claude, what US states start with C?'}
175
+
user_message= {'role': 'humuseran', 'content': 'Hi Claude, what US states start with C?'}
175
176
assistant_message = {'role': 'assistant', 'content': 'California, Colorado, and Connecticut are the US states that start with the letter C.'}
176
-
messages = [human_message, assistant_message]
177
+
messages = [user_message, assistant_message]
177
178
```
178
179
179
-
Here is a human message, followed by a tool_inputs message, followed by a successful tool_outputs message:
180
+
Here is a user message, followed by a tool_inputs message, followed by a successful tool_outputs message:
180
181
```python
181
-
human_message= {'role': 'human', 'content': 'If Maggie has 3 apples and eats 1, how many apples does maggie have left?'}
182
+
user_message= {'role': 'user', 'content': 'If Maggie has 3 apples and eats 1, how many apples does maggie have left?'}
182
183
tool_inputs_message = {
183
184
'role': 'tool_inputs',
184
185
'content': "Let's think this through. Maggie had 3 apples, she ate one so:",
That's it for the new messages format. To help wrap your head around this concept, at the end of the "Putting it Together" section below, we will build a python function to handle these sorts of requests.
"content": "Sally has 17 apples. She gives 9 to Jim. Later that day, Peter gives 6 Bananas to Sally. How many pieces of fruit does Sally have at the end of the day?"
You may also notice that we set `execution_mode='automatic'`, recall that this means Claude will have its tool usage requests automatically executed and fed back in until it decides it has done enough to answer your query, at which point it will respond to you with that answer. If you set `execution_mode='manual'`, Claude will stop after its first request to use a tool/tools and you will be returned the requested tool(s) to use and the arguments to use them with.
@@ -141,7 +141,7 @@ Finally, we pass `sql_tool` to `ToolUser` and run our query!
141
141
```python
142
142
tool_user = ToolUser([sql_tool])
143
143
144
-
messages = [{"role": "human", "content": "Who is our oldest employee?"}]
144
+
messages = [{"role": "user", "content": "Who is our oldest employee?"}]
Copy file name to clipboardexpand all lines: tool_use_package/calculator_example.py
+1-1
Original file line number
Diff line number
Diff line change
@@ -46,5 +46,5 @@ def use_tool(self, a, b):
46
46
47
47
# Call the tool_user with a prompt to get a version of Claude that can use your tools!
48
48
if__name__=='__main__':
49
-
messages= [{"role":"human", "content":"John has three apples. Maggie has nine apples. Tim has 4 bananas. If John gives Maggie 1 apple and Tim gives John 2 bananas, how much fruit does each person have?"}]
49
+
messages= [{"role":"user", "content":"John has three apples. Maggie has nine apples. Tim has 4 bananas. If John gives Maggie 1 apple and Tim gives John 2 bananas, how much fruit does each person have?"}]
0 commit comments