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

Google Drive

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

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

Google Drive connector を使用するには、Google Cloud project と Google Cloud 側でのいくつかの設定手順が必要です。必要な手順は、このドキュメント内で関連する Google のドキュメントへのリンクとともに説明します。

Google Drive APIs を有効化する

この Google Drive connector を開始するには、まず Google Cloud project 上で必要な Google APIs を有効化してください。

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

  2. Google Cloud documentationSet up your environment 手順に従って、Google Drive API を有効化し、必要な credentials を作成します。API keyclient IDclient secret が必要になります。

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

connector を追加する

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

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

  3. Google Drive connector を見つけ、Add Connector を選択します。

  4. 次の設定詳細を入力します:

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

Client ID: Google Cloud Console の OAuth2 client ID。

Client Secret: Google Cloud Console の OAuth2 client secret。

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

connector を使用する

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

Note

Squid SDK の personal storage methods は、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 connector を選択します。

  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 connector を使用するには、@squidcloud/google-drive-client npm package をインストールします。

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 token 管理のために Squid の External Authentication API を使用します。詳細はドキュメントを参照してください。

authorization code を取得した後:

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

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

OAuth2 Scopes

Google Drive connector には、次の OAuth2 scopes が必要です:

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 package から export されています。

Google Picker API の使用

ユーザーがコンテキストとして保存するファイルを選択できるようにするには、Google Picker API を使用します。Google documentation に記載されている手順に従って Google Picker をロードして表示し、accessToken の値には Squid Drive client によって生成された access token を渡してください。

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 を参照してください。

ドキュメントとフォルダの indexing

AI 処理用にドキュメントを indexing するには、indexDocumentOrFolder method を使用します。個別のファイル、またはフォルダ全体(フォルダ内の全ファイルを再帰的に indexing)を indexing できます。

// 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);
}

indexing 済みドキュメントの一覧表示

// 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)}...`);
});

index からドキュメントを削除する

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

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

自動ドキュメント同期

indexing 済みドキュメントは、バックグラウンドの再 indexing プロセスを通じて Google Drive と自動的に同期されます。これにより、AI agent は常に最新のコンテンツへアクセスできます。

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

  • ファイル変更の検出: Google Drive で変更された際に indexing 済みドキュメントを更新します
  • 削除の処理: Drive から削除されたドキュメントを index から自動的に削除します
  • フォルダ更新の処理: 新規ファイル、変更、削除についてフォルダを監視します
  • metadata の保持: 再 indexing 中もカスタム metadata を維持します

特別な操作は不要です。いったん indexing されれば、ドキュメントは自動的に最新状態に保たれます。

AI Agents と一緒に使用する

Google Drive connector は、AI agent が indexing 済みドキュメントを扱えるようにする AI functions を提供します。ユーザー識別子(user identifier)は agent 上で設定するか、実行時(runtime)に渡すことができます。

agent の設定

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

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

indexing 済みドキュメントでの Querying

ドキュメントが indexing されると、AI agent は indexing 済みコンテンツを使って質問に回答できます。

// 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 connector は、indexing 済みドキュメントを一覧表示する 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
}
);

動的 user context

リクエストごとにデフォルトの 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 agent を通じて Google Drive 内のコンテンツを検索できるようになりました!