Calendar Agent

Calendar Agent GitHub Repository

Explore the complete source code for the Calendar Agent project. This repository contains all the necessary files and scripts to set up and run the Calendar Agent using CrewAI and Composio.

1

Install the required packages

1pip install composio-crewai langchain-openai

Create a .env file and add your OpenAI API Key.

2

Import base packages

Next, we’ll import the essential libraries for our project.

1import os
2from datetime import datetime
3
4from composio_crewai import App, ComposioToolSet
5from crewai import Agent, Task
6from dotenv import load_dotenv
7from langchain_openai import ChatOpenAI
3

Initialise Language Model and Define tools

We’ll initialize our language model and set up the necessary tools for our agents.

1load_dotenv()
2
3# Initialize the language model
4llm = ChatOpenAI(model="gpt-4o")
5
6# Define tools for the agents
7# We are using Google calendar tool from composio to connect to our calendar account.
8composio_toolset = ComposioToolSet()
9tools = composio_toolset.get_tools(apps=[App.GOOGLECALENDAR])
10
11# Retrieve the current date and time
12date = datetime.today().strftime("%Y-%m-%d")
13timezone = datetime.now().astimezone().tzinfo
4

Setup Todo

Define the todo list that we want to convert into calendar events.

1# Setup Todo
2todo = """
3 1PM - 3PM -> Code,
4 5PM - 7PM -> Meeting,
5 9AM - 12AM -> Learn something,
6 8PM - 10PM -> Game
7"""
5

Create and Execute Agent

Finally, we’ll define and execute the agent responsible for creating Google Calendar events based on the todo list.

1# Create and Execute Agent.
2def run_crew():
3 calendar_agent = Agent(
4 role="Google Calendar Agent",
5 goal="""You take action on Google Calendar using Google Calendar APIs""",
6 backstory="""You are an AI agent responsible for taking actions on Google Calendar on users' behalf.
7 You need to take action on Calendar using Google Calendar APIs. Use correct tools to run APIs from the given tool-set.""",
8 verbose=True,
9 tools=tools,
10 llm=llm,
11 )
12 task = Task(
13 description=f"Book slots according to {todo}. Label them with the work provided to be done in that time period. Schedule it for today. Today's date is {date} (it's in YYYY-MM-DD format) and make the timezone be {timezone}.",
14 agent=calendar_agent,
15 expected_output="if free slot is found",
16 )
17 crew = Crew(agents=[calendar_agent], tasks=[task])
18 result = crew.kickoff()
19 print(result)

Complete Code

1# Import base packages
2import os
3from datetime import datetime
4
5from composio_crewai import App, ComposioToolSet
6from crewai import Agent, Task
7from dotenv import load_dotenv
8from langchain_openai import ChatOpenAI
9
10# Load environment variables
11load_dotenv()
12
13# Initialize the language model
14llm = ChatOpenAI(model="gpt-4o")
15
16# Define tools for the agents
17# We are using Google calendar tool from composio to connect to our calendar account.
18composio_toolset = ComposioToolSet()
19tools = composio_toolset.get_tools(apps=[App.GOOGLECALENDAR])
20
21# Retrieve the current date and time
22date = datetime.today().strftime("%Y-%m-%d")
23timezone = datetime.now().astimezone().tzinfo
24
25# Setup Todo
26todo = """
27 1PM - 3PM -> Code,
28 5PM - 7PM -> Meeting,
29 9AM - 12AM -> Learn something,
30 8PM - 10PM -> Game
31"""
32
33# Create and Execute Agent.
34def run_crew():
35 calendar_agent = Agent(
36 role="Google Calendar Agent",
37 goal="""You take action on Google Calendar using Google Calendar APIs""",
38 backstory="""You are an AI agent responsible for taking actions on Google Calendar on users' behalf.
39 You need to take action on Calendar using Google Calendar APIs. Use correct tools to run APIs from the given tool-set.""",
40 verbose=True,
41 tools=tools,
42 llm=llm,
43 )
44 task = Task(
45 description=f"Book slots according to {todo}. Label them with the work provided to be done in that time period. Schedule it for today. Today's date is {date} (it's in YYYY-MM-DD format) and make the timezone be {timezone}.",
46 agent=calendar_agent,
47 expected_output="if free slot is found",
48 )
49 crew = Crew(agents=[calendar_agent], tasks=[task])
50 result = crew.kickoff()
51 print(result)
52 return result
53
54run_crew()
Built with