Calendar Agent with Native Multi Step Tool

In the example below, we demonstrate how to use the cohere Chat API with the list_calendar_events and create_calendar_event tools to book appointments. Booking the correct appointment requires the model to first check for an available slot by listing existing events, reasoning about the correct slot to book the new appointment and then finally invoking the right tool to create the calendar event. To learn more about Tool Use, read the official multi-step tool use guide.

PYTHON
1# !pip install cohere==5.5.3
PYTHON
1# Instantiate the Cohere client
2
3import cohere
4import os
5
6COHERE_API_KEY = os.environ["COHERE_API_KEY"]
7co = cohere.Client(api_key=COHERE_API_KEY)
PYTHON
1# Define the tools
2
3import json
4
5def list_calendar_events(date: str):
6 events = '[{"start": "14:00", "end": "15:00"}, {"start": "15:00", "end": "16:00"}, {"start": "17:00", "end": "18:00"}]'
7 print(f"Listing events: {events}")
8 return events
9
10def create_calendar_event(date: str, time: str, duration: int):
11 print(f"Creating a {duration} hour long event at {time} on {date}")
12 return True
13
14list_calendar_events_tool = {
15 "name": "list_calendar_events",
16 "description": "returns a list of calendar events for the specified date, including the start time and end time for each event",
17 "parameter_definitions": {
18 "date": {
19 "description": "the date to list events for, formatted as mm/dd/yy",
20 "type": "str",
21 "required": True
22 }
23 }
24}
25
26create_calendar_event_tool = {
27 "name": "create_calendar_event_tool",
28 "description": "creates a calendar event of the specified duration at the specified time and date",
29 "parameter_definitions": {
30 "date": {
31 "description": "the date on which the event starts, formatted as mm/dd/yy",
32 "type": "str",
33 "required": True
34 },
35 "time": {
36 "description": "the time of the event, formatted using 24h military time formatting",
37 "type": "str",
38 "required": True
39 },
40 "duration": {
41 "description": "the number of hours the event lasts for",
42 "type": "float",
43 "required": True
44 }
45 }
46}
47
48# helper function for routing to the correct tool
49def invoke_tool(tool_call: cohere.ToolCall):
50 if tool_call.name == list_calendar_events_tool["name"]:
51 date = tool_call.parameters["date"]
52 return [{
53 "events": list_calendar_events(date)
54 }]
55 elif tool_call.name == create_calendar_event_tool["name"]:
56 date = tool_call.parameters["date"]
57 time = tool_call.parameters["time"]
58 duration = tool_call.parameters["duration"]
59
60 return [{
61 "is_success": create_calendar_event(date, time, duration)
62 }]
63 else:
64 raise f"Unknown tool name '{tool_call.name}'"
PYTHON
1# Check what tools the model wants to use and how to use them
2res = co.chat(
3 model="command-r-plus",
4 preamble="Today is Thursday, may 23, 2024",
5 message="book an hour long appointment for the first available free slot after 3pm",
6 force_single_step=False,
7 tools=[list_calendar_events_tool, create_calendar_event_tool])
8
9while res.tool_calls:
10 print(res.text) # This will be an observation and a plan with next steps
11
12 # invoke the recommended tools
13 tool_results = []
14 for call in res.tool_calls:
15 tool_results.append({"call": call, "outputs": invoke_tool(call)})
16
17 # send back the tool results
18 res = co.chat(
19 model="command-r-plus",
20 chat_history=res.chat_history,
21 message="",
22 force_single_step=False,
23 tools=[list_calendar_events_tool, create_calendar_event_tool],
24 tool_results=tool_results,
25 )
26
27print(res.text) # print the final answer
Output
I will check the user's calendar for today after 3pm and book an hour-long appointment in the first available slot.
Listing events: [{"start": "14:00", "end": "15:00"}, {"start": "15:00", "end": "16:00"}, {"start": "17:00", "end": "18:00"}]
The user has events scheduled from 2pm to 4pm and from 5pm to 6pm. I will book an hour-long appointment from 4pm to 5pm.
Creating a 1 hour long event at 16:00 on 05/23/2024
I've booked an hour-long appointment for you today from 4pm to 5pm.