언어 모델 프롬프트 최적화

완료됨

프롬프트는 LLM(대규모 언어 모델)에 제공하는 대화형 큐로, 쿼리 또는 지침에 따라 응답을 형성합니다. 예를 들어 문장을 영어에서 프랑스어로 변환하거나 텍스트 요약을 생성하라는 메시지를 LLM에 표시할 수 있습니다.

이전 단원에서는 프롬프트를 입력 문자열로 만들었습니다.

string input = @"I'm a vegan in search of new recipes. I love spicy food! 
Can you give me a list of breakfast recipes that are vegan friendly?";
input = """I'm a vegan in search of new recipes. I love spicy food! 
Can you give me a list of breakfast recipes that are vegan friendly?"""

이 프롬프트에서는 지침과 함께 언어 모델에 콘텐츠를 제공합니다. 콘텐츠는 모델이 사용자와 더 관련성이 있는 결과를 생성하는 데 도움이 됩니다.

프롬프트에는 원하는 응답을 생성하도록 모델을 안내하는 명확하고 컨텍스트가 풍부한 지침을 만드는 작업이 포함됩니다. 효과적인 프롬프트를 만들려면 정밀도와 명확성이 핵심입니다. 정확한 결과를 위해 프롬프트를 실험하고 조정해야 할 수 있습니다.

예제를 사용하여 모델 가이드

프롬프트에 예제를 포함하여 응답을 안내할 수 있습니다. 예제는 지침 전후에 제공 될 수 있습니다. 프롬프트 예제는 축자 완성이 포함되어 있는지 여부에 따라 제로샷 학습 또는 몇 번의 학습으로 분류됩니다. 직접 완료는 프롬프트에 포함된 응답의 사전에 정의된 특정 예입니다. AI에게 기대되는 답변의 종류를 단어 하나하나 정확히 보여줍니다. 이러한 예제를 사용하면 AI가 제공된 완성도의 구조, 스타일 또는 톤을 더 쉽게 모방할 수 있습니다.

제로샷 학습

제로샷 학습을 통해 지침은 포함하지만 말 그대로 완성은 제외합니다. 제로샷 프롬프트는 모델의 기존 지식을 사용하여 응답을 생성합니다. 제로샷 프롬프트는 일반적인 답변을 원하거나 작업이 간단하며 많은 지침이 필요하지 않은 경우에 유용합니다. 제로샷 프롬프트는 기존 지식에 의존하기 때문에 리소스 집약적이 적습니다.

다음은 모델에 사용자 입력을 평가하고, 사용자의 의도를 확인하고, 출력 앞에 "의도: "를 표시하도록 지시하는 제로샷 프롬프트의 예입니다.

string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent: 
""";
prompt = f"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent: 
"""

몇 가지 샷 학습

몇 번의 학습을 통해 모델의 응답을 안내하는 데 도움이 되는 프롬프트에 축자 완성을 포함합니다. 일반적으로 1~5개의 예제가 포함됩니다. 예제에서는 원하는 응답의 구조, 스타일 또는 형식을 보여 줍니다. 몇 번의 학습은 더 많은 토큰을 생성하고 모델이 지식을 업데이트하도록 합니다. 몇 샷 프롬프트는 모호성을 줄이고 원하는 결과에 맞춰 조정하는 데 특히 유용합니다.

다음은 모델에 사용자 입력을 평가하고, 사용자의 의도를 결정하고, 출력 앞에 "의도: "를 표시하도록 지시하는 몇 번의 프롬프트의 예입니다.

string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.

User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage

User Input: Can you send the full update to the marketing team?
Intent: SendEmail

User Input: {request}
Intent:
""";
prompt = f"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.

User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage

User Input: Can you send the full update to the marketing team?
Intent: SendEmail

User Input: {request}
Intent:
"""

프롬프트에서 가상 사용자 사용

프롬프트에서 가상 사용자를 할당하는 것은 응답을 생성할 때 특정 관점, 톤 또는 전문 지식을 채택하도록 모델을 안내하는 데 사용되는 기술입니다. 가상 사용자를 사용하면 작업의 컨텍스트 또는 대상에 더 잘 맞게 출력을 조정할 수 있습니다. 페르소나는 직업을 시뮬레이션하거나 음성 톤을 반영하기 위해 응답이 필요할 때 유용합니다. 가상 사용자를 할당하려면 프롬프트에서 역할 정의를 명확하게 설명해야 합니다.

가상 사용자를 할당하는 프롬프트의 예는 다음과 같습니다.

string prompt = $"""
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
""";
prompt = """
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
"""

생각의 연쇄 유도

일련의 생각 프롬프트가 표시되면 모델에서 작업을 단계별로 수행하고 각 단계와 결과를 출력 순서대로 표시하라는 메시지를 표시합니다. 체인 프롬프트를 사용하면 일부 실행 계획을 모델에 오프로드하여 프롬프트 엔지니어링을 간소화할 수 있습니다. 체인 프롬프트를 사용하면 문제를 특정 단계로 쉽게 격리할 수 있으므로 추가 노력을 집중할 위치를 알 수 있습니다. 모델에 생각 체인을 포함하도록 지시하거나 예제를 사용하여 작업을 분해하는 방법을 모델에 표시할 수 있습니다.

다음은 단계별 추론을 설명하도록 모델에 지시하는 예제입니다.

string prompt = $"""
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
""";
prompt = """
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
"""

모델을 완성하기 위한 단계들을 설명하는 예가 있습니다.

prompt = $"""
Instructions: A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?

First, calculate how many full baskets the farmer can make by dividing the total apples by the apples per basket:
1. 

Next, subtract the number of apples used in the baskets from the total number of apples to find the remainder: 
1.

"Finally, the farmer will eat the remaining apples:
1.
""";
prompt = """
Instructions: A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?

First, calculate how many full baskets the farmer can make by dividing the total apples by the apples per basket:
1. 

Next, subtract the number of apples used in the baskets from the total number of apples to find the remainder: 
1.

Finally, the farmer will eat the remaining apples:
1.
"""

이 프롬프트의 출력은 다음 출력과 유사합니다.

Divide 150 by 12 to find the number of full baskets the farmer can make: 150 / 12 = 12.5 full baskets
The farmer can make 12 full baskets with 12 apples each.
Multiply 12 full baskets by 12 apples per basket to find the number of apples used in the baskets: 12 * 12 = 144 apples
Subtract the number of apples used in the baskets from the total number of apples: 150 - 144 = 6 apples
The farmer will eat 6 remaining apples.

프롬프트 작성 팁

  • 특정 출력을 생성하는 특정 입력: LLM은 수신하는 입력에 따라 응답합니다. 원하는 출력을 얻으려면 명확하고 구체적인 프롬프트를 만드는 것이 중요합니다.

  • 실험이 핵심입니다. 모델이 응답을 해석하고 생성하는 방법을 이해하기 위해 다양한 프롬프트를 반복하고 실험해야 할 수 있습니다. 작은 조정으로 인해 결과가 크게 변할 수 있습니다.

  • 컨텍스트의 중요성: LLM은 프롬프트에 제공된 컨텍스트를 고려합니다. 정확하고 일관된 응답을 얻기 위해 컨텍스트가 잘 정의되고 관련성이 있는지 확인해야 합니다.

  • 모호성 처리: LLM이 모호한 쿼리로 어려움을 겪을 수 있음을 명심하세요. 모호하거나 예기치 않은 결과를 방지하기 위해 컨텍스트 또는 구조를 제공합니다.

  • 프롬프트 길이: LLM은 짧고 긴 프롬프트를 모두 처리할 수 있지만 간결성과 명확성 간의 균형을 고려해야 합니다. 프롬프트 길이를 실험하면 최적의 균형을 찾는 데 도움이 될 수 있습니다.

효과적인 프롬프트를 만들려면 선명도, 정밀도 및 사려 깊은 디자인이 필요합니다. 제로 샷 및 몇 샷 학습, 페르소나 할당 및 생각의 체인 프롬프트와 같은 기술은 응답의 품질과 관련성을 향상시킬 수 있습니다. 필요한 경우 명확한 지침, 잘 정의된 컨텍스트 및 예제를 제공하여 모델을 안내하여 세밀하게 조정된 관련 응답을 생성할 수 있습니다. 최상의 결과를 얻으려면 프롬프트를 실험하고 구체화해야 합니다.