크크루쿠쿠

Prompt Engineering Guide 1. Introduction 본문

DeepLearning/공부

Prompt Engineering Guide 1. Introduction

JH_KIM 2023. 3. 27. 01:54

이 글은 https://github.com/dair-ai/Prompt-Engineering-Guide 를 공부하기 위해 제 입맛대로 번역한 글입니다.

Introduction

Prompting Introduction

프롬프트 엔지니어링은 다양한 애플리케이션과 연구 주제에 언어 모델(LM)을 효율적으로 사용할 수 있도록 프롬프트를 개발하고 최적화하는 비교적 새로운 분야입니다. 프롬프트 엔지니어링 기술은 대규모 언어 모델(LLM)의 기능과 한계를 더 잘 이해하는 데 도움이 됩니다. 연구자들은 프롬프트 엔지니어링을 사용하여 질문 답변 및 산술 추론과 같은 일반적이고 복잡한 다양한 작업에서 LLM의 역량을 향상시킵니다. 개발자는 프롬프트 엔지니어링을 사용하여 LLM 및 기타 도구와 인터페이스하는 강력하고 효과적인 프롬프트 기술을 설계합니다.

All examples are tested with text-davinci-003(using OpenAI's playground) unless otherwise specified. It uses the default configurations, e.g., temperature=0.7 and top-p=1

Basic Prompts

우린 프롬프트를 이용해 많은걸 얻을 수 있지만 그 결과의 퀄리티는 얼마나 많은 정보를 제공하냐에 따라 다릅니다.

모델에 전달할 때 프롬프트는 intruction이나 qa같은 것 같은 정보나 inputs 과 examples 같은 정보를 추가로 전달도 가능합니다.

예시를 들어보면

# Prompt
The sky is 

# Output
blue

The sky is blue on a clear day. On a cloudy day, the sky may be gray or white.

보시다시피 LM 의 output은 “The sky is” 의 문맥과 관련된 string의 연속된 값을 보여줍니다.

output은 우리가 하려던 것과 멀 수 있습니다.

이 예시는 우리가 하려는 것에 대한 구체적인 context나 instruction들을 제공해야한다는 것을 강조합니다.

그렇다면 살짝 개선된 버전으로 입력해보면

# Prompt
Complete the sentence: 

The sky is

# Output
so  beautiful today.

이렇게 모델에게 “Complete the sentence” 라고 지시를 했을 때 결과가 더 우리의 목적에 맞다는 것을 볼 수 있습니다.

모델에 우리가 원하는 작업을 위해 optimal한 prompt를 설계하는 접근 방식을 Prompt Engineering 이라고 합니다.

위의 예시는 아주 기본적인 예시이고 오늘날에는 text summarization, mathematical reasoning and code generation 같은 매우 다양한 advanced task들을 수행이 가능합니다.

A Word on LLM Settings

여기 와 같은 API를 사용하며 prompt를 사용할 때 몇가지 parameter 를 설정해 다른 결과를 얻을 수 있습니다.

  1. Temperature
  2. 요약하면 낮은 Temperature 는 더욱 deterministic한 결과를 보여줍니다. 낮으면 낮을수록 다음 token에 대한 선택에 높은 prob을 가진 토큰이 선택됩니다. Temperature 가 높이면 randomness가 더 높아지게 되면서 더욱 다양하고 창의적인 output를 볼 수 있습니다. 이 parameter는 다른 가능한 토큰의 가중치를 조절하는 parameter입니다. 낮은 Temperature 를 사용할 때는 fact base QA 와 같은 task에서 더욱 사실적이고 간결한 답변을 얻을 수 있습니다. 반대로 높은 Temperature 같은 경우에는 시 창자이나 다른 creative 한 task에 적합하다고 볼 수 있습니다.
  3. Top_p
  4. Temperature 과 비슷합니다. temperature를 샘플링하는 기법인 nucleus samplling 기법인데 얼마나 모델이 응답을 deterministic하게 생성하는지 control해주는 parameter입니다. 만약 더욱 정확하고 factual한 답변을 원한다면 이 parameter를 낮게 유지해야 합니다. 반대로 다양한 응답을 원한다면 이 param을 높게 유지하면 됩니다.

일반적으로 둘 다 변경하는것보다 하나만 변경하는 것이 좋습니다.

Standard Prompts

우리는 매우 간단한 prompt를 시도해볼 것입니다.

# which is standard in a lot of QA dataset
Q: <Question>?
A:

위의 standard format을 고려하면 유명하고 효과적인 prompt 기법중 하나인 few-shot prompting를 소개하겠습니다.

Q: <Question>?
A: <Answer>

Q: <Question>?
A: <Answer>

Q: <Question>?
A: <Answer>

Q: <Question>?
A:

꼭 이 format을 hard하게 맞출필요는 없습니다. 형식은 task에 맞게 가져가면 됩니다. 예시를 들면

# Prompt
This is awesome! // Positive
This is bad! // Negative
Wow that movie was rad! // Positive
What a horrible show! //

# Output
Negative

Few-shot prompts는 몇가지 예제로 context 를 LM이 학습이 가능하게끔 합니다.

Elements of Prompt

다양한 Prompt의 예시들과 apps 를 점점 더 다루게 되면서 프롬프트를 구성하는 특정 요소들이 있다는 것을 알게 될 것입니다.

  1. Instruction - 모델이 수행하기 원하는 특정 task나 명령어
  2. Context - 모델이 더 좋은 응답을 하기 위해 포함될 수 있는 외부 정보나 추가적인 문맥
  3. Input Data - 응답을 찾고자 하는 입력 또는 질문
  4. Output Indicator - output의 유형이나 format

Prompt에 모든 구성 요소가 필요한것은 아닙니다. 당연하게 어떤 task냐에 따라 다르게 됩니다.

General Tips for Designing Prompts

Start Simple

prompt를 디자인 시작할 때는 optimal한 result는 수많은 실험이 필요한 반복적인 process입니다.

간단한 프롬프트로 시작해서 여러 요소나 contexts를 추가함으로써 더나은 결과를 가져올 수 있습니다. 그렇기 때문에 prompt의 Versioning이 중요합니다. 가이드를 읽다 보면 많은 예시들이 specificity, simplicity and conciseness 를 통해 더 좋은 결과를 얻을 수 있는 것을 볼 수 있습니다.

만약 여러 subtask가 포함된 큰 task를 하게된다면 그 task를 simple task로 쪼개고 더 낳은 결과를 얻을 때까지 쌓아올려가세요. 이러한 과정으로 prompt 디자인 프로세스를 시작할 때 너무 많은 complexity를 추가하는 것을 피할 수 있습니다.

The Instruction

Write”, “Classify’, “Summarize”, “Translate”, “Order” 과 같은 commands를 사용해 model에 명령함으로 다양한 simple task의 effective prompts를 디자인 할 수 있습니다.

어떤 것이 best인지 알기 위해서는 많은 실험이 필요한 것을 명심해야 합니다. 특정 case와 task에 가장 best 인지에 대해서는 다양한 명령어와 함께 다양한 keywords, contexts and data를 시도해서 알아야 합니다. 대게 너가 하려는 task와 연관성이 높고 구체적일수록 성능이 잘나온다.

추가적으로 프롬프트의 시작 부분에 instruction을 배치하는게 좋은 방법입니다. ###와 같은 명확한 separator를 이용해 context와 instruction을 구분하는것도 좋습니다. 예를 들면

# Prompt
### Instruction ###
Translate the text below to Korean:

Text: "hello!"

# Output
안녕하세요!

Specificity

모델이 수행하고자 하는 task와 instruction을 매우 구체적으로 설명하세요. 특별히 원하는 결과나 생성관련 스타일이 있을 때 중요합니다. 더 나은 결과로 이어지는 특정 token이나 키워드는 없습니다. 더 중요한것은 좋은 format과 descriptive한 prompt입니다. 특히 prompt에서 예시를 제공하는 것은 특정 결과의 output을 얻는데 효과적입니다.

prompt의 길이에 제한이 있다는 것도 염두에 둬야합니다. 얼마나 구체적이고 상세해야 할지도 고려해야 합니다. 필요없는 디테일이 너무 많은 것은 좋은 접근방식이 아닙니다. 디테일은 연관성이 있어야 합니다. 그래서 이런 점이 실험이 많이 필요한 부분입니다.

예를 들어서 text에서 특정 정보를 추출하는 간단한 prompt를 만들어 봅시다.

# Prompt
Extract the name of places in the following text. 

Desired format:
Place: <comma_separated_list_of_company_names>

Input: "Although these developments are encouraging to researchers, much is still a mystery. 
“We often have a black box between the brain and the effect we see in the periphery,” 
says Henrique Veiga-Fernandes, a neuroimmunologist at the Champalimaud Centre for the Unknown in Lisbon. 
“If we want to use it in the therapeutic context, we need to understand the mechanism."

# Output
Place: Champalimaud Centre for the Unknown, Lisbon

Avoid Impreciseness

위의 몇가지 팁을 고려할 때 너무 똑똑해지려는 욕심에 부정확한 설명을 작성하는 함정에 빠질 수 있습니다. 보통 직접적이거 구체적인게 좋습니다.

예시로 프롬프트 엔지니어링에 관심이 있을 때 다음과 같이 시도해 볼 수 있습니다.

Explain the concept of prompt engineering. 
Keep the explanation short, only a few sentences, and don't be too descriptive.

이는 몇개의 문장을 사용하고 어떤 스타일을 사용하는지에 대해 명확하지 않습니다. 위의 prompt로도 적당히 좋은 응답을 얻을 수는 있지만 더 좋은 prompt는 매우 구체적이고 간결하며 핵심을 짚습니다. 예를 들자면.

Use 2-3 sentences to explain the concept of prompt engineering to a high school student.

To do or not to do?

다른 팁으로 하지 말아야 할것을 얘기하는게 아닌 해야할 것을 얘기하는 것입니다. 이렇게 하면 보다 구체적이고 세부적인 사항에 집중해서 좋은 반응을 이끌어낼 수 있습니다.

다음은 하지말아야 할 것에 초점을 둬 정확한 반응을 받지못한 예시입니다.

# Prompt
The following is an agent that recommends movies to a customer. 
DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION.
Customer: Please recommend a movie based on my interests.
Agent:

# Output
Sure, I can recommend a movie based on your interests. 
What kind of movie would you like to watch? 
Do you prefer action, comedy, romance, or something else?

좋은 예시의 경우에는

# Prompt
The following is an agent that recommends movies to a customer. 
The agent is responsible to recommend a movie from the top global trending movies. 
It should refrain from asking users for their preferences and avoid asking for personal information. If the agent doesn't have a movie to recommend, it should respond "Sorry, couldn't find a movie to recommend today.".

Customer: Please recommend a movie based on my interests.
Agent:
Output:
# Output
Sorry, I don't have any information about your interests. 
However, here's a list of the top global trending movies right now: [list of movies]. 
I hope you find something you like!
Comments