# أنماط رسائل الدردشة

تتوقع نماذج المحادثة المحادثات على شكل قائمة من القواميس. يستخدم كل قاموس مفاتيح `role` و`content`. يحتوي مفتاح `content` على رسالة المستخدم المُمررة إلى النموذج. تقبل النماذج اللغوية الكبيرة النص والأدوات، بينما تجمع النماذج متعددة الوسائط بين النص والصور والفيديوهات والصوت.

يستخدم Transformers تنسيقًا موحّدًا حيث يُحدَّد كل نوع من أنواع الوسائط صراحةً، مما يجعل من السهل دمج المدخلات المختلفة وتبديلها في رسالة واحدة.

يتناول هذا الدليل أنماط تنسيق الرسائل لكل نمط، والأدوات، والاستدلال الدُفعي، والمحادث متعددة الأدوار.

## نص

النص هو نوع المحتوى الأساسي ביותר. وهو الأساس لجميع الأنماط الأخرى. مرّر رسالتك إلى `"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` معالجة طلب الأداة. اضبط `"type": "function"` في المفتاح `"tool_calls"` وقدم أداتك إلى المفتاح `"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?"},
        ],
    }
]
```

## المجمّعة

يعالج الاستدلال المجمّع (Batched inference) محادثات متعددة في تمريرة أمامية واحدة لتحسين معدل الإنتاجية والكفاءة. قم بتغليف كل محادثة في قائمتها الخاصة، ثم مررها معًا كقائمة من القوائم.

```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."}
        ]
    }
]
```

