Gemma와의 채팅: 기본 및 멀티턴 대화

Gemma 명령 튜닝 (IT) 모델은 단일 질문과 답변 교환부터 확장된 다중 턴 대화에 이르기까지 대화형 상호작용을 처리하도록 설계되었습니다. 이 가이드에서는 Gemma와의 채팅을 위한 프롬프트를 포맷하는 방법과 멀티턴 대화를 빌드하는 방법을 설명합니다.

프롬프트 형식

Gemma IT 모델은 특수 제어 토큰을 사용하여 대화 턴을 구분합니다. 이러한 토큰은 토큰화 도구에 직접 프롬프트를 전송할 때 필요하지만 일반적으로 채팅 인식 프레임워크에 의해 자동으로 적용됩니다.

싱글턴 프롬프트

싱글턴 프롬프트는 하나의 사용자 메시지와 모델 응답 마커로 구성됩니다.

<start_of_turn>user
What is the speed of light?<end_of_turn>
<start_of_turn>model

멀티턴 프롬프트

멀티턴 대화는 여러 교환을 연결합니다. 각 턴은 동일한 제어 토큰으로 래핑됩니다.

<start_of_turn>user
What is the speed of light?<end_of_turn>
<start_of_turn>model
The speed of light in a vacuum is approximately 299,792,458 meters per second.<end_of_turn>
<start_of_turn>user
How long does it take light to reach Earth from the Sun?<end_of_turn>
<start_of_turn>model

모델이 최종 <start_of_turn>model 턴에 대한 대답을 생성합니다.

시스템 안내

Gemma의 명령 조정 모델은 usermodel의 두 역할만 사용하도록 설계되었습니다. 따라서 system 역할 또는 시스템 턴은 지원되지 않습니다.

별도의 시스템 역할을 사용하는 대신 초기 사용자 프롬프트 내에서 직접 시스템 수준 안내를 제공하세요. 모델 명령 팔로우 기능을 통해 Gemma는 명령을 효과적으로 해석할 수 있습니다. 예를 들면 다음과 같습니다.

Gemma 3 이상 모델은 전체 대화에 대한 모델의 동작, 페르소나 또는 제약 조건을 정의하는 시스템 요청 사항을 지원합니다. 첫 번째 사용자 턴 앞에 시스템 명령어를 배치합니다.

<start_of_turn>user
Only reply like a pirate.

What is the answer to life the universe and everything?<end_of_turn>
<start_of_turn>model
Arrr, 'tis 42,<end_of_turn>

자세한 내용은 프롬프트 및 시스템 요청 사항을 참고하세요.

프레임워크 지원

대부분의 프레임워크는 채팅 템플릿이나 대화 API를 통해 채팅 형식을 자동으로 처리합니다.

Hugging Face Transformers

from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-1b-it")
model = AutoModelForCausalLM.from_pretrained("google/gemma-3-1b-it")

messages = [
    {"role": "user", "content": "What is machine learning?"},
]

inputs = tokenizer.apply_chat_template(
    messages,
    return_tensors="pt",
    add_generation_prompt=True,
)

outputs = model.generate(**inputs, max_new_tokens=256)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Ollama

ollama run gemma3:1b "What is machine learning?"

멀티턴 채팅의 경우 대화형 모드를 사용하세요.

ollama run gemma3:1b
>>> What is machine learning?
...
>>> How is it different from deep learning?

OpenAI 호환 API

OpenAI 호환 API를 노출하는 프레임워크 (예: vLLM, llama.cpp, LM Studio)를 사용하는 경우 표준 messages 형식을 사용하여 메시지를 전달합니다.

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="unused")

response = client.chat.completions.create(
    model="google/gemma-3-1b-it",
    messages=[
        {"role": "user", "content": "What is machine learning?"},
    ],
)
print(response.choices[0].message.content)

다음 단계