メインコンテンツまでスキップ

Google Drive

Google Drive のドキュメントやフォルダを、Squid AI エージェントのコンテキストとして使用します。

Google Drive コネクタを使用すると、ユーザーが自身の Google Drive からファイルまたはフォルダを選択し、Squid AI エージェントのコンテキストとしてアップロードできるようになります。AI エージェントはその後、選択されたファイルを情報ソースとしてクライアントのプロンプトに応答できます。カスタム AI エージェントの作成について詳しくは、AI エージェントのドキュメントを参照してください

Google Drive コネクタを使用するには、Google Cloud プロジェクトと Google Cloud 側でのいくつかの設定手順が必要です。必要な手順はこのドキュメント内に、関連する Google のドキュメントへのリンクとともに記載されています。

Google Drive APIs を有効化する

この Google Drive コネクタを開始するには、まず Google Cloud プロジェクトで必要な Google API を有効化します。

  1. Google Cloud Console で、アプリケーション用のプロジェクトを選択するか、新しいプロジェクトを作成します。

  2. Google Cloud のドキュメントSet up your environment の手順に従って Google Drive API を有効化し、必要な認証情報を作成します。API keyclient IDclient secret が必要になります。

  3. Google Cloud Console で Google Picker API を有効化します。

コネクタを追加する

  1. Squid ConsoleConnectors タブに移動します。

  2. Available Connectors をクリックします。

  3. Google Drive コネクタを見つけて、Add Connector を選択します。

  4. 次の設定情報を入力します。

Connector ID: コード内でコネクタを一意に識別する文字列。

Client ID: Google Cloud Console の OAuth2 client ID。

Client Secret: Google Cloud Console の OAuth2 client secret。

Redirect URI: OAuth2 redirect URI(Google Cloud で設定したものと一致している必要があります)。

コネクタを使用する

Google Drive コネクタを作成したら、Studio または SDK のいずれかで使用できます。

Note

Squid SDK の personal storage メソッドは Squid API key へのアクセスを必要とします。API key は admin access を有効にするため、personal storage は Squid backend のような安全な admin 環境でのみ使用してください。

No-code Studio

Squid Agent Studio で no-code ソリューションを作成します。

  1. Squid ConsoleStudio タブに移動します。

  2. Create AI Agent をクリックします。

  3. Agent IDdescription を入力します(例: "gdrive-agent"、"This agent helps the user search for content inside Google Drive")。

  4. Add Abilities をクリックし、Storage セクションまでスクロールして、先ほど作成した Google Drive コネクタを選択します。

  5. エージェントがこの接続をどのように使用すべきかの指示を入力します(例: "Call this when a user asks for about Google Drive content.")。

  6. Test をクリックし、エージェントに「"Please search my Google Drive for project proposals and list them."」と尋ねてみます。

コードでの基本ビルディングブロック

Squid アプリケーションで Google Drive コネクタを使用するには、@squidcloud/google-drive-client npm パッケージをインストールします。

npm install @squidcloud/google-drive-client

Client setup

import { GoogleDriveClient, GOOGLE_DRIVE_SCOPES } from '@squidcloud/google-drive-client';
import { Squid } from '@squidcloud/client';

const squid = new Squid({
appId: 'your-app-id',
region: 'us-east-1.aws',
environmentId: 'dev',
});

const driveClient = new GoogleDriveClient(
squid,
'google_drive' // your connector ID
);

Authentication

Google Drive client は、OAuth2 トークン管理のために Squid の External Authentication API を使用します。詳細はドキュメントを参照してください。

認可コード(authorization code)を取得した後:

const response = await driveClient.saveAuthCode(authCode, userId);

Google 固有の OAuth2 要件については、Google OAuth2 documentation を参照してください。

OAuth2 Scopes

Google Drive コネクタには、次の OAuth2 scope が必要です。

const GOOGLE_DRIVE_SCOPES = ['openid', 'email', 'https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive.metadata.readonly'];

定数 GOOGLE_DRIVE_SCOPES は利便性のため @squidcloud/google-drive-client パッケージから export されています。

Google Picker API を使用する

ユーザーがコンテキストとして保存するファイルを選択できるようにするには、Google Picker API を使用します。Google のドキュメントに記載された手順に従って Google Picker を読み込み・表示し、Squid Drive client が生成した access token を 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);
};

実装に関する最新情報は、Google Picker documentation を参照してください。

ドキュメントとフォルダのインデックス作成

AI 処理向けにドキュメントをインデックスするには、indexDocumentOrFolder メソッドを使用します。個別のファイル、またはフォルダ全体(フォルダ内のすべてのファイルを再帰的にインデックス)をインデックスできます。

// Index a single document
const errors = await driveClient.indexDocumentOrFolder(documentId, userId, {
category: 'documentation',
description: 'Product documentation',
});

// Index an entire folder recursively
const folderErrors = await driveClient.indexDocumentOrFolder(folderId, userId, {
category: 'project-files',
});

if (folderErrors.length > 0) {
console.error('Some files failed to index:', folderErrors);
}

インデックス済みドキュメントの一覧表示

// List all indexed documents
const allDocuments = await driveClient.listIndexedDocuments(userId);

// List only files
const files = await driveClient.listIndexedDocuments(userId, 'file');

// List only folders
const folders = await driveClient.listIndexedDocuments(userId, 'folder');

ドキュメント内容の抽出

// Extract text content from a document
const extractedData = await driveClient.extractDataFromDocument(documentId, userId);

console.log('Pages:', extractedData.pages.length);
extractedData.pages.forEach((page, i) => {
console.log(`Page ${i + 1}: ${page.content.substring(0, 200)}...`);
});

インデックスからドキュメントを削除する

// Unindex a document or folder
const errors = await driveClient.unindexDocumentOrFolder(documentId, userId);

if (errors.length === 0) {
console.log('Successfully unindexed');
}

ドキュメントの自動同期

インデックス済みのドキュメントは、バックグラウンドの再インデックス処理により Google Drive と自動的に同期されます。これにより、AI エージェントは常に最新のコンテンツへアクセスできます。

自動同期プロセスは次を行います。

  • Detects file changes: Google Drive でファイルが変更されたときに、インデックス済みドキュメントを更新します
  • Handles deletions: Drive から削除されたときに、インデックスからドキュメントを自動的に削除します
  • Processes folder updates: フォルダ内の新規ファイル、変更、削除を監視します
  • Preserves metadata: 再インデックス中もカスタム metadata を維持します

ユーザー側での操作は不要です。いったんインデックスされれば、ドキュメントは自動的に最新状態に保たれます。

AI Agents と一緒に使う

Google Drive コネクタは、AI エージェントがインデックス済みドキュメントを扱えるようにする AI functions を提供します。ユーザー識別子(user identifier)は、エージェント上で設定するか実行時に渡すことができます。

エージェントの設定

// Configure the agent with Google Drive integration and user identifier
const agent = squid.ai().agent('document_agent');
await agent.setAgentOptionInPath('connectedIntegrations', [
{
integrationId: 'google_drive',
integrationType: 'google_drive',
options: {
identifier: userId, // unique identifier per user
},
},
]);
Note

identifier の設定に対する No-code Studio サポートは近日提供予定です。現時点では SDK を使用して identifier 付きの AI Agent を設定する必要があります。

インデックス済みドキュメントでクエリする

ドキュメントがインデックスされると、AI エージェントはインデックス済みコンテンツを使って質問に回答できます。

// The agent will search through indexed documents to answer
const response = await agent.ask('In Google Drive, what are the key points in the product documentation?');

AI functions を使用する

Google Drive コネクタは、インデックス済みドキュメントを一覧表示するための AI function を提供します。

// List documents using AI function
const response = await agent.ask('List all my indexed Google Drive documents', {
agentContext: { identifier: userId }, // Override default identifier if needed
});

動的なユーザーコンテキスト

リクエストごとにデフォルトの identifier を上書きできます。

// Use a different user's documents for this query
const response = await agent.ask('Search for project proposals in my Google Drive', {
agentContext: { identifier: differentUserId },
});

おめでとうございます!AI エージェントを通じて Google Drive 内のコンテンツを検索できるようになりました!