Connect Google Drive to Squid AI
Use documents and folders from your Google Drive as context for your Squid AI agent.
The Google Drive connector allows you to let users choose files or folders from their Google Drive to upload as context for their Squid AI agent. An AI agent can then respond to client prompts using the selected files as sources of information. For more information on creating custom AI agents, view the AI agent documentation.
Using the Google Drive connector requires a Google Cloud project and some configuration steps for Google Cloud. The required steps are outlined in this documentation along with links to relevant Google documentation.
Enable Google Drive APIs
To get started with this Google Drive connector, first enable the required Google APIs on your Google Cloud project.
In the Google Cloud Console, select a project for your application or create a new project.
Follow the Set up your environment steps in the Google Cloud documentation to enable the Google Drive API and create the necessary credentials. You will need your API key, client ID, and client secret.
In the Google Cloud Console, enable the Google Picker API.
Add the integration
In the Squid Console, navigate to the Integrations tab.
Provide the following details:
Integration ID: A unique identifier for the integration. Use this as a reference to the integration in your code.
Client ID: Your Google OAuth 2.0 Client ID you created in the Google Cloud Console. The client ID ends with 'apps.googleusercontent.com'.
Redirect URI: For use with requests from a web server. Do not include a trailing slash.
Client Secret: Your Google OAuth 2.0 Client Secret. This is only required when managing your OAuth in the backend. Stored securely using Squid Secrets.
Click Test Connection to verify the connection is successful. If an error appears, check your Client ID and Client Secret values.
When the connection is successful, click Add Integration to save.
Using the connection
The personal storage methods of the Squid SDK require access to your Squid API key. The API key enables admin access, so only use personal storage in a secure admin environment like the Squid backend.
Initializing the personal storage client
To initialize the connection to Google Drive, use the personalStorage
method, passing the integration ID for your Drive connection:
const driveClient = this.squid.personalStorage('INTEGRATION_ID');
Getting a Google API authorization code
To permit Squid AI to access files, you must provide a Google API authorization code. This code is generated using OAuth 2.0. To view the JavaScript API for creating the authorization code, check out the Google Developers Identity API reference. To learn more about Google OAuth 2.0 authentication and authorization, view the Google Developers documentation.
Passing the authorization code to Squid
Once you have your authorization code, provide it to Squid using the saveAuthCode
method, passing the Google authorization code and a custom string to uniquely identify the Google Drive connection.
const driveClient = this.squid.personalStorage('INTEGRATION_ID');
const response = await driveClient.saveAuthCode(
'GOOGLE_ACCESS_CODE',
'CUSTOM_IDENTIFIER'
);
console.log(response.accessToken); // ya29.a1BcD234e...
This function returns a promise that resolves to an access token to use with the Google Picker API.
Save the custom identifier for your reference. A user ID can be a useful custom identifier, for example. You only need to run this function once per custom identifier, as from then on, users can access the Google Picker using an access token managed by Squid.
Retrieving an access token
By saving your authorization code using the saveAuthCode
method, Squid automatically handles generating access tokens for you.
const driveClient = this.squid.personalStorage('INTEGRATION_ID');
const response = await driveClient.getAccessToken('CUSTOM_IDENTIFIER');
console.log(response.accessToken); // ya29.a1BcD234e...
Passing the token to the Google Picker API
To allow users to select files to save as context, use the Google Picker API. Follow the steps provided in the Google documentation to load and display the Google Picker, passing the access token generated by your Squid personal storage instance as the value for accessToken
.
const showPicker = () => {
// TODO(developer): Replace with your API key
const picker = new google.picker.PickerBuilder()
.addView(google.picker.ViewId.DOCS)
.setOAuthToken(accessToken) // From Squid
.setDeveloperKey('API_KEY')
.setCallback(pickerCallback)
.setAppId(APP_ID)
.build();
picker.setVisible(true);
};
Refer to the Google Picker documentation for the most up-to-date information on implementation.
Adding chosen files as context
Once a client selects files or folders to use as context, pass the array to your secure environment, and then use the indexDocumentOrFolder
method to add the selections as context.
The simplest way to securely pass information to the Squid backend is through Squid Executables. The following code uses a Squid Executable to pass the array to the Squid backend:
function pickerCallback(data) {
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
const docs = data[google.picker.Response.DOCUMENTS];
squid.executeFunction('addToStorage', docs);
}
}
To add a document to a Squid AI agent, use the indexDocumentOrFolder
method, passing the ID of the document obtained from the Google Picker, the custom identifier for the user's personal storage and the ID of the AI agent.
The following example shows using a Squid Executable to iterate through the results obtained from the Google Picker and add them as context to the given AI agent profile:
@executable()
async addToStorage(docs: any[]): Promise<string | any> {
try {
docs.map(async (doc) => {
await this.squid
.personalStorage('INTEGRATION_ID')
.indexDocumentOrFolder(
doc.id,
'CUSTOM_IDENTIFIER',
'AGENT_ID',
'ai_agents',
);
})
return 'success';
} catch (error) {
console.error(error);
return error;
}
Next steps
To learn how to implement your AI agent, view the AI agent documentation.