Transformers documentation
채팅 메시지 패턴
채팅 메시지 패턴
Chat 모델은 대화 목록을 딕셔너리 리스트로 기대합니다. 각 딕셔너리는 role 및 content 키를 사용합니다. content 키에는 모델에 전달되는 사용자 메시지가 포함됩니다. 대규모 언어 모델은 텍스트와 도구를, 멀티모달 모델은 텍스트와 이미지, 비디오, 오디오를 함께 처리합니다.
Transformers는 각 모달리티 유형을 명시적으로 지정하는 통합 형식을 사용하므로, 단일 메시지에서 입력을 자유롭게 조합할 수 있습니다.
이 가이드에서는 각 모달리티의 메시지 포맷 패턴, 도구, 배치 추론, 다중 턴 대화를 다룹니다.
텍스트
Text는 가장 기본적인 콘텐츠 유형입니다. 모든 다른 패턴의 기반이 됩니다. 메시지를 문자열로 "content"에 전달하세요.
message = [
{
"role": "user",
"content": "Explain the French Bread Law."
}
]나중에 이미지, 비디오 또는 오디오를 추가할 때 코드의 일관성을 유지하려면 명시적인 "type": "text" 형식을 사용할 수도 있습니다.
message = [
{
"role": "user",
"content": [{"type": "text", "text": "Explain the French Bread Law."}]
}
]도구
Tools는 챗 모델이 자체적으로 생성하는 대신 실시간 날씨 데이터와 같은 정보를 가져오기 위해 호출할 수 있는 함수입니다.
assistant 역할은 도구 요청을 처리합니다. "tool_calls" 키에 "type": "function"을 설정하고 "function" 키에 도구를 제공합니다. 어시스턴트의 도구 요청을 메시지에 추가하세요.
weather = {"name": "get_current_temperature", "arguments": {"location": "Paris, France", "unit": "celsius"}}
message.append(
{
"role": "assistant",
"tool_calls": [{"type": "function", "function": weather}]
}
)tool 역할은 결과를 처리합니다. "content"에 추가하세요. 이 값은 항상 문자열이어야 합니다.
message.append({"role": "tool", "content": "22"})멀티모달
멀티모달 모델은 이 형식을 확장하여 이미지, 비디오, 오디오를 처리합니다. 각 입력은 "type"을 지정하고 "url" 또는 "path"로 미디어를 제공합니다.
이미지
"type": "image"를 설정하고 링크에는 "url", 로컬 파일에는 "path"를 사용합니다.
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?"}
]
}
]Video
"type": "video"를 설정하고 링크에는 "url", 로컬 파일에는 "path"를 사용합니다.
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"를 사용하세요.
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 리스트는 모든 유형의 조합을 허용합니다. 모델은 모든 입력을 함께 처리하여 비교 또는 크로스 모달 추론을 수행할 수 있습니다.
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는 단일 forward pass에서 여러 대화를 처리하여 처리량과 효율성을 높입니다. 각 대화를 별도의 리스트로 감싼 후, 리스트의 리스트 형태로 함께 전달합니다.
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" 역할이 번갈아 나타나는 여러 번의 교환이 포함됩니다. 각 턴은 목록에 새로운 메시지를 추가하여 모델이 전체 대화 이력에 접근할 수 있게 합니다. 이러한 컨텍스트는 모델이 더 적절한 응답을 생성하는 데 도움이 됩니다.
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."}
]
}
]