Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

Google Calendar Toolkit

Google Calendar is a product of Google Workspace that allows users to organize their schedules and events. It is a cloud-based calendar that allows users to create, edit, and delete events. It also allows users to share their calendars with others.

Overview

This notebook will help you get started with the Google Calendar Toolkit. This toolkit interacts with the Google Calendar API to perform various operations on the calendar. It allows you to:

  • Create events.
  • Search events.
  • Update events.
  • Move events between different calendars.
  • Delete events.
  • List events.

Setup

To use this toolkit, you will need to:

  1. Have a Google account with access to Google Calendar.
  2. Set up your credentials as explained in theGoogle Calendar API docs. Once you've downloaded thecredentials.json file, you can start using the Google Calendar API.

To enable automated tracing of individual tools, set yourLangSmith API key:

# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

Installation

This toolkit lives in thelangchain-google-community package of thelangchain-google repository. We'll need thecalendar extra:

%pip install-qU langchain-google-community\[calendar\]

Instantiation

By default the toolkit reads the localcredentials.json file. You can also manually provide aCredentials object.

from langchain_google_communityimport CalendarToolkit

toolkit= CalendarToolkit()
API Reference:CalendarToolkit

Customizing Authentication

Behind the scenes, agoogleapi resource is created using the following methods. you can manually build agoogleapi resource for more auth control.

from langchain_google_communityimport CalendarToolkit
from langchain_google_community.calendar.utilsimport(
build_resource_service,
get_google_credentials,
)

# Can review scopes here: https://developers.google.com/calendar/api/auth
# For instance, readonly scope is https://www.googleapis.com/auth/calendar.readonly
credentials= get_google_credentials(
token_file="token.json",
scopes=["https://www.googleapis.com/auth/calendar"],
client_secrets_file="credentials.json",
)

api_resource= build_resource_service(credentials=credentials)
toolkit= CalendarToolkit(api_resource=api_resource)

Tools

View available tools:

tools= toolkit.get_tools()
tools
[CalendarCreateEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),
CalendarSearchEvents(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),
CalendarUpdateEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),
GetCalendarsInfo(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),
CalendarMoveEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),
CalendarDeleteEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),
GetCurrentDatetime(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>)]

Invocation

Invoke directly with args

You can invoke the tool directly by passing the required arguments in a dictionary format. Here is an example of creating a new event using theCalendarCreateEvent tool.

from langchain_google_community.calendar.create_eventimport CalendarCreateEvent

tool= CalendarCreateEvent()
tool.invoke(
{
"summary":"Calculus exam",
"start_datetime":"2025-07-11 11:00:00",
"end_datetime":"2025-07-11 13:00:00",
"timezone":"America/Mexico_City",
"location":"UAM Cuajimalpa",
"description":"Event created from the LangChain toolkit",
"reminders":[{"method":"popup","minutes":60}],
"conference_data":True,
"color_id":"5",
}
)
API Reference:CalendarCreateEvent
'Event created: https://www.google.com/calendar/event?eid=amoxdjVsM2UzMW51Yjk2czc4ajhvaGdkcGcgam9yZ2VhbmczM0Bt'

Use within an agent

Below we show how to incorporate the toolkit into anagent.

We will need a LLM or chat model:

pip install -qU "langchain[google-genai]"
import getpass
import os

ifnot os.environ.get("GOOGLE_API_KEY"):
os.environ["GOOGLE_API_KEY"]= getpass.getpass("Enter API key for Google Gemini: ")

from langchain.chat_modelsimport init_chat_model

llm= init_chat_model("gemini-2.0-flash", model_provider="google_genai")
from langgraph.prebuiltimport create_react_agent

agent_executor= create_react_agent(llm, tools)
API Reference:create_react_agent
example_query="Create a green event for this afternoon to go for a 30-minute run."

events= agent_executor.stream(
{"messages":[("user", example_query)]},
stream_mode="values",
)
for eventin events:
event["messages"][-1].pretty_print()
================================ Human Message =================================

Create a green event for this afternoon to go for a 30-minute run.
================================== Ai Message ==================================
Tool Calls:
get_current_datetime (call_drHRRhm6pdvcAuqagONUEKs5)
Call ID: call_drHRRhm6pdvcAuqagONUEKs5
Args:
================================= Tool Message =================================
Name: get_current_datetime

Time zone: America/Mexico_City, Date and time: 2025-04-02 19:07:30
================================== Ai Message ==================================
Tool Calls:
create_calendar_event (call_p60zSVMmmjTy5Ctezzmlb9zD)
Call ID: call_p60zSVMmmjTy5Ctezzmlb9zD
Args:
summary: Run
start_datetime: 2025-04-02 19:30:00
end_datetime: 2025-04-02 20:00:00
timezone: America/Mexico_City
color_id: 2
================================= Tool Message =================================
Name: create_calendar_event

Event created: https://www.google.com/calendar/event?eid=czZyZHVpcG43ajNiY241dmJmNWwycjE0NWsgam9yZ2VhbmczM0Bt
================================== Ai Message ==================================

I have created a green event for your run this afternoon. You can view it [here](https://www.google.com/calendar/event?eid=czZyZHVpcG43ajNiY241dmJmNWwycjE0NWsgam9yZ2VhbmczM0Bt). Enjoy your run!

API reference

Related


[8]ページ先頭

©2009-2025 Movatter.jp