跳到主要内容
Google 日历是 Google Workspace 的产品,允许用户组织他们的日程和活动。它是一个基于云的日历,允许用户创建、编辑和删除活动。它还允许用户与他人共享他们的日历。

概览

本笔记本将帮助您开始使用 Google 日历工具包。此工具包与 Google 日历 API 交互,以对日历执行各种操作。它允许您
  • 创建活动。
  • 搜索活动。
  • 更新活动。
  • 在不同的日历之间移动活动。
  • 删除活动。
  • 列出活动。

设置

要使用此工具包,您需要
  1. 拥有一个可以访问 Google 日历的 Google 帐户。
  2. 按照 Google 日历 API 文档 中所述设置您的凭据。下载 credentials.json 文件后,您就可以开始使用 Google 日历 API 了。
要启用单个工具的自动化跟踪,请设置您的 LangSmith API 密钥
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

安装

此工具包位于 langchain-google 存储库的 langchain-google-community 包中。我们需要 calendar 额外功能
pip install -qU langchain-google-community\[calendar\]

实例化

默认情况下,该工具包读取本地 credentials.json 文件。您也可以手动提供 Credentials 对象。
from langchain_google_community import CalendarToolkit

toolkit = CalendarToolkit()

自定义认证

在幕后,使用以下方法创建了一个 googleapi 资源。您可以手动构建 googleapi 资源以获得更多身份验证控制。
from langchain_google_community import CalendarToolkit
from langchain_google_community.calendar.utils import (
    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 = 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>)]

调用

直接使用参数调用

您可以通过字典格式传递所需参数来直接调用该工具。这是一个使用 CalendarCreateEvent 工具创建新活动的示例。
from langchain_google_community.calendar.create_event import 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",
    }
)
'Event created: https://www.google.com/calendar/event?eid=amoxdjVsM2UzMW51Yjk2czc4ajhvaGdkcGcgam9yZ2VhbmczM0Bt'

在代理中使用

下面我们展示如何将工具包集成到 代理 中。 我们将需要一个 LLM 或聊天模型:
# | output: false
# | echo: false

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
from langchain.agents import create_agent


agent_executor = create_agent(llm, tools)
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 event in 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 参考

  • 有关 Google 日历 API 的更多详细信息,请参阅 Google 日历 API 概述
  • 有关所有 Google 日历工具包功能和配置的详细文档,请访问 日历文档

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.