-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add Google Calendar Toolkit documentation notebook
- Loading branch information
1 parent
e3d2e94
commit 3ea34f3
Showing
1 changed file
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "plaintext" | ||
} | ||
}, | ||
"source": [ | ||
"# Google Calendar Toolkit\n", | ||
"\n", | ||
"> [Google Calendar](https://workspace.google.com/intl/en-419/products/calendar/) is a product of Google 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.\n", | ||
"\n", | ||
"This notebook will help you getting 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, search, update, and delete events on your calendar. It also allows you to list events on your calendar.\n", | ||
"\n", | ||
"## Setup\n", | ||
"\n", | ||
"To use this toolkit, you will need to set up your credentials explained in the [Google Calendar API docs](https://developers.google.com/calendar/api/quickstart/python#authorize_credentials_for_a_desktop_application). Once you've downloaded the `credentials.json` file, you can start using the Google Calendar API." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Installation\n", | ||
"\n", | ||
"This toolkit requires the Google Python libraries:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%pip install --upgrade --quiet google-api-python-client google-auth-httplib2 google-auth-oauthlib\n", | ||
"%pip install -qU langchain-community" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Usage\n", | ||
"\n", | ||
"By default the toolkit reads the local `credentials.json` file. You can also manually provide a `Credentials` object.\n", | ||
"\n", | ||
"### Build the resource rervice and tollkit" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_community.agent_toolkits import GoogleCalendarToolkit\n", | ||
"from langchain_community.tools.google_calendar.utils import (\n", | ||
" build_resource_service,\n", | ||
" get_google_calendar_credentials, \n", | ||
")\n", | ||
"\n", | ||
"# Can review scopes here: https://developers.google.com/calendar/api/auth \n", | ||
"credentials = get_google_calendar_credentials(\n", | ||
" token_file=\"token.json\",\n", | ||
" scopes=[\"https://www.googleapis.com/auth/calendar\"],\n", | ||
" client_secrets_file=\"credentials.json\",\n", | ||
")\n", | ||
"api_resource = build_resource_service(credentials=credentials)\n", | ||
"toolkit = GoogleCalendarToolkit(api_resource=api_resource)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Tools\n", | ||
"\n", | ||
"View available tools:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[CalendarCreateEvent(api_resource=<googleapiclient.discovery.Resource object at 0x109ef48f0>),\n", | ||
" CalendarSearchEvents(api_resource=<googleapiclient.discovery.Resource object at 0x109ef48f0>),\n", | ||
" CalendarUpdateEvent(api_resource=<googleapiclient.discovery.Resource object at 0x109ef48f0>),\n", | ||
" GetCalendarsInfo(api_resource=<googleapiclient.discovery.Resource object at 0x109ef48f0>),\n", | ||
" CalendarMoveEvent(api_resource=<googleapiclient.discovery.Resource object at 0x109ef48f0>),\n", | ||
" CalendarDeleteEvent(api_resource=<googleapiclient.discovery.Resource object at 0x109ef48f0>)]" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"tools = toolkit.get_tools()\n", | ||
"tools" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Use within an agent\n", | ||
"\n", | ||
"Below we show how to incorporate the toolkit into an Agent.\n", | ||
"\n", | ||
"We will need a LLM or chat model:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_openai import ChatOpenAI\n", | ||
"\n", | ||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langgraph.prebuilt import create_react_agent\n", | ||
"\n", | ||
"agent_executor = create_react_agent(llm, tools)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"================================\u001b[1m Human Message \u001b[0m=================================\n", | ||
"\n", | ||
"Create an event with invite to [email protected] to invite him to have a coffee this afternoon\n", | ||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n", | ||
"Tool Calls:\n", | ||
" create_calendar_event (call_Xs3ik5vOlYA53crrXbZslsGh)\n", | ||
" Call ID: call_Xs3ik5vOlYA53crrXbZslsGh\n", | ||
" Args:\n", | ||
" summary: Coffee Invitation\n", | ||
" start_datetime: 2024-11-27 15:00:00\n", | ||
" end_datetime: 2024-11-27 16:00:00\n", | ||
" attendees: ['[email protected]']\n", | ||
" description: Let's meet for coffee this afternoon!\n", | ||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n", | ||
"Name: create_calendar_event\n", | ||
"\n", | ||
"Event created: https://www.google.com/calendar/event?eid=bXZuM2syZ2ZiMjBsZXNlbzRraHE0YTczM28gam9yZ2VhbmczM0Bt\n", | ||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n", | ||
"\n", | ||
"I have created the event for a coffee invitation this afternoon and sent an invite to **[email protected]**. You can view the event [here](https://www.google.com/calendar/event?eid=bXZuM2syZ2ZiMjBsZXNlbzRraHE0YTczM28gam9yZ2VhbmczM0Bt).\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"example_query = \"Create an event with invite to [email protected] to \\\n", | ||
"invite him to have a coffee this afternoon\"\n", | ||
"\n", | ||
"events = agent_executor.stream(\n", | ||
" {\"messages\": [(\"user\", example_query)]},\n", | ||
" stream_mode=\"values\",\n", | ||
")\n", | ||
"for event in events:\n", | ||
" event[\"messages\"][-1].pretty_print()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "langchain_gcalendar-Z32eEg3T", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |