[Llama] Special Token 정리

kimjayhyun·2025년 6월 28일
0

🦙 Llama 3.1 의 Special Token 완전 정리

이 포스팅은 아래 문서를 참고하여 정리하고 번역한 글입니다.

Llama 3.1 공식문서


🔄 지난 내용 복습

이전 포스팅에서 우리는 다음을 배웠습니다:

  • LLM이 메시지를 하나의 긴 프롬프트 문자열로 변환하는 방식
  • Chat Template이 메시지 역할(system, user, assistant)을 구분하는 방법
  • 모델마다 다른 Special Token을 사용한다는 점

이번 포스팅에서는 Llama 모델 계열에서 사용하는 Special Token들을 하나씩 뜯어보겠습니다.


🎭 Llama가 지원하는 4가지 역할

Llama 모델에서는 4가지 핵심 역할을 지원합니다.

📋 system

AI 모델과 상호작용할 맥락을 설정합니다.

일반적으로 규칙, 가이드라인, 또는 모델이 효과적으로 응답하는 데 필요한 정보를 포함합니다.

👤 user

모델과 상호작용하는 인간을 나타냅니다.

LLM에 대한 입력, 명령, 질문을 포함합니다.

🤖 assistant

system, ipython, user 프롬프트에서 제공된 컨텍스트를 바탕으로 AI 모델이 생성한 응답을 나타냅니다.

🔧 ipython

Llama 3.1에서 새롭게 도입된 역할입니다.

의미적으로 이 역할은 "도구(tool)"를 의미합니다.

도구 실행 결과를 모델로 전송될 때 결과 메시지를 표시하는 데 사용됩니다.


📝 기본 대화 분석

role을 참고하여 기본적인 대화의 구성을 분석해보겠습니다.

복잡해 보이지만, 각 토큰의 역할을 이해하면 매우 체계적인 구조임을 알 수 있습니다.

📋 기본적인 대화 예시

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>

Who are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>

I'm an AI assistant, which means I'm a computer program designed to simulate conversation and answer questions to the best of my ability.<|eot_id|>

🔑 Speical Token 분석하기

1️⃣ <|begin_of_text|>

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

전체 프롬프트(입력)의 시작을 알리는 토큰

  • 모든 Llama 대화는 이 토큰으로 시작됩니다
  • 이제부터 대화가 시작됩니다라는 의미

2️⃣ <|start_header_id|> & <|end_header_id|>

Role을 명시하는 토큰

<|start_header_id|>system<|end_header_id|>
<|start_header_id|>user<|end_header_id|>  
<|start_header_id|>assistant<|end_header_id|>
<|start_header_id|>ipython<|end_header_id|>

3️⃣ <|eot_id|>

I'm an AI assistant designed to help you with various tasks.<|eot_id|>

현재 턴(turn)의 완전한 종료를 의미

  • "이 응답은 완료되었습니다"라는 신호
  • 사용자-AI 직접 대화 종료 시
  • 모델-도구 상호작용이 모두 끝난 후

🐍 Code Interpreter 대화 분석

이번에는 앞서 설명한 ipython 역할을 활용한

Code Interpreter 대화가 어떻게 구성되는지 분석해보겠습니다.

📋 Code Interpreter 활성화 조건

Code Interpreter 기능은 시스템 프롬프트에 Environment: ipython이 포함되면 자동으로 활성화됩니다.

🔄 Code Interpreter 대화 예시

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

Environment: ipython<|eot_id|><|start_header_id|>user<|end_header_id|>

Write code to check if number is prime, use that to see if the number 7 is prime<|eot_id|><|start_header_id|>assistant<|end_header_id|>

<|python_tag|>
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

print(is_prime(7))  # Output: True
<|eom_id|><|start_header_id|>ipython<|end_header_id|>

True<|eot_id|><|start_header_id|>assistant<|end_header_id|>

The number 7 is indeed prime! The function returned True, confirming that 7 has no divisors other than 1 and itself.<|eot_id|>

🔍 단계별 분석

1️⃣ 시스템 설정

<|start_header_id|>system<|end_header_id|>
Environment: ipython<|eot_id|>
  • Environment: ipython으로 Code Interpreter 모드 활성화

2️⃣ 사용자 요청

<|start_header_id|>user<|end_header_id|>
Write code to check if number is prime, use that to see if the number 7 is prime<|eot_id|>
  • 일반적인 사용자 메시지와 동일한 형태

3️⃣ 모델의 코드 생성

<|start_header_id|>assistant<|end_header_id|>
<|python_tag|>
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

print(is_prime(7))  # Output: True
<|eom_id|>

일반적인 대화와 다른 Speical Token을 사용한다.

  • <|python_tag|>: 실행 가능한 Python 코드 블록 시작
  • <|eom_id|>: "이 코드를 실행해주세요"라는 신호

4️⃣ 실행 결과 반환

<|start_header_id|>ipython<|end_header_id|>
True<|eot_id|>
  • 역할을 ipython로 설정하여 코드의 실행 결과를 보냄
  • 즉, 외부 실행 환경에서 모델로 결과를 다시 보냄

5️⃣ 최종 응답

<|start_header_id|>assistant<|end_header_id|>
The number 7 is indeed prime! The function returned True...<|eot_id|>
  • 실행 결과를 바탕으로 사용자에게 최종 설명 제공

🔑 Code interpreter의 Speical Token

1️⃣ <|python_tag|>

  • 역할: Python 코드 블록의 시작을 표시
  • 의미: "이 코드를 실행해주세요"

2️⃣ <|eom_id|> vs <|eot_id|>

  • <|eom_id|>: 메시지 끝, 하지만 도구 실행 대기 중
  • <|eot_id|>: 턴 완전 종료, 다음 상대방 차례

💡 중요 사항

중요한 점은 모델 자체는 코드를 실행하지 않는다는 것입니다

  1. 모델: <|python_tag|> 코드 생성 → <|eom_id|>
  2. 실행 환경: llama-stack-apps 등의 코드 실행 도구
  3. 실행 환경: 결과를 ipython 역할로 모델에 재전달
  4. 모델: 결과 해석 후 최종 응답 → <|eot_id|>

이러한 구조로 AI와 도구 간의 협업이 가능해집니다!


🔜 다음 글

이번 글에서는 Llama의 주요 Speical Token에 대해서 정리했습니다.
다음 글에는 MCP와 밀접한 연관이 있는 Tool Calling에 대해서 알아보겠습니다.

0개의 댓글