Meta의 Llama 모델에 대한 Prompt Engineering 공식 문서에 대해 정리해보려고 한다.
https://www.llama.com/docs/how-to-guides/prompting/
Prompt engineering | How-to Guides
Learn how to improve the performance of large language models through prompt engineering by crafting effective prompts and using techniques such as zero-shot and few-shot prompting, role-based prompts, and retrieval-augmented generation. Discover how to re
www.llama.com
Prompt Engineering이란?

이미 LLMOps에서 핵심적인 요소로 자리잡은 Prompt Engineering이란, 자연어 처리(NLP)에서 LLM의 성능을 향상시키기 위해 사용되는 기법이다.
이때, 모델에게 더 많은 Context와 Task information을 제공하기 위해, 짧은 Text 조각인 Prompt를 설계하는 것이 핵심이다.
이를 통해 모델이 보다 정확하고 관련성 높은 결과를 생성하도록 유도할 수 있다.
모델의 성능을 실제 서비스 환경에서 사용할 수 있는 수준으로 발전시키는 방법으로는 Prompt Engineering 외에도, Fine-tuning, Distillation, 더 큰 모델로의 업그레이드 등이 있지만, Prompt 최적화는 추가적인 모델 학습이나 인프라 비용 없이 가장 빠르게 성능 개선을 얻을 수 있는 방법이다.
Prompt 작성 방법
1. 명확하고 간결하게 작성하기
Prompt는 이해하기 쉬워야 하며, 모델이 적절한 출력을 생성하는 데 필요한 정보는 충분히 포함해야 한다.
불필요한 jargon이나 과도한 기술 용어는 모델을 혼란스럽게 만들 수 있으므로 피하는 것이 좋다.
2. 구체적인 예시 사용 (Few-shot)
원하는 출력 형태에 대한 구체적인 예시(few-shot)를 제공하면, 모델이 기대되는 결과를 더 잘 이해할 수 있다.
예를 들어 특정 주제의 이야기를 생성하고 싶다면, 배경·등장인물·플롯에 대한 몇 문장을 함께 제시하는 것이 도움이 된다.
3. 다양한 Prompt 시도하기
서로 다른 prompt를 사용하면 모델이 동일한 작업을 더 잘 수행하거나, 더 다양하고 창의적인 결과를 낼 수 있다.
스타일, 톤, 형식을 바꿔가며 실험해보는 것이 중요하다.
4. 테스트 및 반복 개선
작성한 prompt를 실제로 모델에 적용해보고 결과를 확인해야 한다.
원하는 결과가 나오지 않으면, 세부 정보를 추가하거나 톤과 스타일을 조정하면서 반복적으로 개선한다.
5. 피드백 수집
사용자 또는 외부 피드백을 통해 prompt를 지속적으로 개선하면, 모델이 더 많은 가이드를 필요로 하는 부분을 파악할 수 있다.
Explicit instruction의 방법
일반적으로 open-ended prompt보다 상세하고 명시적인 지시(explicit instructions)가 더 좋은 결과를 낸다.
이는 모델의 응답 범위와 규칙을 명확히 제한해주는 역할을 한다.
1. Stylization (스타일 제어)
Prompt를 통해 모델의 글쓰기 스타일을 유도할 수 있다.
Explain this to me like a topic on a children's educational network show teaching elementary students.
I'm a software engineer using LLMs for summarization. Summarize the following text in under 250 words:
Give your answer like an old-timey private investigator hunting down a case step-by-step.
2. Formatting (출력 형식 지정)
출력 형식을 명시적으로 요청할 수 있다.
Use bullet points.
Return as a JSON object.
Use fewer technical terms and help me apply it in my work in communications.
3. Restrictions (제약 조건)
제약 조건은 모델이 하지 말아야 할 것을 정의한다.
Only use academic papers.
Never give sources older than 2020.
If you don't know the answer, say that you don't know.
Prompting Technique
1. Zero-shot & Few-shot Prompting
Shot이란, 어려운 것이 아니라 모델에게 보여주는 예시를 의미한다.
Zero-shot의 의미는 사전 예시 없이 지시만으로 작업을 수행하는 방식이고,
Few-shot의 의미는 원하는 출력 예시 몇 개를 제공하여 정확도와 일관성을 높이는 방식이다.
Text: This was the best movie I've ever seen! The sentiment of the text is:
Text: The director was trying too hard. The sentiment of the text is:
2. Role-based Prompting
모델에게 역할을 부여하는 방식이다.
이를 통해 응답의 관련성과 몰입도를 증가시키고 오해나 오류를 줄일 수 있다.
그러나 역할에 대한 정보 설계에 추가적인 노력이 필요하다.
You are a virtual tour guide walking tourists around Eiffel Tower on a night tour.
Describe Eiffel Tower to your audience in a way that includes its history, the number of people visiting each year, the amount of time it takes to do a full tour and why so many people visit it each year.
3. Chain-of-Thought Prompting
문제를 단계적으로 사고하도록 유도하는 방식으로, 논리적이고 일관된 응답과 깊이 있는 분석을 기대할 수 있지만,
Prompt 설계가 복잡하다는 단점이 있다.
You are a virtual tour guide from 1901. You are guiding tourists visiting Eiffel Tower.
Describe Eiffel Tower to your audience.
Begin with:
1. Why it was built
2. Then by how long it took them to build
3. Where were the materials sourced to build
4. Number of people it took to build
End with the number of people visiting the Eiffel tour annually in the 1900's, the amount of time it completes a full tour and why so many people visit this place each year.
Make your tour funny by including 1 or 2 funny jokes at the end of the tour.
4. Self-consistency
LLM은 확률적이기 때문에, 한 번만 수행한 결과가 틀릴 가능성이 있다.
Self-consistency는 동일한 질문을 여러 번 실행항 뒤, 가장 많이 등장한 답을 선택하는 방식이다.
정확도는 높아지지만 계산 비용은 증가한다는 한계가 있다.
John found that the average of 15 numbers is 40.
If 10 is added to each number then the mean of the numbers is?
Report the answer surrounded by three backticks, for example: ```123```
5. Retrieval-Augmented Genearation (RAG)
모델이 학습하지 않은 Domain, 시점 특화 정보를 다루기 위해,
외부 데이터베이스에서 정보를 검색해 prompt에 포함시키는 기법이다.
Fine-tuning보다 저렴하면서 기본 모델의 성능을 훼손시키지 않는다는 장점이 있다.
Given the following information about temperatures in Menlo Park:
2023-12-11 : 52 degrees Fahrenheit
2023-12-12 : 51 degrees Fahrenheit
2023-12-13 : 55 degrees Fahrenheit
What was the temperature in Menlo Park on 2023-12-12?
# Sure! The temperature in Menlo Park on 2023-12-12 was 51 degrees Fahrenheit.
What was the temperature in Menlo Park on 2023-07-18 ?
# Sorry, I don't have information about the temperature in Menlo Park on 2023-07-18.
The information provided only includes temperatures for December 11th, 12th, and 13th of 2023.
6. Limiting extraneous tokens (불필요한 토큰 제한)
LLM Application에서 흔히 겪는 문제 중 하나가, 모델이 적절한 응답 대신 불필요한 토큰(extraneous tokens)을 함께 생성하는 것이다. 이는 다음과 같은 예시가 있다. “Sure! Here’s more information on…” / “Of course! I’d be happy to help…”
이러한 문제는 역할, 규칙과 제약, 명시적인 지시, 예시 등의 요소를 사용함으로써 줄일 수 있다.
이를 통해 모델이 원하는 형식의 응답만 생성하도록 유도할 수 있다
You are a robot that only outputs JSON.
You reply in JSON format with the field 'zip_code'.
Example question: What is the zip code of the Empire State Building? Example answer: {'zip_code': 10118}
Question: What is the zip code of Menlo Park?
# {'zip_code': 94025}
7. Program-aided language models
LLM은 본질적으로 수치 계산(arithmetic)에는 강하지 않다.
하지만 코드 생성(code generation)에는 매우 뛰어나다.
이 방법은 모델에게 직접 계산을 시키는 대신, 계산을 수행하는 코드를 생성하도록 지시하는 방식이다.
Only return Python code, nothing else.
Generate python code to calculate the following: ((-5 + 93 * 4 - 0) * (4^4 + -7 + 0 * 5))
def calculate_expression():
# Calculate the first part of the expression
part1 = -5 + 93 * 4 - 0
# Calculate the second part of the expression
part2 = 4 ** 4 + -7 + 0 * 5
# Calculate the final result
result = part1 * part2
return result
print(calculate_expression())
이렇게 생성된 코드를 실제로 실행하면 올바른 결과를 얻을 수 있다.
'Agentic AI 구축' 카테고리의 다른 글
| Meta의 Llama 시리즈 Overview (Llama 1 ~ Llama 4) (0) | 2025.12.11 |
|---|---|
| MCP 아키텍처 총정리 (MCP Host, MCP Server) (0) | 2025.11.05 |