# カスタムメタデータ：フロントエンドと会話AI間で情報をやり取りする

## カスタムメタデータを送信する

ホステッドエクスペリエンスの[customMetadata](https://gitlab.digitalhumans.jp/docs/docs-digitalhumansjp/-/blob/main/development/README.md#custommetadata)または`uneeqInstance.customPromptMetadata()`を使用すると、設定されたデータはユーザー発話時に対話AIに送信され、ログインユーザの識別や、応答を生成する際に使用することができます。

下記はDifyを例にして説明していますが、miiboでも同様に利用できます。

![customMetadata](/files/x9HKVF4Nvn4g8i8cEZ10)

customMetadata

![customPromptMetadata()](/files/GcBhsJirhXPfBLYrsSoG)

customPromptMetadata()

1. フロントエンド ホステッドエクスペリエンスコードスニペットの`uneeqOptions.customMetadata` にパラメータを設定する。 もしくは、`uneeqInstance.customPromptMetadata()`を使用してカスタムデータを動的に設定する。

![](/files/fDdqG09405d3lEyTwYBF)

![](/files/BNSiEQxltJYhFMaXdTmr)

2.接続するDify ChatBotのStartノード入力フィールドで、`customMetadata`や`customPromptMetadata()`に登録したパラメータと同名の入力フィールドを設定して`変数`として定義する。

miiboはStateに登録されるため、自動的に受け取ることができます。 ※ 必須の✓は運用にあわせてください。

![](/files/rFl10TjMrSjzNsVZfmx0)

![](/files/dw3ZmEFBug8s3j5HLPDl)

3.`変数`をプロンプトや処理に埋め込んで使用する。

以上でカスタムメタデータの受け渡しが完了しました。変数を使用することで、ユーザーのIDや属性などを使用した　動的な制御が可能になります。

## カスタムメタデータを更新する

会話AIからフロントエンドへカスタムメタデータを送信するには、`instructions.customMetadata` にキーと値のペア（例：`{'test':'true'}`）を指定します。これにより、`uneeqInstance.setCustomPromptMetadata` が自動的に動作し、カスタムメタデータが更新されます。

この方法でカスタムメタデータを登録した場合、既存のカスタムメタデータは消えず、新しいメタデータが追加される動きになります。もし、同じキーの値が既に存在する場合には、その値が上書きされます。

```json
{
    "instructions": {
        "customMetadata": {
            "test": "true"
        }
    }
}

```

### カスタムメタデータを送信直後の動作

![](/files/q50wddn5fcHaPQ5x5k8B)

`"test": "true"`を会話AIからフロントエンドへ送信して更新すると左図の様なログが出力され、カスタムメタデータが更新されます。ユーザーの発話・リクエストが行われると、`customMetadata` に `"test": "true"`が値で入って会話AIへ届けられます。

更新されたカスタムメタデータは、UneeQイベントの`CustomMetadataUpdate`イベントで取得できます。

## イベントの取得方法

イベントの取得方法については [イベント](/dev/hosted-experience/hosted-experience-events-2.md) をご覧下さい。また、本件には直接関係ありませんが、 [スピーチイベント：発話のタイミングにあわせてイベントを発生させる](https://gitlab.digitalhumans.jp/docs/docs-digitalhumansjp/-/blob/main/settings/experience/hosted-experience-speech-events.md) もあわせてご覧下さい。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.digitalhumans.jp/ops/experience/custom-metadata-frontend-to-backend.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
