

# 双方向 API を使用した出力イベントの処理
<a name="output-events"></a>

**注記**  
このドキュメントは Amazon Nova バージョン 1 を対象としています。Amazon Nova 2 Sonic ガイドについては、「[双方向 API を使用した出力イベントの処理](https://docs.aws.amazon.com/nova/latest/nova2-userguide/sonic-output-events.html)」を参照してください。

Amazon Nova Sonic モデルが応答する際は、構造化されたイベントシーケンスに従います。フローは、`sessionId`、`promptName`、`completionId` などの一意の識別子を含む `completionStart` イベントで始まります。これらの識別子はレスポンスサイクル全体で一貫性があり、後続のすべてのレスポンスイベントを統一します。

各レスポンスタイプは一貫した 3 つの部分からなるパターンに従います。`contentStart` は、コンテンツタイプと形式、実際のコンテンツイベントを定義し、`contentEnd` はそのセグメントを閉じます。レスポンスには、通常、自動音声認識 (ASR) 文字起こし (ユーザーの発言)、オプションのツールの使用 (外部情報が必要な場合)、テキストレスポンス (モデルの発言予定内容)、音声レスポンス (音声出力) の複数のコンテンツブロックが順番に含まれます。

ASR 文字起こしが最初に表示され、`contentStart` で `role: "USER"` および `"additionalModelFields": "{\"generationStage\":\"FINAL\"}"` を使用してモデルのユーザーの音声を理解できます。モデルに外部データが必要な場合、特定のツール名とパラメータを含むツール関連のイベントが送信されます。テキストレスポンスは、`role: "ASSISTANT"` と `"additionalModelFields": "{\"generationStage\":\"SPECULATIVE\"}"` で予定された音声のプレビューを提供します。その後、音声レスポンスは、ストリーム全体で同じ `contentId` を共有する、base64 でエンコードされた音声チャンクを配信します。

オーディオ生成中、Amazon Nova Sonic は割り込み機能を通じて自然な会話フローをサポートします。Amazon Nova Sonic が話している間にユーザーがそれを中断すると、Nova Sonic は直ちに音声の生成を停止し、リッスンモードに切り替え、中断が発生したことを示すコンテンツ通知を送信します。Nova Sonic はリアルタイムよりも速く動作するため、一部のオーディオは配信済みでも再生されていない場合があります。中断通知により、クライアントアプリケーションはオーディオキューをクリアし、すぐに再生を停止できるため、応答性の高い会話エクスペリエンスが得られます。

音声生成が完了すると (または割り込みによって中断されると)、Amazon Nova Sonic は、実際に話された内容の文レベルの文字起こしを含む追加のテキストレスポンスを提供します。このテキストレスポンスには、`role: "ASSISTANT"` および `"additionalModelFields": "{\"generationStage\":\"FINAL\"}"` を含む `contentStart` イベントが含まれます。

レスポンス処理全体を通して、トークン消費を追跡するために `usageEvent` イベントが送信されます。これらのイベントには、入力トークンと出力トークン (音声とテキストの両方) の詳細なメトリクス、およびその累積合計が含まれます。各 `usageEvent` は、会話フローの他のイベントと同じ `sessionId`、`promptName`、および `completionId` を維持します。詳細セクションには、増分変更 (デルタ) とトークン使用率の累計の両方が表示され、会話中の使用率を正確にモニタリングできます。

モデルは、元の識別子と、会話が終了した仕方を示す `stopReason` を含む `completionEnd` イベントを送信します。このイベント階層により、アプリケーションはレスポンスのどの部分が一緒に属しているかを追跡し、それに応じて処理できるため、複数のターンにわたって会話コンテキストを維持できます。

出力イベントフローは、レスポンス生成フェーズに入ることから開始されます。自動音声認識で始まり、使用するツールを選択し、音声を書き起こし、音声を生成し、書き起こしを確定して、セッションを終了します。

![Amazon Nova Sonic 出力イベントフローを説明する図。](http://docs.aws.amazon.com/ja_jp/nova/latest/userguide/images/output-events.png)


## 出力イベントフロー
<a name="output-event-flow"></a>

出力イベントフローの構造については、このセクションで説明します。

1. `UsageEvent`

   ```
   "event": {
       "usageEvent": {
           "completionId": "string", // unique identifier for completion
           "details": {
               "delta": { // incremental changes since last event
                   "input": {
                       "speechTokens": number, // input speech tokens
                       "textTokens": number // input text tokens
                   },
                   "output": {
                       "speechTokens": number, // speech tokens generated
                       "textTokens": number // text tokens generated
                   }
               },
               "total": { // cumulative counts
                   "input": {
                       "speechTokens": number, // total speech tokens processed
                       "textTokens": number // total text tokens processed
                   },
                   "output": {
                       "speechTokens": number, // total speech tokens generated
                       "textTokens": number // total text tokens generated
                   }
               }
           },
           "promptName": "string", // same unique identifier from promptStart event
           "sessionId": "string", // unique identifier
           "totalInputTokens": number, // cumulative input tokens
           "totalOutputTokens": number, // cumulative output tokens
           "totalTokens": number // total tokens in the session
       }
   }
   ```

1. `CompleteStartEvent`

   ```
   "event": {
           "completionStart": {
               "sessionId": "string", // unique identifier
               "promptName": "string", // same unique identifier from promptStart event
               "completionId": "string", // unique identifier
           }
       }
   ```

1. `TextOutputContent`
   + `ContentStart`

     ```
     "event": {
             "contentStart": {
                 "additionalModelFields": "{\"generationStage\":\"FINAL\"}" | "{\"generationStage\":\"SPECULATIVE\"}",
                 "sessionId": "string", // unique identifier
                 "promptName": "string", // same unique identifier from promptStart event
                 "completionId": "string", // unique identifier
                 "contentId": "string", // unique identifier for the content block
                 "type": "TEXT",
                 "role": "USER" | "ASSISTANT",
                 "textOutputConfiguration": {
                     "mediaType": "text/plain"
                 }
             }
         }
     ```
   + `TextOutput`

     ```
     "event": {
             "textOutput": {
                 "sessionId": "string", // unique identifier
                 "promptName": "string", // same unique identifier from promptStart event
                 "completionId": "string", // unique identifier
                 "contentId": "string", // same unique identifier from its contentStart
                 "content": "string" // User transcribe or Text Response
             }
         }
     ```
   + `ContentEnd`

     ```
     "event": {
         "contentEnd": {
                 "sessionId": "string", // unique identifier
                 "promptName": "string", // same unique identifier from promptStart event
                 "completionId": "string", // unique identifier
                 "contentId": "string", // same unique identifier from its contentStart
                 "stopReason": "PARTIAL_TURN" | "END_TURN" | "INTERRUPTED",
                 "type": "TEXT"
         }
       }
     ```

1. `ToolUse`

   1. `ContentStart`

      ```
      "event": {
          "contentStart": {
            "sessionId": "string", // unique identifier
            "promptName": "string", // same unique identifier from promptStart event
            "completionId": "string", // unique identifier
            "contentId": "string", // unique identifier for the content block
            "type": "TOOL",
            "role": "TOOL",
            "toolUseOutputConfiguration": {
              "mediaType": "application/json"
            }
          }
        }
      ```

   1. `ToolUse`

      ```
      "event": {
          "toolUse": {
            "sessionId": "string", // unique identifier
            "promptName": "string", // same unique identifier from promptStart event
            "completionId": "string", // unique identifier
            "contentId": "string", // same unique identifier from its contentStart
            "content": "json",
            "toolName": "string",
            "toolUseId": "string"
          }
        }
      ```

   1. `ContentEnd`

      ```
      "event": {
          "contentEnd": {
            "sessionId": "string", // unique identifier
            "promptName": "string", // same unique identifier from promptStart event
            "completionId": "string", // unique identifier
            "contentId": "string", // same unique identifier from its contentStart
            "stopReason": "TOOL_USE",
            "type": "TOOL"
          }
        }
      ```

1. `AudioOutputContent`

   1. `ContentStart`

      ```
      "event": {
          "contentStart": {
            "sessionId": "string", // unique identifier
            "promptName": "string", // same unique identifier from promptStart event
            "completionId": "string", // unique identifier
            "contentId": "string", // unique identifier for the content block
            "type": "AUDIO",
            "role": "ASSISTANT",
            "audioOutputConfiguration": {
                  "mediaType": "audio/lpcm",
                  "sampleRateHertz": 8000 | 16000 | 24000,
                  "sampleSizeBits": 16,
                  "encoding": "base64",
                  "channelCount": 1
                  }
            }
        }
      ```

   1. `AudioOutput`

      ```
      "event": {
              "audioOutput": {
                  "sessionId": "string", // unique identifier
                  "promptName": "string", // same unique identifier from promptStart event
                  "completionId": "string", // unique identifier
                  "contentId": "string", // same unique identifier from its contentStart
                  "content": "base64EncodedAudioData", // Audio
              }
          }
      ```

   1. `ContentEnd`

      ```
      "event": {
          "contentEnd": {
            "sessionId": "string", // unique identifier
            "promptName": "string", // same unique identifier from promptStart event
            "completionId": "string", // unique identifier
            "contentId": "string", // same unique identifier from its contentStart
            "stopReason": "PARTIAL_TURN" | "END_TURN",
            "type": "AUDIO"
          }
        }
      ```

1. `CompletionEndEvent`

   ```
   "event": {
       "completionEnd": {
         "sessionId": "string", // unique identifier
         "promptName": "string", // same unique identifier from promptStart event
         "completionId": "string", // unique identifier
         "stopReason": "END_TURN" 
       }
     }
   ```