ChatGPT Prompt Engineering for Developers (0)

City_Duck·2023년 5월 22일
0

Prompt Engineering

목록 보기
1/3

ChatGPT란?

ChatGPT란 OpenAI가 개발한 GPT 3.5 or GPT 4 기반의 대형 언어 모델(LLM) 챗봇입니다.

여기서 LLM(Large Language Model)은 크게 2가지로 나눌 수 있습니다.

  1. Base LLM : predicts next word, based on text training data
  2. Instruction Tuned LLM : Tries to follow instructions

같은 문장을 input으로 주었을 때 방식에 따른 차이점을 쉽게 살펴볼 수 있습니다.

  • What is the capital of France? (input)
    • Base LLM (output)
      • What is France's largest city?
      • What is France's population?
    • Instruction Tuned LLM (output)
      • The capital of France is Paris

이 차이는 서로의 학습 과정의 차이에 의해 발생합니다.

Base LLM의 경우 인터넷에서 수집한 대량의 corpus를 통해 학습합니다.
"What is the capital of France?"와 같은 질문은 "What is France's largest city?"와 같은 질문과 같이 있는 경우가 많기에 질문의 대답보다는 확률적으로 높은 결과물을 반환합니다.

반면 Instruction Tuned LLM의 경우 사람의 피드백을 통해 강화학습을 진행하는 RLHF 기법과 instructions에 따른 good attempts을 함께 fine-tune하기에 학습하는 사람의 의도에 맞는 LLM을 만들 수 있습니다.

그렇기에 ChatGPT도 해당 방식인 Instruction Tuned 방식이 적용되었습니다.

그렇다면 어떻게 우리는 ChatGPT를 통해 원하는 답변을 얻을 수 있을까요?
해당 방식을 알기위해서는 Prompt Engineering을 알아야합니다.

따라서 본 포스팅은 DeepLearning.AI의 ChatGPT Prompt Engineering for Developers 강의를 통해 Prompt Engineering에 대해 알아보고자 합니다.

1. Guidelines for Prompting

해당 장에서는 LLM에 효과적인 두가지의 prompting 원리에 대해서 배우고자 합니다.

  • First principles : Write clear and specific instructions
  • Second principles : Give the model time to think

이에 앞서 OpenAI API key를 설정해야합니다.

import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key = os.getenv('OPENAI_API_KEY')

이후 API 결과값을 반환하는 함수를 작성합니다.

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message["content"]

1-1 Principle 1. Write clear and specific instructions

Tatic 1. Use delimiters

Use delimiters to clearly indicate distinct parts of the input

  • Delimiters can be anything like: ```, """, < >, , :
text = f"""
You should express what you want a model to do by \ 
providing instructions that are as clear and \ 
specific as you can possibly make them. \ 
This will guide the model towards the desired output, \ 
and reduce the chances of receiving irrelevant \ 
or incorrect responses. Don't confuse writing a \ 
clear prompt with writing a short prompt. \ 
In many cases, longer prompts provide more clarity \ 
and context for the model, which can lead to \ 
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \ 
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)

해당 방식과 같이 "Summarize the text delimited by triple backticks into a single sentence."
delimiters와 task를 지정하는 instructions을 주고 해당 delimiter에 맞는 ```{text}```을 통해 명확하게 instruction과 text를 구별할 수 있습니다.

이를 통해 다음과 같은 Prompt Injections를 방지할 수 있습니다.

Tatic 2. Ask for structured output

  • JSON, HTML
prompt = f"""
Generate a list of three made-up book titles along \ 
with their authors and genres. 
Provide them in JSON format with the following keys: 
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)

이와 같이 Instruction과 format을 지정하면 다음과 같은 결과를 얻을 수 있습니다.

Tatic 3. Check whether conditions are satisfied Check assumptions required to do the task

text_1 = f"""
Making a cup of tea is easy! First, you need to get some \ 
water boiling. While that's happening, \ 
grab a cup and put a tea bag in it. Once the water is \ 
hot enough, just pour it over the tea bag. \ 
Let it sit for a bit so the tea can steep. After a \ 
few minutes, take out the tea bag. If you \ 
like, you can add some sugar or milk to taste. \ 
And that's it! You've got yourself a delicious \ 
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes. 
If it contains a sequence of instructions, \ 
re-write those instructions in the following format:

Step 1 - ...
Step 2 - …
…
Step N - …

If the text does not contain a sequence of instructions, \ 
then simply write \"No steps provided.\"

\"\"\"{text_1}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)

"If it contains a sequence of instructions, re-write those instructions in the following format:" 이와 같이 조건을 검사하는 instructions 또한 사용할 수 있습니다.
해당 예시의 경우 만족했기에 다음과 같은 output이 반환됩니다.

이와 반대로 조건을 만족하지 않는 다음과 같은 text를 input한다면 다음과 같은 output이 반환됩니다.

Tactic 4. Few-shot prompting

기존의 Few-shot 방법과 동일하게 예시를 통해 prompting을 진행할 수 있습니다.

prompt = f"""
Your task is to answer in a consistent style.

<child>: Teach me about patience.

<grandparent>: The river that carves the deepest \ 
valley flows from a modest spring; the \ 
grandest symphony originates from a single note; \ 
the most intricate tapestry begins with a solitary thread.

<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)

해당 예시의 경우 다음과 같은 output이 반환됩니다.

1-2 Principle 2. Give the model time to "think"

Tactic 1. Specify the steps required to complete a task

text = f"""
In a charming village, siblings Jack and Jill set out on \ 
a quest to fetch water from a hilltop \ 
well. As they climbed, singing joyfully, misfortune \ 
struck—Jack tripped on a stone and tumbled \ 
down the hill, with Jill following suit. \ 
Though slightly battered, the pair returned home to \ 
comforting embraces. Despite the mishap, \ 
their adventurous spirits remained undimmed, and they \ 
continued exploring with delight.
"""
# example 1
prompt_1 = f"""
Perform the following actions: 
1 - Summarize the following text delimited by triple \
backticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following \
keys: french_summary, num_names.

Separate your answers with line breaks.

Text:
```{text}```
"""

prompt_2 = f"""
Your task is to perform the following actions: 
1 - Summarize the following text delimited by 
  <> with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the 
  following keys: french_summary, num_names.

Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in Italian summary>
Output JSON: <json with summary and num_names>

Text: <{text}>
"""

response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)

복잡한 문제를 해결하기 위해서는 다음과 같이 Step 별로 처리하는 instruction을 주면 좋다.

Tactic 2. Instruct the model to work out its own solution before rushing to a conclusion

prompt = f"""
Determine if the student's solution is correct or not.

Question:
I'm building a solar power installation and I need \
 help working out the financials. 
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost \ 
me a flat $100k per year, and an additional $10 / square \
foot
What is the total cost for the first year of operations 
as a function of the number of square feet.

Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
"""
response = get_completion(prompt)
print(response)

다음과 같은 요청을 하면 ChatGPT는 오답을 말해준다.
그렇기에 다음과 같은 Prompt를 사용하면 좋다.

prompt = f"""
Your task is to determine if the student's solution \
is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem. 
- Then compare your solution to the student's solution \ 
and evaluate if the student's solution is correct or not. 
Don't decide if the student's solution is correct until 
you have done the problem yourself.

Use the following format:
Question:
```
question here
```
Student's solution:
```
student's solution here
```
Actual solution:
```
steps to work out the solution and your solution here
```
Is the student's solution the same as actual solution \
just calculated:
```
yes or no
```
Student grade:
```
correct or incorrect
```

Question:
```
I'm building a solar power installation and I need help \
working out the financials. 
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat $100k per year, and an additional $10 / square \
foot
What is the total cost for the first year of operations \
as a function of the number of square feet.
``` 
Student's solution:
```
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
```
Actual solution:
"""
response = get_completion(prompt)
print(response)

위의 예시와 다른점은 LLM이 먼저 자신의 답을 계산한 후에 학생이 제시한 정답과 비교를 한다는 점이다.
해당 방식처럼 스텝을 나누어 모델이 생각할 시간을 확보해준다면 더 좋은 정확도를 얻을 수 있다.

Model Limitations: Hallucinations

Hallucinations은 그럴 듯한 거짓말을 만드는 것이다.

예로 들어 다음과 같은 prompt를 제시했을 때
"Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie"
ChatGPT는 가짜 product를 소개해준다.
이를 줄이기 위해서는 다음과 같은 Step을 사용할 수 있다.
1. 모델에게 관련된 정보를 찾아달라고 한다.
2. 해당 정보를 기반으로 질문을 한다.

A note about the backslash

  • GPT-3에서는 \을 사용해도 괜찮았지만 일반적인 LLM에서는 문제가 될 수 있다.
profile
AI 새싹

1개의 댓글

comment-user-thumbnail
2024년 1월 29일

Im Reich der modernen Technologie, wo Grenzen ständig verschoben werden und sich Möglichkeiten entfalten, liegt ein Tor zu einer Welt der nahtlosen Kommunikation und grenzenlosen Kreativität: ChatGPT Deutsch ( https://gptdeutsch.com/ ). Diese revolutionäre Plattform macht sich die immense Kraft der künstlichen Intelligenz zunutze und bietet eine interaktive Erfahrung, die die Art und Weise, wie wir mit Sprache umgehen, neu definiert.

답글 달기