AI 関数
AI 関数はカスタム機能を用いて AI エージェントを強化・拡張します
あなたが AI agents または AI assistants を設定する際、特定の質問に対して、指示だけでは設定が難しい特定の応答を求められる場合があります。例えば、モデルがあまり馴染みのない業界固有の計算式がある場合や、特定のプロンプトに反応してエージェントにアクションを実行させたい場合などです。
このような場合(およびその他多数のケース)には、Squid の AI 関数を利用できます。Squid Service 内の関数に @aiFunction デコレーターを付加することで、エージェントをこれらの関数に接続し、各エージェントに必要な機能をカスタマイズできるようになります。
これらの関数は、Typescript で記述された Squid Service 内に定義されているため、Squid プラットフォームが持つ全ての機能にアクセス可能です。つまり、これらの関数のカスタマイズには限界がありません。
AI 関数の作成方法
AI 関数は、関数を説明する文字列である description パラメーターを受け取ります。この説明は、AI エージェントがいつこの関数を使用するかを判断するために利用されるので、明確で有益なものであることが重要です。
以下の例では、AI エージェントが使用可能な関数を定義するために @aiFunction デコレーターを使用しています:
Backend code
import { SquidService, aiFunction } from '@squidcloud/backend';
class AiService extends SquidService {
  @aiFunction<{ shipName: string }>(
    'This function returns the list of pirates in a ship. ' +
    'Call this function whenever the user asks about the crew of a ship.',
    [
      {
        name: 'shipName',
        description: 'The name of the ship',
        type: 'string',
        required: true,
      }
    ]
  )
  async listPiratesInAShip(params: { shipName: string }): Promise<string> {
    const { shipName } = params;
    if (shipName === 'Black Pearl') {
      return 'Jack Sparrow';
    }
    else if (shipName === "Queen Anne's Revenge") {
      return 'Black Beard';
    }
    return 'No ship found';
  }
}
AI 関数をエージェントに追加するには、ask のオプションパラメーターの一部として関数名を渡します:
const response = await squid
  .ai()
  .agent('AGENT_ID')
  .ask('Which pirate was captain of the Black Pearl?', {
    functions: ['listPiratesInAShip'],
  });
const assistantId = await createAssistant('YOUR_ASSISTANT_NAME', "YOUR ASSISTANT'S INSTRUCTIONS", ['listPiratesInAShip']);
次のステップ
より複雑な AI 関数の実例を見てみたいですか?AI home maintenance tutorial をご覧ください。