본문 바로가기
카테고리 없음

Step-Back Prompt Engineering

by ML_MJSHIN 2024. 4. 9.

https://arxiv.org/pdf/2310.06117.pdf

# 개요 

LLM (Large Language Model) 의 상승세와 함께 LLM을 활용한 Application에 대한 관심도가 증가하고 있다. 이와 함께 등장한 하나의 중요한 키워드가 Prompt Engineering 이다. 

 

여기에서 "프롬프트"는 LLM에게 특정 작업을 수행하도록 요청하는 "자연어 텍스트" 라고 정의할 수 있다. 이 프롬프트를 어떻게 설계하느냐가 LLM 모델의 출력 품질에 결정적인 영향을 끼지며 효과적인 프롬프트 설계는, 모델이 사용자의 의도를 정확하게 파악하고, 맥락에 맞는 유용한 답변을 생성할 수 있도록 한다.

 

https://www.coursera.org/learn/generative-ai-with-llms/home/week/1]

 

위 그림은 프롬프트가 무엇인지 이해하기 제일 쉬운 사진이라고 생각된다. 사용자의 질문은 프롬프트가 되며 LLM에게 전달되어지고 LLM은 사용자의 질문에 대한 결과를 생성한다. 

 

이 블로그에서 다루고자 하는 Step-Back Prompt Engineering 이 왜 등장했는지를 이해하려면 다음과 같은 사실에 대해서는 숙지해야 한다.

LLM의 성능은 입력 데이터에 매우 민감하다. 예를들어, 한국어로 "설명하다" 라는 말이 영어 단어로 "describe", "explain" 등이 있을 수 있는데 이에 따라 결과가 달라진다. 매우매우 민감하고 까다롭다.  뿐만 아니라 전달하는 내용의 순서도 중요하고... 예민한 친구다.

 

그래서 단어 선택, 질문 구성, 문맥 설정 등이 매우 중요해지게 되고 프롬프트 엔지니어링은 이러한 요소들을 모두 고려하여, LLMdl 단순히 언어를 처리하는 수준을 넘어서, 인간처럼 생각하고 반응할 수 있도록 만들어주는 핵심 기이 Prompt Engineering이다.  결과적으로, LLM의 상승세를 타고 뛰어난 프롬프트 엔지니어링은 AI 애플리케이션의 효율성과 창의성을 극대화하는 데에 절대적인 역할을 하고 있다. 

 

물론 OpenAI에서는 프롬프트가 큰 영향을 미치는 것이 버그라고 하면서 고쳐나갈 것이라고 했지만 아직은 시기상조가 아닐까 싶다. 작동 원리에 의해서 어쩔 수 없지 않나? 라는 생각도 들고 ...

 

# Step-Back Prompt Engineering

본격적으로 포스팅의 제목인 Step-Back Prompt Engineering에 대해서 살펴보도록 하자.

 

https://github.com/Tongji-KGLLM/RAG-Survey/

 

위 사진은 LLM을 활용한 Application을 개발할 때 핵심으로 여겨지고 있는 기술인 RAG (Retrieval Augmented Generation) 의 파이프라인 요소 별 기술표이다. RAG를 몰라도 Step-Back Prompt Engineering이 Pre-Retrieval 의 Query Transformation (파란박스 우상단) 에 있다는 것 정도만 우선 파악해도 된다.

 

Step-Back Prompt Engineering이 Query Transform의 한 종류라는 것을 이해 했다면, 이때 Query라는 건 LLM 에게 요청하기 위한 내용이라는 것 정도는 감으로 알 수 있을 것이라고 생각한다.

 

Step-Back Prompt Engineering은 Query가 주어졌을 때 Query를 이해하고 보다 큰 그림을 먼저 보고 그것으로부터 해결책을 찾아가는 방식이라고 생각하면 된다. 더 쉽게 말하면, LLM에게 주어진 Query에서 중요한 개념이나 기본 원칙을 먼저 파악하라고 하고, 이 정보를 바탕으로 LLM이 문제의 답을 찾아가게 하는 방식이다.

 

huggingface

 

위 그림은 Step-Back Prompt Engineering에 관한 논문의 그림이다. 우측 상자의 Original Question이 Query이고 Step 1 으로

이 Query에 대한 "중요 개념"인 physics principles 와 이의 기본 원칙인 "ideal gas law" 에 대해 파악하여 정보를 만들어 낸 뒤 이 정보와 Query를 함께 이용해서 Step 2에서 답을 찾는 것을 이해할 수 있다.

 

이를 통해서 얻는 장점과 단점은 다음과 같다.

 

장점

(1) 향상된 추론 능력 - 분석, 추론, 지식 적용을 요구하는 작업에서의 성능 향상.

(2) 오류율 감소 - Query에 대한 Response 생성시에 핵심적인 측면에 집중하게 하여 오류율을 줄임

 

단점

(1) 프롬프트 엔지니어링의 어려움 - 효과적인 프롬프트를 만들기 위해서는 모호함이나 편향을 피하기 위한 정밀함이 요구

(2) Step-Back 의 리소스 비용 - 추가적인 추상화 단계가 처리 과부하를 약간 가중시키며, 자원이 제한된 경우 문제가 있음

(3) 응용에 따른 적용 가능성 - 특히 텍스트 생성 등 모든 LLM 애플리케이션에 필요하지 않을 수 있음

 

# 정리

사실 Step-Back Prompt Engineerning의 개념이 위와 같다는 것만 인지하고 실제로 사용하기 위해서는 내가 만들고자 하는 Application에 맞는 Step-Back 용 프롬프트를 만들어야하는 어려움이 있다.

 

그래서 하나의 예시를 가져왔으니까 한번 같이 봐보자.

 

Here is a question or task: How many presidents were born in New York?

Let's think step-by-step to answer this:
Step 1) Abstract the key concepts and principles relevant to this question:

Step 2) Use the abstractions to reason through the question:
Final Answer:

 

위의 내용이 prompt이다. 

 

여기에서 주목해야 할 점은 Step 1) 과 Step 2) 부분이다. 이 부분이 이 포스팅의 첫 번째 줄에서 연결된 arxiv 논문에서 몇 페이지씩 설명하고 있는 Step-Back Prompt Engineering에 관한 사용 예제라고 볼 수 있다.

 

Step 1을 통해서 Query 에 대해서 ""추상화"를 통해서 "키워드"와 "기본 원리"를 찾아내줘" 라는 요청을 하고. Step 2를 통해서 이 정보를 이용해서 답을 만들어줘 라고 전달하고 있다.

 

이렇게 하면 우리의 친절한 LLM은 아래와 같이 답을 준다.

Step 1) Abstract the key concepts and principles relevant to this question:

To answer this question, we need to know the birthplaces of all the presidents of the United States and be able to filter that information to count those born in New York state. The key concepts here include understanding the definition of a president in the context of the United States, which is the head of state and head of government, and the term "born in New York" which refers to the actual place of birth, not necessarily where they lived or served as politicians.

Step 2) Use the abstractions to reason through the question:

With the list of all presidents and their places of birth, we would look for those who were born within the geographical boundaries of what is now known as New York state. This list would exclude any president born elsewhere, regardless of their association with New York later in life.

As of my last update in April 2023, there were five U.S. presidents born in New York: Martin Van Buren, Millard Fillmore, Theodore Roosevelt, Franklin D. Roosevelt, and Donald Trump.

Final Answer: Five presidents of the United States were born in New York.

 

또 다른 사용법으로는 아래와 같이 할 수도 있다.

 

먼저 LLM에게 1번째로 아래와 같이 요청을 한다.

 

Here is a question or task: How many presidents were born in New York?

Step 1) Abstract the key concepts and principles relevant to this question:

 

그러면 LLM 은 아래와 같은 결과를 줄 것이고...

 

- Presidents: Individuals who have held the highest office in the United States.
- Born in New York: Refers to individuals who were born within t he geographical boundaries of the state of New York.

 

이 LLM의 Step 1에 대한 정보와 함께 다음과 같이 다시 프롬프트를 보내면 ..

 

Here is a question or task: How many presidents were born in New York?

Step 1) Abstract the key concepts and principles relevant to this question:

- Presidents: Individuals who have held the highest office in the United States.
- Born in New York: Refers to individuals who were born within t he geographical boundaries of the state of New York.

Step 2) Use the abstractions to reason through the question:

 

다시 LLM이 다음과 같은 결과를 반환한다.

Based on the key concepts identified, we can establish the following reasoning:

To find the number of U.S. presidents born in New York, we consider only their birthplaces, not where they were raised or where they pursued their political careers.
The information we are considering is factual and historical, which means it can be verified through reliable sources such as historical records, presidential libraries, and official biographies.
Since the number of presidents and their birthplaces is a matter of public record, we do not need to speculate or infer but can rely on established data.
Using this reasoning with the data available as of April 2023, we know that the following presidents were born in New York:

Martin Van Buren was born in Kinderhook, New York, on December 5, 1782.
Millard Fillmore was born in Moravia, New York, on January 7, 1800.
Theodore Roosevelt was born in New York City, New York, on October 27, 1858.
Franklin D. Roosevelt was born in Hyde Park, New York, on January 30, 1882.
Donald Trump was born in New York City, New York, on June 14, 1946.

 

그래서 이 정보들을 다시 다 모아서 아래처럼 보내면?

 

Here is a question or task: How many presidents were born in New York?

Step 1) Abstract the key concepts and principles relevant to this question:

- Presidents: Individuals who have held the highest office in the United States.
- Born in New York: Refers to individuals who were born within t he geographical boundaries of the state of New York.

Step 2) Use the abstractions to reason through the question:

Based on the key concepts identified, we can establish the following reasoning:

To find the number of U.S. presidents born in New York, we consider only their birthplaces, not where they were raised or where they pursued their political careers.
The information we are considering is factual and historical, which means it can be verified through reliable sources such as historical records, presidential libraries, and official biographies.
Since the number of presidents and their birthplaces is a matter of public record, we do not need to speculate or infer but can rely on established data.
Using this reasoning with the data available as of April 2023, we know that the following presidents were born in New York:

Martin Van Buren was born in Kinderhook, New York, on December 5, 1782.
Millard Fillmore was born in Moravia, New York, on January 7, 1800.
Theodore Roosevelt was born in New York City, New York, on October 27, 1858.
Franklin D. Roosevelt was born in Hyde Park, New York, on January 30, 1882.
Donald Trump was born in New York City, New York, on June 14, 1946.



Final Answer:

 

LLM이 모든 정보를 잘 모아모아서 최적의 결론을 잘 낼 수 있게 된다.

Five presidents of the United States were born in New York.

 

이런 Step-Back Prompt Engineering 방식이 잘 되는 이유는 "Self-Consistency" 라는 프롬프트 기법에 대한 용어에서 LLM에게 어떤 질문을 하여 답변을 원할 때 해당 질문과 관련된 Knowledge를 같이 전달하면 One-Shot 과 같이 예제를 같이 넣어주지 않아도 제공된 정보를 기반으로 답변을 잘 내놓는다 라는 것과 관련이 있는 내용이다. 

 

Step-Back Prompt Engineering을 찾아봤다면 Self-Consistency도 살펴보기를 추천한다.