AI Agent と Agent API Key の保護
@secureAiAgent decorator を使用して、指定 agent を保護する function を指定します。Agent API Key を使用して security method を bypass し、App API Key を不要にします。
Squid Client を使用して AI agent を作成し chat を有効にする場合、data の保護は重要です。AI agent と agent との chat には sensitive information が含まれる可能性があるため、unauthorized usage や modification を防ぐために access と update を制限することが重要です。
AI agent が public に設定されていない限り、access は default で制限され、access permission を設定する明示的な rule がなければ機能しません。(ぜひ試してみてください!)
Squid は、security rule の設定に使用できる @secureAiAgent decorator を提供します。これらの rule は、unauthorized user による agent の update や unauthorized chat を防ぎ、data の integrity を保護します。
security function は boolean を返します。true は request を許可し、false は拒否します。
AI agent の保護を試す前に、保護したい agent の public setting が OFF に toggle されていること、および backend を setupしていることを確認してください。AI agent に security rule を設定するにはこれらが必要です。
Chat の保護
chat permission を管理するには、@secureAiAgent decorator を使用します。すべての user に対してすべての agentとの chat を有効にするには、backend に以下を追加します。
import { secureAiAgent, SquidService } from '@squidcloud/backend';
export class ExampleService extends SquidService {
@secureAiAgent()
allowChat(): boolean {
return true;
}
}
chat を authenticated user のみに制限するには、Squid backend の isAuthenticated() method を使用します。この method は、action を実行しようとする client が authenticated かどうかを示す boolean を返します。
@secureAiAgent()
allowChat(): boolean {
return this.isAuthenticated();
}
すべての agent に対する access の open または close は非常に広範囲であるため、access security を構成する対象の Agent ID を指定することを推奨します。
指定するには、AI agent の ID を @secureAiAgent decorator に追加します。
@secureAiAgent('AGENT_ID')
Conditional Check
API では、client が query とともにさまざまな option を送信でき、これらを制限したい場合があります。
たとえば、query option では agent に設定された model とは異なる AI model を使用するよう指定できます。これを制限するには、次の method で options.model に value が指定されているか確認し、指定されている場合は failure にします。
@secureAiAgent()
immutableModel(context: SecureAiAgentContext): boolean {
if (context.options?.model !== undefined) {
// Don't allow overriding the model that was configured for the agent.
return false;
}
return this.isAuthenticated();
}
他にも多数の option があります。どこから始めるべきかわからない場合、最も安全なのはすべての option を拒否することです。
@secureAiAgent()
immutableAgent(context: SecureAiAgentContext): boolean {
const options = context.options || {};
if (Object.keys(options).length > 0) {
// Don't allow any options
return false;
}
return this.isAuthenticated();
}
block または allow できる その他すべての optionについて確認してください。
Agent API Key
agent は、agent security rule を bypass するために App API Key の代わりに使用できる独自の API key を持つことができます。App API Key は destructive action を含む application 全体への full access を提供できるため、security risk を最小化するには、Agent に scope された API Key の使用が役立ちます。
制限事項
- Agent API Key は Agent-based action にのみ使用できるため、有用性が制限されます。
- App API Key が存在する場合、Agent API Key は無視されます。
使用方法
Agent API Key を使用するには、Agent Client を作成するときに option array の一部として渡します。
/**
* Note the lack of App API Key when creating the Squid instance. This is important as passing an API Key here will cause the Agent API Key to be ignored.
*/
const squid = new Squid({
appId: 'YOUR_APP_ID',
region: 'YOUR_REGION',
environmentId: 'dev',
squidDeveloperId: 'YOUR_SQUID_DEVELOPER_ID',
});
const agentClient = await squid.ai().agent('banking-copilot', {
apiKey: process.env.SQUID_AGENT_API_KEY,
});
API call を直接実行する場合は、x-squid-agent-api-key header を使用できます。
curl -X POST "https://console.us-east-1.aws.squid.cloud/openapi/squid-api/v1/ai/agent/ask" \
-H "Content-Type: application/json" \
-H "x-squid-agent-api-key: YOUR_AGENT_API_KEY" \
-d '{
"agentId": "lkj",
"prompt": "What is a squid?"
}'