Configure LlamaIndex to read from Google Calendar

Using data loaders, we can give LlamaIndex access to a Google Calendar to gain information about schedules and events.

Posted: May 9, 2023 Updated:
Find more posts about:

LlamaIndex can read from the Google Calendar API, but to do so, you must first configure a credentials file.

To generate the credentials file, first navigate to this guide on creating credentials. To read from the Calendar, we’ll need an OAuth client ID (see relevant heading). Click the Go to Credentials button and navigate to the Credentials dashboard.

If you don’t yet have an account, sign up.

If you don’t yet have a service account, make one.

Next, navigate to the Google API Library, search for the Google Calendar API, and click Enable.

Return to the Credentials dashboard, click Create Credentials, and choose OAuth client ID. Fill out the required details on the first screen (OAuth consent screen).

Next, you’ll need to add a scope to read from the calendar. Click Add or remove scopes and add the following scope from the list:

  • .../auth/calendar.readonly

Return to the Credentials dashboard, click Create Credentials, and click OAuth client ID. Under Application type, choose Desktop app, add a name, then click Create.

On the pop-up window that appears, click Download json and save your credentials file as credentials.json. This credentials file should live alongside the python script you’ll use.

The script

Place the following script in a python file alongside your credentials file:

# llama_index 0.6.5
from llama_index import GPTVectorStoreIndex, download_loader

# Setup data loader
GoogleCalendarReader = download_loader('GoogleCalendarReader')
loader = GoogleCalendarReader()

# load data
documents = loader.load_data()
index = GPTVectorStoreIndex.from_documents(documents)

# query model
query_engine = index.as_query_engine()
response = query_engine.query('<your question>')

When you run the application for the first time, a browser window will open asking you to consent to API access.

Once you consent, the script will run and answer your question.

Keep in mind that the default model for LlamaIndex is text-davinci-003. See Change the model used by LlamaIndex. The model is not aware of the current date.

Access calendar events from other users’ calendars

The default LlamaIndex Google Calendar loader only loads calendar events for the authenticated user. In order to access calendar events for other users as well, I’ve written my own Google Calendar loader.

Problems using a Vector store for calendar events

The basic index used in LlamaIndex examples is the GPTSimpleVectorIndex. However, a vector store is most useful for creating a cloud of ideas and doing text analysis. It is not a good data structure for handling calendaring events.

Here are all the LlamaIndex index types.

A better index type for calendar events is probably the List index. The pros to this are that the LLM can examine all of the events to come to a conclusion. The cons are that we need to pass ALL the events through to the model, and this takes a while and uses up a lot of tokens. Running a test with this method took over a minute for a response when comparing only two users’ calendars.

It seems like it would be better to handle calendaring logic in code itself, and have the LLM be the conversationalist/middle-man between the user and the code.