# チャットメッセージのパターン

チャットモデルは、辞書のリストとして会話を受け取ります。各辞書は`role`と`content`キーを使用します。`content`キーには、モデルに渡されるユーザーメッセージが含まれます。大規模言語モデルはテキストとツールを受け入れ、マルチモーダルモデルはテキストと画像、動画、オーディオを組み合わせます。

Transformersでは、各モダリティタイプが明示的に指定される統一フォーマットを使用しているため、単一のメッセージ内で入力を自由に組み合わせることができます。

このガイドでは、各モダリティのメッセージフォーマットパターン、ツール、バッチ推論、およびマルチターン会話について説明します。

## テキスト

Text は最も基本的なコンテンツタイプです。他のすべてのパターンの基盤となります。メッセージを文字列として `"content"` に渡します。

```py
message = [
    {
        "role": "user",
        "content": "Explain the French Bread Law."
    }
]
```

後で画像、動画、またはオーディオを追加する際にコードの一貫性を保つため、明示的な`"type": "text"`形式を使用することもできます。

```py
message = [
    {
        "role": "user",
        "content": [{"type": "text", "text": "Explain the French Bread Law."}]
    }
]
```

## ツール

[ツール](./chat_extras)とは、チャットモデルが呼び出すことのできる関数です。例えば、リアルタイムの天気データを取得するなど、モデル自身で生成するのではなく、外部から情報を取得するために使用されます。

`assistant`ロールがツールリクエストを処理します。`"tool_calls"`キーに`"type": "function"`を設定し、`"function"`キーにツールを指定します。アシスタントのツールリクエストをメッセージに追加してください。

```py
weather = {"name": "get_current_temperature", "arguments": {"location": "Paris, France", "unit": "celsius"}}
message.append(
    {
        "role": "assistant", 
        "tool_calls": [{"type": "function", "function": weather}]
    }
)
```

`tool`ロールは結果を処理します。`"content"`に追加してください。この値は常に文字列である必要があります。

```py
message.append({"role": "tool", "content": "22"})
```

## マルチモーダル

マルチモーダルモデルは、このフォーマットを拡張して、画像、動画、音声に対応します。各入力は`"type"`を指定し、`"url"`または`"path"`でメディアを提供します。

### 画像

`"type": "image"` を設定し、リンクには `"url"`、ローカルファイルには `"path"` を使用します。

```py
message = [
    {
        "role": "user",
        "content": [
            {"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
            {"type": "text", "text": "What pastry is shown in the image?"}
        ]
    }
]
```

### 動画

`"type": "video"` を設定し、リンクには `"url"`、ローカルファイルには `"path"` を使用します。

```py
message = [
    {
        "role": "user",
        "content": [
            {"type": "video", "url": "https://static01.nyt.com/images/2019/10/01/dining/01Sourdough-GIF-1/01Sourdough-GIF-1-superJumbo.gif"},
            {"type": "text", "text": "What is shown in this video?"}
        ]
    }
]
```

### オーディオ

`"type": "audio"` を設定し、リンクには `"url"`、ローカルファイルには `"path"` を使用します。

```py
message = [
    {
        "role": "user",
        "content": [
            {"type": "audio", "url": "https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac"},
            {"type": "text", "text": "Transcribe the speech."}
        ]
    }
]
```

### 混合複数

`content` リストは、任意の型の組み合わせを受け入れます。モデルはすべての入力をまとめて処理するため、比較やクロスモーダルな推論を可能にします。

```py
message = [
    {
        "role": "user",
        "content": [
            {"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
            {"type": "video", "url": "https://static01.nyt.com/images/2019/10/01/dining/01Sourdough-GIF-1/01Sourdough-GIF-1-superJumbo.gif"},
            {"type": "text", "text": "What does the image and video share in common?"},
        ],
    },
    {
        "role": "user",
        "content": [
            {"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
            {"type": "image", "url": "https://assets.bonappetit.com/photos/57e191f49f19b4610e6b7693/master/w_1600%2Cc_limit/undefined"},
            {"type": "text", "text": "What type of pastries are these?"},
        ],
    }
]
```

## バッチ処理

バッチ推論では、複数の会話を単一のフォワードパスで処理することでスループットと効率を向上させます。各会話を個別のリストでラップし、リストのリストとしてまとめて渡します。

```py
messages = [
    [
        {"role": "user",
        "content": [
                {"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
                {"type": "text", "text": "What type of pastry is this?"}
            ]
        },
    ],
    [
        {"role": "user",
        "content": [
                {"type": "image", "url": "https://assets.bonappetit.com/photos/57e191f49f19b4610e6b7693/master/w_1600%2Cc_limit/undefined"},
                {"type": "text", "text": "What type of pastry is this?"}
            ]
        },
    ],
]
```

## マルチターン

会話には複数のやり取りが含まれ、`"user"`と`"assistant"`の役割が交互に現れます。各ターンでリストに新しいメッセージが追加され、モデルは会話の完全な履歴にアクセスできます。このコンテキストにより、モデルはより適切な応答を生成できるようになります。

```py
message = [
    {
        "role": "user",
        "content": [
            {"type": "image", "url": "https://assets.bonappetit.com/photos/57ad4ebc53e63daf11a4ddc7/master/w_1280,c_limit/kouign-amann.jpg"},
            {"type": "text", "text": "What pastry is shown in the image?"}
        ]
    },
    {
        "role": "assistant",
        "content": [{"type": "text", "text": "This is kouign amann, a laminated dough pastry (i.e., dough folded with layers of butter) that also incorporates sugar between layers so that during baking the sugar caramelizes."}]
    },
    {
        "role": "user",
        "content": [
            {"type": "image", "url": "https://static01.nyt.com/images/2023/07/21/multimedia/21baguettesrex-hbkc/21baguettesrex-hbkc-videoSixteenByNineJumbo1600.jpg"},
            {"type": "text", "text": "Compare it to this image now."}
        ]
    }
]
```

