채팅 메시지 패턴
채팅 메시지 패턴 (Chat message patterns)
채팅 모델은 대화를 딕셔너리 리스트로 받아요. 각 딕셔너리는 role과 content 키를 사용해요. content 키에는 모델에 전달되는 사용자 메시지가 들어가지요. 대규모 언어 모델(Large Language Model)은 텍스트와 도구(tools)를 받고, 멀티모달 모델은 텍스트에 이미지, 비디오, 오디오를 결합해요.
출처: 문서
본문
Transformers는 각 양식(modality) 타입을 명시적으로 지정하는 통일된 형식을 사용해서, 하나의 메시지 안에서 입력을 자유롭게 섞고 맞추기 쉬워요.
이 가이드에서는 각 양식의 메시지 형식 패턴, 도구, 배치 추론(batch inference), 멀티턴 대화를 다룰게요.
텍스트 (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" 키에 여러분의 도구를 제공해요. assistant의 도구 요청을 메시지에 추가해요.
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"})
멀티모달 (Multimodal)
멀티모달 모델은 이 형식을 확장해서 이미지, 비디오, 오디오를 처리해요. 각 입력은 자신의 "type"을 지정하고 "url" 또는 "path"로 미디어를 제공해요.
이미지 (Image)
"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?"}
]
}
]
오디오 (Audio)
"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."}
]
}
]
혼합 다중 (Mixed multiple)
content 리스트는 어떤 타입의 조합이든 받아들여요. 모델이 모든 입력을 함께 처리하므로 비교나 cross-modal 추론이 가능해져요.
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)
배치 추론은 여러 대화를 단일 forward 패스로 처리해서 처리량과 효율을 높여요. 각 대화를 자신만의 리스트로 감싼 다음, 리스트의 리스트로 함께 전달해요.
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?"}
]
},
],
]
멀티턴 (Multi-turn)
대화는 여러 차례의 교환에 걸쳐 이어지고, "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."}
]
}
]