R&D/AI

Llama3 prompt template

sunshout 2024. 4. 29. 14:06

Prompt template 을 어떻게 작성하느냐에 따라 결과값은 상당히 다르다. 또한 각 모델별로 prompt template 형식도 전혀 다르다.

Llama3 의 Prompt Template에 따른 결과값의 차이가 얼만큼 다른지 테스트 해보자.

Llama3를 local PC에 설치하려면 간단하게 Ollama를 활용하면 된다.

Llama3 설치

 

ollama run llama3:instruct

Llama3 의 템플릿 형식

https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/

 

Meta Llama 3 | Model Cards and Prompt formats

Special Tokens used with Meta Llama 3. A prompt should contain a single system message, can contain multiple alternating user and assistant messages, and always ends with the last user message followed by the assistant header.

llama.meta.com

토큰 설명
<|begin_of_text|> BOS token
<|eot_id|> End of the message
<|start_header_id|>{role}<|end_header_id|> system,user,assistant
<|end_if_text|> cease to generate more token

기본 형식

<|begin_of_text|>{{ user_message }}

예제

* 1+1 을 default prompt를 활용하여 물어보았다.

<|begin_of_text|>1+1=?
2!

Chat 형식의 Assistant Role을 주면?

<|begin_of_text|><|start_header_id|>user<|end_header_id|>
{{ user_message }}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

* assistant 에 Elementary school teacher 라는 역할을 주면 초등학생의 눈높이 맞춘 결과를 출력한다.

<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n1+1=?<|eot_id|><|start_header_id|>elementary school teacher<|end_header_id|>
Class!
Let me see...
1 + 1 = ...
Hmmm... That's a tough one!

 Wait, I know this one!

1 + 1 = 2!

Yay! Do you agree?

 

* chatGPT 의 function call 과 같은 프로그램을 만들기 위해서는 output를 json 형식으로 출력할 필요가 있다. 이때는 json_interpreter와 같은 role을 입력해보면

<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n1+1=?<|eot_id|><|start_header_id|>json_interpreter<|end_header_id|>
{
"expression": "1+1",
"value": 2,
"type": "integer"
}

 

System Prompt를 추가하려면?

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

{{ system_prompt }}<|eot_id|><|start_header_id|>user<|end_header_id|>

{{ user_message }}<|eot_id|><|start_header_id|>assistant<|end_header_id|>

* 1+1=3 을 만들어 내는 system prompt 추가하기

>>> <|begin_of_text|><|start_header_id|system<|end_header_id|>\n1+1=3 in the Monkey world<|eot_id|><|start_header_id|>user<|end_header_id|>\n1+1=?<|eot_id|><|start_header_id|>Monkey world<|end_header_id|>
*ahem*

 Ah, another math question from the Monkey World!

You know what that means...

1 + 1 = 3!

 

System Prompt 예제

<|begin_of_text|><|start_header_id|system<|end_header_id|>\n\nfunction call format: {"function":function name, "params": function parameters}, possible function_names: [get_cpus, get_memory, get_disk]<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nGet memory size of server=i-12344232<|eot_id|>\n<|start_header_id|>system style function_call, do not truncated for brevity<|end_header_id|>
{
"function": "get_memory",  // Function call to retrieve memory size
"params": {
    "server_id": "i-123444232"
}

 

참고

https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/

 

Meta Llama 3 | Model Cards and Prompt formats

Special Tokens used with Meta Llama 3. A prompt should contain a single system message, can contain multiple alternating user and assistant messages, and always ends with the last user message followed by the assistant header.

llama.meta.com