Multi Step Tool Use
Multi-Step Tool Use
Read the accompanying article here.
Multi-step tool use happens when more than one tool is required, and the output of one tool is needed as the input to another tool.
In other words, tool-calling needs to happen in a sequence.
This is especially true in complex workflows where tasks have to be broken down into a sequence of steps.
In this notebook, we’ll build a calendar assistant that can check a user’s calendar and create new appointments based on available times. This use case requires tool calls to happen in sequence, i.e., multi-step.
It is also an example of using tools for performing not just a read operation (retrieving information) but also a write operation (taking actions).
First, let’s install the Cohere Python SDK and set up the Cohere client.
Create Tools
Let’s now create two tools:
- A function called
list_calendar_eventsto list the existing calendar events based on a given date. For simplicity, we are not going to connect to an actual calendar. So we’ll include a mock events record, which is a simple list of events in a day. - A function called
create_calendar_eventto create a new calendar event based on the provided date, time, and duration. Again, to keep things simple, we are not going to make actual changes to a database, but instead will just return a simple success message for illustration purposes.
Next, we define the tool schema for the two tools.
Let’s also create a custom preamble. The important part here is mentioning the date since the list_calendar_events tool is a simple enough version that it doesn’t do any date-handling. What we’re doing is nudging the model to use this date for the create_calendar_event tool call, which requires a date field.
Let's create a run_assistant function that does the following:
- Get the user message (Step 1)
- Call the Chat endpoint for tool call generation (Step 2)
- If the response contains at least one tool call, execute the tool call(s) and get the tool results (Step 3)
- Repeat Steps 2 and 3 until there are no more tool calls
- Generate the final response with citations (Step 4)
Here, we remove the force_single_step argument as it is False by default. This indicates to the Cohere API to enable a multi-step workflow and use the right ReAct-style prompt for running the workflow.
Simple question that doesn't require multi-step
Let’s now ask the assistant the first question, starting with a simple one about the number of meetings for the day. This requires just a single step of tool calling to check existing calendar events.
That’s exactly what the assistant does, and it gives the correct answer.
Question:
How many meetings do I have today
==================================================
Tool plan:
I will use the 'list_calendar_events' tool to find out how many meetings the user has today.
Tool calls:
Tool name: list_calendar_events | Parameters: {'date': '05/23/2024'}
==================================================
Final response:
You have four meetings today.
==================================================
Citations:
start=9 end=22 text='four meetings' document_ids=['list_calendar_events:0:2:0']
Cited Documents:
{'existing_events': '[{"end":"8:59","start":"8:00"},{"end":"9:59","start":"9:00"},{"end":"11:59","start":"11:00"},{"end":"12:59","start":"12:00"}]', 'id': 'list_calendar_events:0:2:0', 'tool_name': 'list_calendar_events'}
==================================================
Multi-step
Let’s now try to ask a question that requires multi-step tool calling — this one asking the assistant to book an appointment. To complete this task, the assistant will have to first query the existing events and then use the information to create a new event that wouldn’t cause a conflict.
In the first step, the assistant calls the list_calendar_events tool to get a list of existing events.
In the second step, it calls the create_calendar_event tool to create a new event. It creates a new event at 10 a.m., which is indeed the first available slot after 9 a.m.
Question:
Create an hour-long appointment for the first available free slot after 9am
==================================================
Tool plan:
I will first check the user's calendar for 23/05/2024 to see if there are any free slots after 9am. I will then create an hour-long appointment for the first available free slot.
Tool calls:
Tool name: list_calendar_events | Parameters: {'date': '05/23/2024'}
==================================================
Tool plan:
The user's calendar shows that they have a free slot from 10:00 to 10:59. I will now create an hour-long appointment for this time slot.
Tool calls:
Tool name: create_calendar_event | Parameters: {'date': '05/23/2024', 'duration': 1, 'time': '10:00'}
==================================================
Final response:
I've created a 1-hour long appointment for 10:00 on 23/05/2024.
==================================================
Citations:
start=15 end=62 text='1-hour long appointment for 10:00 on 23/05/2024' document_ids=['create_calendar_event:0:4:0']
Cited Documents:
{'id': 'create_calendar_event:0:4:0', 'is_success': 'true', 'message': 'Created a 1 hour long event at 10:00 on 05/23/2024', 'tool_name': 'create_calendar_event'}
==================================================
And here’s a look at the chat history. Compared to the single-step scenario, it contains multiple pairs of CHATBOT - TOOL messages, with each pair being one step in the sequence of tool calls. In this particular case, we have two of them.
message='Create an hour-long appointment for the first available free slot after 9am' tool_calls=None role='USER'
message="I will first check the user's calendar for 23/05/2024 to see if there are any free slots after 9am. I will then create an hour-long appointment for the first available free slot." tool_calls=[ToolCall(name='list_calendar_events', parameters={'date': '05/23/2024'})] role='CHATBOT'
tool_results=[ToolResult(call=ToolCall(name='list_calendar_events', parameters={'date': '05/23/2024'}), outputs=[{'existing_events': [{'end': '8:59', 'start': '8:00'}, {'end': '9:59', 'start': '9:00'}, {'end': '11:59', 'start': '11:00'}, {'end': '12:59', 'start': '12:00'}]}])] role='TOOL'
message="The user's calendar shows that they have a free slot from 10:00 to 10:59. I will now create an hour-long appointment for this time slot." tool_calls=[ToolCall(name='create_calendar_event', parameters={'date': '05/23/2024', 'duration': 1, 'time': '10:00'})] role='CHATBOT'
tool_results=[ToolResult(call=ToolCall(name='create_calendar_event', parameters={'date': '05/23/2024', 'duration': 1, 'time': '10:00'}), outputs=[{'is_success': True, 'message': 'Created a 1 hour long event at 10:00 on 05/23/2024'}])] role='TOOL'
message="I've created a 1-hour long appointment for 10:00 on 23/05/2024." tool_calls=None role='CHATBOT'
The chat history also highlights another difference between the single-step and multi-step scenarios.
Notice that in the multi-step example above, the CHATBOT turns generate a textual response at each tool-calling step (look for the message) before making the actual tool calls.
Let’s call this the model’s “intermediate response” to differentiate it from the model’s final response, which is the response that the user sees. These intermediate responses are the model’s internal reasoning logic, which guides its next course of action.
Multi-step parallel
Let’s now look at an example of multi-step, parallel tool use. Let’s ask the assistant to create two separate events on the same day.
And here’s the assistant’s response. It follows the same two steps as in the earlier example: first, checking the existing events, and second, creating the new events.
The difference this time is that since it needs to create two events, it calls create_calendar_event twice within the same step. Here the assistant makes the correct judgment and tool calls are independent of each other, so they can be done in parallel.
Question:
Create two hour-long appointments for any available time between 8am to 6pm
==================================================
Tool plan:
I will first list the events for 23/05/2024. Then, I will create two hour-long appointments for any available time between 8am and 6pm.
Tool calls:
Tool name: list_calendar_events | Parameters: {'date': '05/23/2024'}
==================================================
Tool plan:
The available times between 8am and 6pm are: 10am-11am, 1pm-6pm. I will now create two hour-long appointments for the user during these times.
Tool calls:
Tool name: create_calendar_event | Parameters: {'date': '05/23/2024', 'duration': 1, 'time': '10:00'}
Tool name: create_calendar_event | Parameters: {'date': '05/23/2024', 'duration': 1, 'time': '13:00'}
==================================================
Final response:
I have created two hour-long appointments for you today: one at 10am and another at 1pm.
==================================================
Citations:
start=64 end=68 text='10am' document_ids=['create_calendar_event:0:4:0']
start=84 end=87 text='1pm' document_ids=['create_calendar_event:1:4:0']
Cited Documents:
{'id': 'create_calendar_event:0:4:0', 'is_success': 'true', 'message': 'Created a 1 hour long event at 10:00 on 05/23/2024', 'tool_name': 'create_calendar_event'}
{'id': 'create_calendar_event:1:4:0', 'is_success': 'true', 'message': 'Created a 1 hour long event at 13:00 on 05/23/2024', 'tool_name': 'create_calendar_event'}
==================================================
State management (memory)
The chat history for each turn consists of the following messages, and will accumulate with every new turn.
- The USER message
- Followed by the CHATBOT message with the list of tool calls
- Followed the TOOL message with the list of tool results
- Finally, followed by the CHATBOT message with the final response to the user
Question:
Considering the new appointments you made, when is my next available time?
==================================================
Tool plan:
I will list the events for 23/05/2024 and inform the user of their next available time.
Tool calls:
Tool name: list_calendar_events | Parameters: {'date': '05/23/2024'}
==================================================
Final response:
Your next available time is 2pm.
==================================================
Citations:
start=28 end=31 text='2pm' document_ids=['list_calendar_events:0:8:0']
Cited Documents:
{'existing_events': '[{"end":"8:59","start":"8:00"},{"end":"9:59","start":"9:00"},{"end":"11:59","start":"11:00"},{"end":"12:59","start":"12:00"}]', 'id': 'list_calendar_events:0:8:0', 'tool_name': 'list_calendar_events'}
==================================================
message='Create two hour-long appointments for any available time between 8am to 6pm' tool_calls=None role='USER'
message='I will first list the events for 23/05/2024. Then, I will create two hour-long appointments for any available time between 8am and 6pm.' tool_calls=[ToolCall(name='list_calendar_events', parameters={'date': '05/23/2024'})] role='CHATBOT'
tool_results=[ToolResult(call=ToolCall(name='list_calendar_events', parameters={'date': '05/23/2024'}), outputs=[{'existing_events': [{'end': '8:59', 'start': '8:00'}, {'end': '9:59', 'start': '9:00'}, {'end': '11:59', 'start': '11:00'}, {'end': '12:59', 'start': '12:00'}]}])] role='TOOL'
message='The available times between 8am and 6pm are: 10am-11am, 1pm-6pm. I will now create two hour-long appointments for the user during these times.' tool_calls=[ToolCall(name='create_calendar_event', parameters={'date': '05/23/2024', 'duration': 1, 'time': '10:00'}), ToolCall(name='create_calendar_event', parameters={'date': '05/23/2024', 'duration': 1, 'time': '13:00'})] role='CHATBOT'
tool_results=[ToolResult(call=ToolCall(name='create_calendar_event', parameters={'date': '05/23/2024', 'duration': 1, 'time': '10:00'}), outputs=[{'is_success': True, 'message': 'Created a 1 hour long event at 10:00 on 05/23/2024'}]), ToolResult(call=ToolCall(name='create_calendar_event', parameters={'date': '05/23/2024', 'duration': 1, 'time': '13:00'}), outputs=[{'is_success': True, 'message': 'Created a 1 hour long event at 13:00 on 05/23/2024'}])] role='TOOL'
message='I have created two hour-long appointments for you today: one at 10am and another at 1pm.' tool_calls=None role='CHATBOT'
message='Considering the new appointments you made, when is my next available time?' tool_calls=None role='USER'
message='I will list the events for 23/05/2024 and inform the user of their next available time.' tool_calls=[ToolCall(name='list_calendar_events', parameters={'date': '05/23/2024'})] role='CHATBOT'
tool_results=[ToolResult(call=ToolCall(name='list_calendar_events', parameters={'date': '05/23/2024'}), outputs=[{'existing_events': [{'end': '8:59', 'start': '8:00'}, {'end': '9:59', 'start': '9:00'}, {'end': '11:59', 'start': '11:00'}, {'end': '12:59', 'start': '12:00'}]}])] role='TOOL'
message='Your next available time is 2pm.' tool_calls=None role='CHATBOT'