Calendar Agent

This project is an example which uses Composio to seamlessly convert your to-do lists into Google Calendar events. It automatically schedules tasks with specified labels and times, ensuring your calendar is always up-to-date and organized.

1

Install the required packages

Install the required packages
1pnpm add composio-core dotenv langchain @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.

JS - Import statements
1import dotenv from 'dotenv';
2import { ChatOpenAI } from "@langchain/openai";
3import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
4import { pull } from "langchain/hub";
5import { LangchainToolSet } from "composio-core";
6
7dotenv.config();
3

Initialise Language Model and Define tools

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

Models and Tools
1// Initialize the language model
2const llm = new ChatOpenAI({ model: "gpt-4-turbo" });
3
4// Define tools for the agents
5// We are using Google calendar tool from composio to connect to our calendar account.
6const composioToolset = new LangchainToolSet({
7 apiKey: process.env.COMPOSIO_API_KEY
8});
9const tools = await composioToolset.getTools({
10 actions: ["googlecalendar_create_event", "googlecalendar_list_events"]
11});
12
13// Retrieve the current date and time
14const getCurrentDate = () => new Date().toISOString().split('T')[0];
15const getTimezone = () => new Date().toLocaleTimeString('en-us', { timeZoneName: 'short' }).split(' ')[2];
16
17const date = getCurrentDate();
18const timezone = getTimezone();
4

Setup Todo

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

JS - Todo Setup
1// Setup Todo
2const todo = `
3 1PM - 3PM -> Code solo
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.

Run agent
1// Create and Execute Agent.
2async function runAgent() {
3 const prompt = await pull("hwchase17/openai-functions-agent");
4 const agent = await createOpenAIFunctionsAgent({
5 llm,
6 tools,
7 prompt
8 });
9
10 const agentExecutor = new AgentExecutor({
11 agent,
12 tools,
13 verbose: true,
14 });
15
16 const result = await agentExecutor.invoke({
17 input: `Book slots according to this todo list: ${todo}.
18 Label them with the work provided to be done in that time period.
19 Schedule it for today. Today's date is ${date} (it's in YYYY-MM-DD format)
20 and make the timezone be ${timezone}.`
21 });
22
23 console.log(result.output);
24 return "Agent execution completed";
25}
26
27runAgent().then(console.log).catch(console.error);

Complete Code

Javascript Final Code
1import dotenv from 'dotenv';
2import { ChatOpenAI } from "@langchain/openai";
3import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
4import { pull } from "langchain/hub";
5import { LangchainToolSet } from "composio-core";
6
7dotenv.config();
8
9// Initialize the language model
10const llm = new ChatOpenAI({ model: "gpt-4-turbo"});
11
12// Define tools for the agents
13const composioToolset = new LangchainToolSet({
14 apiKey: process.env.COMPOSIO_API_KEY
15});
16
17// Retrieve the current date and time
18const getCurrentDate = () => new Date().toISOString().split('T')[0];
19const getTimezone = () => new Date().toLocaleTimeString('en-us', { timeZoneName: 'short' }).split(' ')[2];
20
21const date = getCurrentDate();
22const timezone = getTimezone();
23
24// Setup Todo
25const todo = `
26 1PM - 3PM -> Code solo
27`;
28
29async function runAgent() {
30 const tools = await composioToolset.getTools({
31 actions: ["googlecalendar_create_event", "googlecalendar_list_events"]
32});
33 const prompt = await pull("hwchase17/openai-functions-agent");
34 const agent = await createOpenAIFunctionsAgent({
35 llm,
36 tools,
37 prompt
38 });
39
40const agentExecutor = new AgentExecutor({
41 agent,
42 tools,
43 verbose: true,
44});
45
46const result = await agentExecutor.invoke({
47 input: `Book slots according to this todo list: ${todo}.
48 Label them with the work provided to be done in that time period.
49 Schedule it for today. Today's date is ${date} (it's in YYYY-MM-DD format)
50 and make the timezone be ${timezone}.`
51});
52
53console.log(result.output);
54return "Agent execution completed";
55}
56
57runAgent().then(console.log).catch(console.error);