토크나이제이션
토크나이제이션 (Tokenization) — Tool Calling (도구 호출)
Mistral 토크나이저가 tool calling(도구 호출)을 어떻게 인코딩하는지, V2/V3/V3-Tekken 버전별로 살펴보는 페이지예요.
출처: 문서
본문
Tool Calling (도구 호출)
Tool calling은 모델이 외부 도구나 API와 상호작용할 수 있게 해주는 기능이에요. 모델에게 고를 수 있는 도구 목록이 주어지고, 도구의 결과가 다시 모델에게 제공돼요. 이는 정보 검색, 계산, 모델이 스스로 수행할 수 없는 기타 기능이 필요한 작업에 유용해요. tool calling 기능은 효율성, 보안, 그리고 적절한 경계 처리를 보장하기 위해 컨트롤 토큰을 사용해서 구현돼요.
토크나이제이션 — 복습 (Tokenization - Recap)
Tokenization은 문자열을 토큰 ID 시퀀스로 변환하는 과정이에요. 토큰은 문자, 서브워드, 또는 심볼이 될 수 있어요. 토크나이저는 문자열을 토큰으로 쪼개서 ID로 변환(encoding)하고, 그 반대(decoding)도 해요. 그러면 모델은 시퀀스에 기반해서 다음 토큰을 예측해요. 예를 들어 "Hello, how are"라는 문장은 Hello, -> 432, how -> 523, are -> 87처럼 토크나이즈될 수 있어요. 모델은 다음 토큰(예: 75, you로 디코딩)을 예측해서 일관된 텍스트를 생성할 수 있어요.
컨트롤 토큰 — 복습 (Control Tokens - Recap)
컨트롤 토큰은 효율성, 보안, 그리고 경계 문제를 해결해요. 이들은 모델이 이전에 본 적 없고 사용자가 절대 주입할 수 없는 새 토큰을 도입해요. 이 토큰들이 특수 문자열을 대체해서 인코딩 과정을 더 효율적이고 안전하게 만들어요. 예를 들어 특수 문자열로 사용자 지시를 감싸는 대신, 컨트롤 토큰 ID를 직접 사용해요.
전체 개념 (Overall Concept)
아이디어는 이래요:
- 사용자가 도구 집합을 제공하고 모델에게 요청을 해요.
- Assistant는 무엇을 할지 결정해요. 도구가 필요 없으면 답변해요. (모델이 도구를 고르도록 강제하는 것도 가능해요.) 도구가 필요하면:
- Assistant가 tool call로 응답하면서 사용하려는 도구와 인자를 언급해요.
- 사용자가 도구를 실행하고 결과를 제공해요.
- Assistant가 결과를 알고 응답해요.
Tokenizer V2
토크나이저 V2는 tool calling을 위한 컨트롤 토큰을 도입해요. 이 토큰들은 다음과 같아요:
BEGIN_AVAILABLE_TOOLS와END_AVAILABLE_TOOLS: 사용 가능한 도구의 시작과 끝을 나타내는 컨트롤 토큰.BEGIN_TOOL_RESULTS와END_TOOL_RESULTS: 도구 결과의 시작과 끝을 나타내는 컨트롤 토큰.TOOL_CALLS: 도구 호출을 위한 컨트롤 토큰.
시나리오: 사용자가 계산을 요청 (User Asks for a Calculation)
사용자가 외부 도구를 사용해서 계산을 모델에게 요청하는 시나리오를 생각해 볼게요.
User Message:
What's 2+2?
모델에게 도구 집합을 제공해요. 이 예시에서는 단일 도구를 제공할게요.
Tools:
[
{
"type": "function",
"function": {
"name": "calculator",
"description": "Performs mathematical calculations",
"parameters": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"description": "The operation to be done in python format."
}
},
"required": ["operation"]
}
}
}
]
String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST]
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
이제 모델이 도구를 갖게 됐으니, 도구를 사용하기로 결정해요.
Assistant Tool Call:
[{"name": "calculator", "arguments": {"operation": "2+2"}}]
Tool Call String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST][TOOL_CALLS] [{"name": "calculator", "arguments": {"operation": "2+2"}}]</s>
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
다음 단계는 제공된 정보로 도구를 실행하는 것이에요. 결과를 얻으면 모델에게 제공해요.
Tool Result:
[{"name": "calculator", "content": 4}]
Tool Result String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST][TOOL_CALLS] [{"name": "calculator", "arguments": {"operation": "2+2"}}]</s>[TOOL_RESULTS] [{"name": "calculator", "content": 4}][/TOOL_RESULTS]
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
완벽해요, 이제 모델이 답할 수 있어요!
Final Assistant Response:
2+2=4
Final String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST][TOOL_CALLS] [{"name": "calculator", "arguments": {"operation": "2+2"}}]</s>[TOOL_RESULTS] [{"name": "calculator", "content": 4}][/TOOL_RESULTS] 2+2=4</s>
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
이 시나리오에서 컨트롤 토큰 TOOL_CALLS, BEGIN_TOOL_RESULTS, END_TOOL_RESULTS가 tool call과 tool result를 인코딩하는 데 사용돼요. 그러면 모델이 도구 결과에 기반해서 일관된 응답을 생성할 수 있어요!
더 나은 시각화를 위해, 문자열을 여러 섹션으로 나눠 볼게요:
<s>
[AVAILABLE_TOOLS]
[{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}]
[/AVAILABLE_TOOLS]
[INST]
What's 2+2?
[/INST]
[TOOL_CALLS]
[{"name": "calculator", "arguments": {"operation": "2+2"}}]</s>
[TOOL_RESULTS]
[{"name": "calculator", "content": 4}]
[/TOOL_RESULTS]
2+2=4</s>
실제 버전에는 줄바꿈이 없어요.
Tokenizer V3
토크나이저 V3는 전체적으로 V2와 비슷하지만, tool 메시지를 인코딩하는 약간 다른 방식을 도입해요. 주요 차이점은:
- Tool Results 인코딩: V3에서는 tool results가 리스트로 감싸지 않고, tool calls의 기록(history)도 토크나이즈돼요.
- Tool Call 인코딩: V3에서는 tool calls에
id필드가 포함되어, tool calls의 기록을 추적하는 데 사용할 수 있어요.
시나리오: 사용자가 계산을 요청 (User Asks for a Calculation)
사용자가 외부 도구를 사용해서 계산을 모델에게 요청하는 같은 시나리오를 생각해 볼게요.
User Message:
What's 2+2?
모델에게 도구 집합을 제공해요. 이 예시에서는 단일 도구를 제공할게요.
Tools:
[
{
"type": "function",
"function": {
"name": "calculator",
"description": "Performs mathematical calculations",
"parameters": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"description": "The operation to be done in python format."
}
},
"required": ["operation"]
}
}
}
]
String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST]
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
이제 모델이 도구를 갖게 됐으니, 도구를 사용하기로 결정해요.
Assistant Tool Call:
[{"name": "calculator", "arguments": {"operation": "2+2"}, "id": "VvvODy9mT"}]
Tool Call String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST][TOOL_CALLS] [{"name": "calculator", "arguments": {"operation": "2+2"}, "id": "VvvODy9mT"}]</s>
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
다음 단계는 제공된 정보로 도구를 실행하는 것이에요. 결과를 얻으면 모델에게 제공해요.
Tool Result:
[{"name": "calculator", "content": 4}]
Tool Result String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST][TOOL_CALLS] [{"name": "calculator", "arguments": {"operation": "2+2"}, "id": "VvvODy9mT"}]</s>[TOOL_RESULTS] {"content": 4, "call_id": "VvvODy9mT"}[/TOOL_RESULTS]
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
이제 모델이 답할 수 있어요!
Final Assistant Response:
2+2=4
Final String Representation:
<s>[AVAILABLE_TOOLS] [{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST] What's 2+2?[/INST][TOOL_CALLS] [{"name": "calculator", "arguments": {"operation": "2+2"}, "id": "VvvODy9mT"}]</s>[TOOL_RESULTS] {"content": 4, "call_id": "VvvODy9mT"}[/TOOL_RESULTS] 2+2=4</s>
일부 컨트롤 토큰 뒤에 공백이 있음에 주목하세요.
문자열을 여러 섹션으로 나누면:
<s>
[AVAILABLE_TOOLS]
[{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}]
[/AVAILABLE_TOOLS]
[INST]
What's 2+2?
[/INST]
[TOOL_CALLS]
[{"name": "calculator", "arguments": {"operation": "2+2"}, "id": "VvvODy9mT"}]</s>
[TOOL_RESULTS]
{"content": 4, "call_id": "VvvODy9mT"}
[/TOOL_RESULTS]
2+2=4</s>
실제 버전에는 줄바꿈이 없어요.
Tokenizer V3 - Tekken
Tekken의 주요 차이는 또 다시 공백(whitespace)이에요. 전체적으로 동일한 토크나이제이션이고, 유일한 차이는 sentencepiece 기반이 아니라는 거예요. 앞선 시나리오의 최종 문자열 표현은 이렇게 보일 거예요:
<s>[AVAILABLE_TOOLS][{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}][/AVAILABLE_TOOLS][INST]What's 2+2?[/INST][TOOL_CALLS][{"name": "calculator", "arguments": {"operation": "2+2", "format": "celsius"}, "id": "VvvODy9mT"}]</s>[TOOL_RESULTS]{"content": 4, "call_id": "VvvODy9mT"}[/TOOL_RESULTS]2+2=4</s>
이번에는 공백이 없어요.
문자열을 여러 섹션으로 나누면:
<s>
[AVAILABLE_TOOLS]
[{"type": "function", "function": {"name": "calculator", "description": "Performs mathematical calculations", "parameters": {"type": "object", "properties": {"operation": {"type": "string", "description": "The operation to be done in python format."}}, "required": ["operation"]}}}]
[/AVAILABLE_TOOLS]
[INST]
What's 2+2?
[/INST]
[TOOL_CALLS]
[{"name": "calculator", "arguments": {"operation": "2+2", "format": "celsius"}, "id": "VvvODy9mT"}]</s>
[TOOL_RESULTS]
{"content": 4, "call_id": "VvvODy9mT"}
[/TOOL_RESULTS]
2+2=4</s>
실제 버전에는 줄바꿈이 없어요.
더 알아보기 (Learn more)
- 원문 (GitHub)
- 컨트롤 토큰: Control Tokens