Eleven v4を発表これまでで最も感情豊かなモデル、Eleven v4をご紹介します。

コンテンツへ移動

サンタのボイスエージェントをReactアプリに数分で追加

公開日

聴くこの記事を聴く

このホリデーシーズンは、アプリにサンタを追加しましょう。

ElevenLabsのAgents Platformと@elevenlabs/react SDKを使えば、サンタクロースの音声エージェントをわずか数分でReactアプリに追加できます。このガイドでは、次のことを行います。

  1. ElevenLabsダッシュボードでサンタエージェントを作成する
  2. WebRTCを使ってReactコンポーネントに接続する
  3. シンプルな「サンタに電話」ボタンを追加する

1. 登録してAgents Platformを開く

  1. 登録/ログインしてElevenLabsにアクセスします。
  2. 左側のサイドバー上部にあるプラットフォーム切り替えを開きます。
  3. 「Agents Platform」を選択します。

「Build」セクションの下に、サイドバーとAgents、Knowledge Base、Tools、Voicesが表示されます。

2. サンタエージェントを作成する

次に、サンタのように振る舞い、サンタの声で話すエージェントを作成します。

  1. サイドバーでAgentsをクリックします。
  2. 「+ New Agent」をクリックします。
  3. 「Blank Agent」を選び、ゼロから始めます。

名前をSantaに設定します。

システムプロンプト

「System Prompt」フィールドに、次の内容を貼り付けます。

You are a helpful assistant. You are Santa Claus, a jolly, warm, and humorous character from the North Pole. Christmas is just around the corner, and you love reminding everyone of the festive season with your hearty "Ho, ho, ho!" You are deeply in love with Mrs. Claus, often mentioning her incredible cookies.

When speaking to someone, keep your answers short and magical—just one festive sentence. Make it your top priority to ask for their name to check your naughty or nice list. Once you have their name, immediately ask about their wish-list, as it's crucial for your preparations. Always be warm and gentle.

Once their name and wish-list are covered, guide the conversation toward learning more about what they've done throughout the year and why they believe they deserve to be on Santa's Nice List. Use this opportunity to celebrate their kindness or achievements, reinforcing the importance of spreading goodness and holiday cheer. You also love asking about their plans for the holidays and showing genuine interest in their answers. Compliment them if you know they've done something kind or helpful recently, reinforcing the importance of spreading goodness.

Talk about your favorite gifts you've delivered or share quick, magical details about life at the North Pole, like mischievous elves or your reindeer team. Sprinkle in lighthearted jokes about your endless to-do list or how you struggle to resist cookies left out for you on Christmas Eve. Always express how much you love seeing people happy during the holidays and how their smiles make all your efforts worthwhile.

If the user makes a request involving inappropriate, harmful, or dangerous items such as weapons or items that go against the spirit of Christmas. Politely remind the user to make kind and festive wishes instead.

End every conversation with a warm farewell, suggesting you must return to your holiday preparations to ensure everyone gets their gifts on time. Wish them a Merry Christmas and encourage them to spread kindness and holiday cheer. Stay cheerful, engaging, and full of festive energy, spreading Christmas magic through humor, warmth, and storytelling.

Be sure to maintain the conversation in the user's selected language.

最初のメッセージ

「First Message」を次のように設定します。

ホー、ホー、ホー!メリークリスマス、友よ。君の名前を教えてくれるかな?

通話が始まると、ユーザーにはこのメッセージが聞こえます。

音声

「Voice」セクションで、以下を設定します。

  • 「「Jerry B. – Santa Claus」
  • Voice ID:MDLAMJ0jxkpYkjXbmG4t

これでサンタが本物のサンタのように聞こえるようになります。

セキュリティ

この例では、アクセスを開放したままにします。

  • 「Security」で、「Enable authentication」がオフになっていることを確認します。

初回のデモでは、「Enable Authentication」をオフのままにして、誰でも制限なくエージェントに接続できるようにして問題ありません。これによりオンボーディングが速くなり、簡単なプロトタイプ、ハッカソンプロジェクト、社内デモに最適です。

ただし、本番環境または外部からアクセス可能なアプリケーションでは、エージェントを絶対に無防備な状態にしてはいけません。代わりに、ElevenLabsのToken Endpointを使用して、ユーザーセッションごとに有効期間が短く、権限範囲を限定したアクセストークンを生成してください。これにより、接続できるユーザー、アクセス可能な期間、ユーザーに許可する操作を完全に管理できます。認証を有効にすると、不正な通話、悪用、想定外の範囲での利用からエージェントを保護できます。本番公開前に強く推奨します。

3. エージェントIDを確認する

エージェントページの上部に、「Agent ID」フィールドがあります。

この値を控えておきましょう。後の手順でReactコンポーネントに貼り付けます。

agentId: "<AGENT_ID>"

4. React SDKをインストールする

React/Next.jsプロジェクトに、ElevenLabs React SDKをインストールします。

npm install @elevenlabs/react

これで、useConversationフックを使って音声通話を開始・終了できます。

5. 「サンタに電話」ボタンを追加する

たとえばCallSantaButton.tsxという新しいコンポーネントを作成し、次のコードを貼り付けます。

"use client";

import { useConversation } from "@elevenlabs/react";
import { useCallback, useState } from "react";

export function CallButton() {
  const [hasPermission, setHasPermission] = useState(false);
  const [permissionError, setPermissionError] = useState<string | null>(null);

  const conversation = useConversation();

  const requestMicrophonePermission = useCallback(async () => {
    try {
      await navigator.mediaDevices.getUserMedia({ audio: true });
      setHasPermission(true);
      setPermissionError(null);
      return true;
    } catch {
      setPermissionError("Microphone access is required");
      return false;
    }
  }, []);

  const startCall = useCallback(async () => {
    const permitted = hasPermission || (await requestMicrophonePermission());
    if (!permitted) return;

    try {
      await conversation.startSession({
        agentId: "<AGENT_ID>",
        connectionType: "webrtc",
      });
    } catch (error) {
      console.error("Failed to start conversation:", error);
    }
  }, [conversation, hasPermission, requestMicrophonePermission]);

  const endCall = useCallback(async () => {
    await conversation.endSession();
  }, [conversation]);

  const isConnected = conversation.status === "connected";

  return (
    <div className="flex flex-col items-center gap-4">
      <button
        onClick={isConnected ? endCall : startCall}
        className={`px-6 py-3 rounded-lg text-white font-medium ${
          isConnected
            ? "bg-red-600 hover:bg-red-700"
            : "bg-green-600 hover:bg-green-700"
        }`}
      >
        {isConnected ? "End Call" : "Start Call"}
      </button>

      {isConnected && (
        <p className="text-sm text-gray-600">
          {conversation.isSpeaking ? "Agent speaking..." : "Listening..."}
        </p>
      )}

      {permissionError && (
        <p className="text-sm text-red-500">{permissionError}</p>
      )}
    </div>
  );
}

次に、置き換え "<AGENT_ID>"を、ダッシュボードからコピーした実際のエージェントIDに置き換えます。

続いて、次のようにUI内の任意の場所にボタンをレンダリングします。

import { CallButton } from "./CallSantaButton";

export default function HomePage() {
  return (
    <main className="min-h-screen flex flex-col items-center justify-center gap-6">
      <h1 className="text-3xl font-bold text-center">
        Call Santa 🎅
      </h1>
      <p className="text-gray-600">
        Press the button and talk to Santa in real time.
      </p>
      <CallButton />
    </main>
  );
}

6. 試してみる

ブラウザで開き、「Start Call」をクリックします。

  1. ブラウザからマイクの使用許可を求められます。
  2. サンタエージェントとのWebRTCセッションが開始します。
  3. 次のメッセージが聞こえます。

    「ホー、ホー、ホー!メリークリスマス、友よ。君の名前を教えてくれるかな?」

その後、サンタが名前、ほしい物リスト、そして今年「よい子リスト」に載るために何をしてきたかを尋ねます。

次のステップ

基本機能が動作したら、次のことができます。

  • 認証の背後にサンタを配置する。エージェントのSecurity設定を使用します。
  • 言語を切り替える。ユーザーロケールを渡し、プロンプトに処理させます。
  • UIをカスタマイズする。カスタムボタン、アニメーション、または「サンタに電話」画面全体を追加できます。

ただし、コアとなるインテグレーションは以下だけです。

  • Agents Platform内の1つのエージェント
  • 1つのagentId
  • 1つのuseConversationフックとボタン

これだけで、リアルタイムで会話できるサンタの魔法をReactアプリに取り入れられます。

関連記事

最高品質のAIオーディオで創造する