AI Scheduling Agent

This project demonstrates how to use Composio to create a scheduling agent.

Overview

The AI Scheduling Agent is an automated assistant that reads your emails, schedules meetings in Google Calendar, and sends confirmation emails - all without manual intervention. It streamlines the meeting scheduling process by handling the back-and-forth communication automatically.

Getting Started

1

Installation

install dependencies
$pip install composio-llamaindex python-dotenv
2

Connecting to tools and models

connect to required tools
$composio add gmail
>composio add googlecalendar
>
>export OPENAI_API_KEY="<your-openai-api-key>"
3

Importing the required libraries

import required libraries
1import os
2import time
3import dotenv
4import re
5from datetime import datetime
6from composio_llamaindex import App, ComposioToolSet, Action
7from llama_index.core.agent import FunctionCallingAgentWorker
8from llama_index.core.llms import ChatMessage
9from llama_index.llms.openai import OpenAI
10
11from composio.client.collections import TriggerEventData
12
13dotenv.load_dotenv()
4

Initializing the Tools and the LLM

initialize toolset and llm
1toolset = ComposioToolSet(api_key="")
2schedule_tool = composio_toolset.get_tools(
3 actions=[
4 Action.GOOGLECALENDAR_FIND_FREE_SLOTS,
5 Action.GOOGLECALENDAR_CREATE_EVENT,
6 Action.GMAIL_CREATE_EMAIL_DRAFT
7 ]
8)
9
10llm = OpenAI(model="gpt-4o")
11
12date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
13timezone = datetime.now().astimezone().tzinfo
5

Create callback function

create callback function
1listener = composio_toolset.create_trigger_listener()
2@listener.callback(filters={"trigger_name": "GMAIL_NEW_GMAIL_MESSAGE"})
3def callback_new_message(event: TriggerEventData) -> None:
4 # Using the information from Trigger, execute the agent
5 print("here in the function")
6 payload = event.payload
7 thread_id = payload.get("threadId")
8 message = payload.get("messageText")
9 sender_mail = payload.get("sender")
10 if sender_mail is None:
11 print("No sender email found")
12 return
13 print(sender_mail)
14
15 prefix_messages = [
16 ChatMessage(
17 role="system",
18 content=(
19 f"""
20 You are an AI assistant specialized in creating calendar events based on email information.
21 Current DateTime: {date_time} and timezone {timezone}. All the conversations happen in IST timezone.
22 Pass empty config ("config": {{}}) for the function calls, if you get an error about not passing config.
23 Analyze email, and create event on calendar depending on the email content.
24 You should also draft an email in response to the sender of the previous email
25 """
26
27 ),
28 )
29 ]
30 agent = FunctionCallingAgentWorker(
31 tools=schedule_tool, # Tools available for the agent to use
32 llm=llm, # Language model for processing requests
33 prefix_messages=prefix_messages, # Initial system messages for context
34 max_function_calls=10, # Maximum number of function calls allowed
35 allow_parallel_tool_calls=False, # Disallow parallel tool calls
36 verbose=True, # Enable verbose output
37 ).as_agent()
38 analyze_email_task = f"""
39 1. Analyze the email content and decide if an event should be created.
40 a. The email was received from {sender_mail}
41 b. The content of the email is: {message}
42 c. The thread id is: {thread_id}.
43 2. If you decide to create an event, try to find a free slot
44 using Google Calendar Find Free Slots action.
45 3. Once you find a free slot, use Google Calendar Create Event
46 action to create the event at a free slot and send the invite to {sender_mail}.
47
48 If an event was created, draft a confirmation email for the created event.
49 The receiver of the mail is: {sender_mail}, the subject should be meeting scheduled and body
50 should describe what the meeting is about
51 """
52 response = agent.chat(analyze_email_task)
53 print(response)
6

Activating the Listener

run the agent
1print("Listener started!")
2print("Waiting for email")
3listener.listen()
7

Final Code

final code
1# Import necessary libraries
2import os
3import time
4import dotenv
5import re
6from datetime import datetime
7from composio_llamaindex import App, ComposioToolSet, Action
8from llama_index.core.agent import FunctionCallingAgentWorker
9from llama_index.core.llms import ChatMessage
10from llama_index.llms.openai import OpenAI
11
12from composio.client.collections import TriggerEventData
13
14dotenv.load_dotenv()
15
16composio_toolset = ComposioToolSet()
17
18schedule_tool = composio_toolset.get_tools(
19 actions=[
20 Action.GOOGLECALENDAR_FIND_FREE_SLOTS,
21 Action.GOOGLECALENDAR_CREATE_EVENT,
22 Action.GMAIL_CREATE_EMAIL_DRAFT
23 ]
24)
25
26llm = OpenAI(model="gpt-4o")
27
28date_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
29timezone = datetime.now().astimezone().tzinfo
30
31listener = composio_toolset.create_trigger_listener()
32@listener.callback(filters={"trigger_name": "GMAIL_NEW_GMAIL_MESSAGE"})
33def callback_new_message(event: TriggerEventData) -> None:
34 # Using the information from Trigger, execute the agent
35 print("here in the function")
36 payload = event.payload
37 thread_id = payload.get("threadId")
38 message = payload.get("messageText")
39 sender_mail = payload.get("sender")
40 if sender_mail is None:
41 print("No sender email found")
42 return
43 print(sender_mail)
44
45 prefix_messages = [
46 ChatMessage(
47 role="system",
48 content=(
49 f"""
50 You are an AI assistant specialized in creating calendar events based on email information.
51 Current DateTime: {date_time} and timezone {timezone}. All the conversations happen in IST timezone.
52 Pass empty config ("config": {{}}) for the function calls, if you get an error about not passing config.
53 Analyze email, and create event on calendar depending on the email content.
54 You should also draft an email in response to the sender of the previous email
55 """
56
57 ),
58 )
59 ]
60 agent = FunctionCallingAgentWorker(
61 tools=schedule_tool,
62 llm=llm,
63 prefix_messages=prefix_messages,
64 max_function_calls=10,
65 allow_parallel_tool_calls=False,
66 verbose=True,
67 ).as_agent()
68 analyze_email_task = f"""
69 1. Analyze the email content and decide if an event should be created.
70 a. The email was received from {sender_mail}
71 b. The content of the email is: {message}
72 c. The thread id is: {thread_id}.
73 2. If you decide to create an event, try to find a free slot
74 using Google Calendar Find Free Slots action.
75 3. Once you find a free slot, use Google Calendar Create Event
76 action to create the event at a free slot and send the invite to {sender_mail}.
77
78 If an event was created, draft a confirmation email for the created event.
79 The receiver of the mail is: {sender_mail}, the subject should be meeting scheduled and body
80 should describe what the meeting is about
81 """
82 response = agent.chat(analyze_email_task)
83 print(response)
84
85print("Listener started!")
86print("Waiting for email")
87listener.listen()