일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- N21
- RNN
- nlp
- 데이터 구축
- 현대자동차
- ODQA
- 기아
- word2vec
- AI 경진대회
- AI Math
- Ai
- Transformer
- Optimization
- Attention
- 데이터 시각화
- KLUE
- GPT
- 2023 현대차·기아 CTO AI 경진대회
- passage retrieval
- matplotlib
- pyTorch
- N2N
- seaborn
- Bert
- dataset
- mrc
- 딥러닝
- Bart
- Data Viz
- Self-attention
- Today
- Total
쉬엄쉬엄블로그
ChatGPT Prompt Engineering for Developers 본문
1. Introduction
Two Types of large language models (LLMs)
Base LLM
- 학습 데이터를 기반으로 다음 단어를 예측하는 모델
- 학습 데이터가 프랑스 국가에 대한 퀴즈 질문 목록일 수도 있기 때문에 프랑스의 수도를 묻는 질문을 하면 프랑스의 가장 큰 도시, 프랑스의 인구 등에 대한 질문으로 답변을 할 수도 있다.
Instruction Tuned LLM
- Base LLM에 지침을 따르도록 추가 훈련(RLHF 등)된 모델
- Base LLM에 비해 유용하고 안전한 답변을 하도록 훈련되었기 때문에 프랑스의 수도를 물어보면 파리라고 답한다.
helper function
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"]
2. Guidelines for Prompting
Prompting Principles
1. Write clear and specific instructions - 지시문을 명확하고(길고 자세하게) 구체적으로 작성하기
- 전략 1. 구분 기호(```, """, < >, <tag> </tag>, : 등)를 사용하여 입력의 뚜렷한 부분을 명확하게 표시한다.
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)
Clear and specific instructions should be provided to guide a model towards the desired output,
and longer prompts can provide more clarity and context for the model,
leading to more detailed and relevant outputs.
- 전략 2. 정형화된(Structured) 출력 구조를 명시한다.
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)
[
{
"book_id": 1,
"title": "The Lost City of Zorath",
"author": "Aria Blackwood",
"genre": "Fantasy"
},
{
"book_id": 2,
"title": "The Last Survivors",
"author": "Ethan Stone",
"genre": "Science Fiction"
},
{
"book_id": 3,
"title": "The Secret Life of Bees",
"author": "Lila Rose",
"genre": "Romance"
}
]
- 전략 3. 모델에게 요청한 조건이 충족되는지 확인하도록 지시한다.
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)
Completion for Text 1:
Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Once the water is hot enough, pour it over the tea bag.
Step 4 - Let it sit for a bit so the tea can steep.
Step 5 - After a few minutes, take out the tea bag.
Step 6 - Add some sugar or milk to taste.
Step 7 - Enjoy your delicious cup of tea!
text_2 = f"""
The sun is shining brightly today, and the birds are \
singing. It's a beautiful day to go for a \
walk in the park. The flowers are blooming, and the \
trees are swaying gently in the breeze. People \
are out and about, enjoying the lovely weather. \
Some are having picnics, while others are playing \
games or simply relaxing on the grass. It's a \
perfect day to spend time outdoors and appreciate the \
beauty of nature.
"""
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_2}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 2:")
print(response)
Completion for Text 2:
No steps provided.
If 문을 적절하게 수행하는 것이 신기하다.
- 전략 4. 원하는 결과와 비슷한 예시를 알려주면서 지시한다. (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)
<grandparent>: Resilience is like a tree that bends with the wind but never breaks.
It's the ability to bounce back from adversity and keep moving forward,
even when things get tough. Just like a tree needs strong roots to withstand the storm,
we need to cultivate inner strength and perseverance to overcome life's challenges.
2. Give the model time to "think" - 모델에게 생각할 시간 제공하기
- 전략 1. 작업을 수행하기 위해 필요한 구체적인 단계를 지시한다.
- + 정형화된 출력 구조를 명시한다.
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}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
Completion for prompt 1:
Two siblings, Jack and Jill, go on a quest to fetch water from a well on a hilltop,
but misfortune strikes and they both tumble down the hill,
returning home slightly battered but with their adventurous spirits undimmed.
Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits sur une colline,
mais un malheur frappe et ils tombent tous les deux de la colline,
rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.
Noms: Jack, Jill.
{
"french_summary": "Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits
sur une colline, mais un malheur frappe et ils tombent tous les deux de la colline,
rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.",
"num_names": 2
}
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_2)
print("\nCompletion for prompt 2:")
print(response)
Completion for prompt 2:
Summary: Jack and Jill go on a quest to fetch water, but misfortune strikes
and they tumble down the hill, returning home slightly battered but with their
adventurous spirits undimmed.
Translation: Jack et Jill partent en quête d'eau, mais la malchance frappe et
ils dégringolent la colline, rentrant chez eux légèrement meurtris mais avec
leurs esprits aventureux intacts.
Names: Jack, Jill
Output JSON: {"french_summary": "Jack et Jill partent en quête d'eau, mais la
malchance frappe et ils dégringolent la colline, rentrant chez eux légèrement
meurtris mais avec leurs esprits aventureux intacts.", "num_names": 2}
- 전략 2. 모델에게 결론을 내리기 전에 자체 솔루션을 해결하도록 지시한다.
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)
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 + 10x
Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000
Is the student's solution the same as actual solution just calculated:
No
Student grade:
Incorrect
모델의 한계 : Hallucinations
- 사실이 아닌 내용을 사실인 것처럼 그럴듯하게 설명하는 문제
- Hallucinations을 줄이기 위해 모델에게 먼저 텍스트에 관련된 인용문을 찾고 그 인용문을 질문에 답하기 위해 사용하도록 요청하는 것이 좋다.
Boie는 실제 회사이고 The AeroGlide UltraSlim Smart Toothbrush는 존재하지 않는 제품이지만
존재하지 않는 제품에 대해서 그럴듯하게 설명해주고 있다.
prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)
The AeroGlide UltraSlim Smart Toothbrush by Boie is a high-tech toothbrush that
uses advanced sonic technology to provide a deep and thorough clean.
It features a slim and sleek design that makes it easy to hold and maneuver,
and it comes with a range of smart features that help you optimize your brushing routine.
One of the key features of the AeroGlide UltraSlim Smart Toothbrush is
its advanced sonic technology, which uses high-frequency vibrations to break up plaque
and bacteria on your teeth and gums. This technology is highly effective at removing
even the toughest stains and buildup, leaving your teeth feeling clean and refreshed.
In addition to its sonic technology, the AeroGlide UltraSlim Smart Toothbrush
also comes with a range of smart features that help you optimize your brushing routine.
These include a built-in timer that ensures you brush for the recommended two minutes,
as well as a pressure sensor that alerts you if you're brushing too hard.
Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie is a highly advanced
and effective toothbrush that is perfect for anyone looking to take their oral hygiene
to the next level. With its advanced sonic technology and smart features,
it provides a deep and thorough clean that leaves your teeth feeling fresh and healthy.
3. Iterative Prompt Development
모델을 반복적으로 훈련하듯이 prompt를 더 좋게 만드는 과정이 필요하다.
Prompt guidelines
1. 명확하고 구체적으로 작성
2. 원하던 결과가 나오지 않은 이유를 분석
3. 분석 결과를 바탕으로 prompt를 정제하여 다시 작성
4. 위 과정 반복
예시 prompt
fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office furniture,
including filing cabinets, desks, bookcases, meeting tables, and more.
- Several options of shell color and base finishes.
- Available with plastic back and front upholstery (SWC-100)
or full upholstery (SWC-110) in 10 fabric and 6 leather options.
- Base finish options are: stainless steel, matte black,
gloss white, or chrome.
- Chair is available with or without armrests.
- Suitable for home or business settings.
- Qualified for contract use.
CONSTRUCTION
- 5-wheel plastic coated aluminum base.
- Pneumatic chair adjust for easy raise/lower action.
DIMENSIONS
- WIDTH 53 CM | 20.87”
- DEPTH 51 CM | 20.08”
- HEIGHT 80 CM | 31.50”
- SEAT HEIGHT 44 CM | 17.32”
- SEAT DEPTH 41 CM | 16.14”
OPTIONS
- Soft or hard-floor caster options.
- Two choices of seat foam densities:
medium (1.8 lb/ft3) or high (2.8 lb/ft3)
- Armless or 8 position PU armrests
MATERIALS
SHELL BASE GLIDER
- Cast Aluminum with modified nylon PA6/PA66 coating.
- Shell thickness: 10 mm.
SEAT
- HD36 foam
COUNTRY OF ORIGIN
- Italy
"""
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
Introducing our stunning mid-century inspired office chair, the perfect addition
to any home or business setting. Part of a beautiful family of office furniture,
including filing cabinets, desks, bookcases, meeting tables, and more, this chair
is available in several options of shell color and base finishes to suit your style.
Choose from plastic back and front upholstery (SWC-100) or full upholstery (SWC-110)
in 10 fabric and 6 leather options.
The chair is constructed with a 5-wheel plastic coated aluminum base and features
a pneumatic chair adjust for easy raise/lower action. It is available with or without
armrests and is qualified for contract use. The base finish options are stainless steel,
matte black, gloss white, or chrome.
Measuring at a width of 53 cm, depth of 51 cm, and height of 80 cm, with a seat height
of 44 cm and seat depth of 41 cm, this chair is designed for ultimate comfort.
You can also choose between soft or hard-floor caster options and two choices of seat
foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3). The armrests are available
in either an armless or 8 position PU option.
The materials used in the construction of this chair are of the highest quality.
The shell base glider is made of cast aluminum with modified nylon PA6/PA66 coating
and has a shell thickness of 10 mm. The seat is made of HD36 foam, ensuring maximum
comfort and durability.
This chair is made in Italy and is the perfect combination of style and functionality.
Upgrade your workspace with our mid-century inspired office chair today!
Issue 1. 너무 긴 텍스트
fact_sheet_chair 제품에 관한 설명을 해달라고 했을 때 너무 긴 텍스트가 출력된다면 'Use at most 50 words' 와 같은 요청을 추가하면 출력을 제한할 수 있다.
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
Use at most 50 words.
Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, part of a beautiful furniture family.
Available in various shell colors and base finishes, with plastic or full upholstery
options in fabric or leather. Suitable for home or business use, with a 5-wheel base
and pneumatic chair adjust. Made in Italy.
len(response) : 301
Issue 2. 의도와 다른 출력
결과물의 내용이 의도와 다르다면
'The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from.
At the end of the description, include every 7-character Product ID in the technical specification.' 와 같은 더 자세하고 구체적인 요청을 통해 원하는 의도에 가까운 결과물을 얻을 수 있다.
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.
At the end of the description, include every 7-character
Product ID in the technical specification.
Use at most 50 words.
Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
Introducing our mid-century inspired office chair, perfect for home or business settings.
With a range of shell colors and base finishes, and the option of plastic or full upholstery,
this chair is both stylish and comfortable. Constructed with a 5-wheel plastic coated aluminum
base and pneumatic chair adjust, it's also practical. Available with or without armrests and
suitable for contract use. Product ID: SWC-100, SWC-110.
Issue 3. 표를 결과물로 얻고 싶을 때
정보를 추출하여 표에 정리하도록 요청하는 prompt를 작성할 수 있다.
다음 예시에서는 HTML을 통해 웹페이지에서 표를 확인할 수 있도록 결과물을 얻는다.
결과물이 길다면 Issue 1.과 마찬가지로 'Use at most 50 words' 과 같은 요청을 추가하여 제한할 수 있다.
prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.
Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.
The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.
At the end of the description, include every 7-character
Product ID in the technical specification.
After the description, include a table that gives the
product's dimensions. The table should have two columns.
In the first column include the name of the dimension.
In the second column include the measurements in inches only.
Give the table the title 'Product Dimensions'.
Format everything as HTML that can be used in a website.
Place the description in a <div> element.
Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
<div>
<h2>Mid-Century Inspired Office Chair</h2>
<p>Introducing our mid-century inspired office chair, part of a beautiful family of office furniture that includes filing cabinets, desks, bookcases, meeting tables, and more. This chair is available in several options of shell color and base finishes, allowing you to customize it to your liking. You can choose between plastic back and front upholstery or full upholstery in 10 fabric and 6 leather options. The base finish options are stainless steel, matte black, gloss white, or chrome. The chair is also available with or without armrests, making it suitable for both home and business settings. Plus, it's qualified for contract use, ensuring its durability and longevity.</p>
<p>The chair's construction features a 5-wheel plastic coated aluminum base and a pneumatic chair adjust for easy raise/lower action. You can also choose between soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3). The armrests are also customizable, with the option of armless or 8 position PU armrests.</p>
<p>The chair's shell base glider is made of cast aluminum with modified nylon PA6/PA66 coating, with a shell thickness of 10 mm. The seat is made of HD36 foam, ensuring comfort and support during long work hours. This chair is made in Italy, ensuring its quality and craftsmanship.</p>
<h3>Product ID(s): SWC-100, SWC-110</h3>
<table>
<caption>Product Dimensions</caption>
<tr>
<th>Width</th>
<td>53 cm | 20.87"</td>
</tr>
<tr>
<th>Depth</th>
<td>51 cm | 20.08"</td>
</tr>
<tr>
<th>Height</th>
<td>80 cm | 31.50"</td>
</tr>
<tr>
<th>Seat Height</th>
<td>44 cm | 17.32"</td>
</tr>
<tr>
<th>Seat Depth</th>
<td>41 cm | 16.14"</td>
</tr>
</table>
</div>
4. Summarizing
특정 주제에 대해 중점을 두고 텍스트 요약하기
요약할 내용 예시
prod_review = """
Got this panda plush toy for my daughter's birthday, \
who loves it and takes it everywhere. It's soft and \
super cute, and its face has a friendly look. It's \
a bit small for what I paid though. I think there \
might be other options that are bigger for the \
same price. It arrived a day earlier than expected, \
so I got to play with it myself before I gave it \
to her.
"""
제한된 단어/문장/글자 수와 함께 요약하기
Your task is to generate a short summary of a product review from an ecommerce site.
Summarize the review below, delimited by triple backtricks, in at most 30words.
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site.
Summarize the review below, delimited by triple
backticks, in at most 30 words.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
Soft and cute panda plush toy loved by daughter, but a bit small for the price.
Arrived early.
배송에 중점을 두고 요약하기
Your task is to generate a short summary of a product review from an ecommerce site to give feedback to the Shipping deparmtment.
Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects that mention shipping and delivery of the product.
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
Shipping deparmtment.
Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that mention shipping and delivery of the product.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
The panda plush toy arrived a day earlier than expected, but the customer felt it was a
bit small for the price paid.
가격과 품질에 중점을 두고 요약하기
Your task is to generate a short summary of a product review from an ecommerce site to give feedback to the
pricing deparmtment, responsible for determining the price of the product.
Summarize the review below, delimited by triple backticks, in at most 30 words, and focusing on any aspects that are relevant to the price and perceived value.
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
pricing deparmtment, responsible for determining the \
price of the product.
Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
The panda plush toy is soft, cute, and loved by the recipient, but the price may be too high
for its size.
중점을 두려는 주제와 관계없는 주제가 요약에 포함된다면
'요약' 대신 '추출' 을 시도해 보자
Your task is to extract relevant information from a product review from an ecommerce site to give feedback to the Shipping department.
From the review below, delimited by triple quotes extract the information relevant to shipping and delivery. Limit to 30 words.
prompt = f"""
Your task is to extract relevant information from \
a product review from an ecommerce site to give \
feedback to the Shipping department.
From the review below, delimited by triple quotes \
extract the information relevant to shipping and \
delivery. Limit to 30 words.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
The product arrived a day earlier than expected.
5. Inferring
기존에는 긍부정 감정 분류를 위해 기계 학습 워크플로우에서 데이터셋을 수집하고 모델을 훈련시키고 모델을 구축하여 분류했어야 하지만 LLM을 통해 이러한 작업들에 대해 prompt를 작성하면 바로 결과를 생성할 수 있다.
제품 리뷰와 뉴스 기사에서 감정과 주제를 추론하기
제품 리뷰 예시
lamp_review = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast. The string to our lamp broke during the \
transit and the company happily sent over a new one. \
Came within a few days as well. It was easy to put \
together. I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""
리뷰에 대한 긍/부정 추론하기
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
Give your answer as a single word, either "positive" \
or "negative".
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print("결과 : ", response)
결과 : positive
리뷰에 대한 감정 종류 식별하기
prompt = f"""
Identify a list of emotions that the writer of the \
following review is expressing. Include no more than \
five items in the list. Format your answer as a list of \
lower-case words separated by commas.
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print("결과 : ", response)
결과 : happy, satisfied, grateful, impressed, content
리뷰에 분노가 포함되어 있는지 추론하기
prompt = f"""
Is the writer of the following review expressing anger?\
The review is delimited with triple backticks. \
Give your answer as either yes or no.
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print("결과 : ", response)
결과 : No
리뷰를 통해 제품과 회사의 이름을 추출하기
prompt = f"""
Identify the following items from the review text:
- Item purchased by reviewer
- Company that made the item
The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
{
"Item": "lamp",
"Brand": "Lumina"
}
다양한 작업들을 한 번에 수행하기
prompt = f"""
Identify the following items from the review text:
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item
The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
{
"Sentiment": "positive",
"Anger": false,
"Item": "lamp with additional storage",
"Brand": "Lumina"
}
뉴스 기사 예시
story = """
In a recent survey conducted by the government,
public sector employees were asked to rate their level
of satisfaction with the department they work at.
The results revealed that NASA was the most popular
department with a satisfaction rating of 95%.
One NASA employee, John Smith, commented on the findings,
stating, "I'm not surprised that NASA came out on top.
It's a great place to work with amazing people and
incredible opportunities. I'm proud to be a part of
such an innovative organization."
The results were also welcomed by NASA's management team,
with Director Tom Johnson stating, "We are thrilled to
hear that our employees are satisfied with their work at NASA.
We have a talented and dedicated team who work tirelessly
to achieve our goals, and it's fantastic to see that their
hard work is paying off."
The survey also revealed that the
Social Security Administration had the lowest satisfaction
rating, with only 45% of employees indicating they were
satisfied with their job. The government has pledged to
address the concerns raised by employees in the survey and
work towards improving job satisfaction across all departments.
"""
5개 주제 추론하기
prompt = f"""
Determine five topics that are being discussed in the \
following text, which is delimited by triple backticks.
Make each item one or two words long.
Format your response as a list of items separated by commas.
Text sample: '''{story}'''
"""
response = get_completion(prompt)
print("결과 : ", response)
결과 : government survey, job satisfaction, NASA, Social Security Administration, employee concerns
특정 항목에 대한 뉴스 알림 만들기
topic_list = [
"nasa", "local government", "engineering",
"employee satisfaction", "federal government"
]
prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which
is delimited with triple backticks.
Give your answer as list with 0 or 1 for each topic.\
List of topics: {", ".join(topic_list)}
Text sample: '''{story}'''
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
nasa: 1
local government: 0
engineering: 0
employee satisfaction: 1
federal government: 1
topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')}
if topic_dict['nasa'] == 1:
print("ALERT: New NASA story!")
ALERT: New NASA story!
6. Transforming
번역, 철자 및 문법 검사, 톤 조절, 형식 변환 등과 같은 텍스트 변환 작업에 LLM을 사용할 수 있다.
번역
prompt = f"""
Translate the following English text to Spanish: \
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print("결과 : ", response)
결과 : Hola, me gustaría ordenar una licuadora.
###############################################################################################
prompt = f"""
Tell me which language this is:
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print("결과 : ", response)
결과 : This is French.
###############################################################################################
prompt = f"""
Translate the following text to French and Spanish
and English pirate: \
```I want to order a basketball```
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
French pirate: ```Je veux commander un ballon de basket```
Spanish pirate: ```Quiero pedir una pelota de baloncesto```
English pirate: ```I want to order a basketball``
###############################################################################################
prompt = f"""
Translate the following text to Spanish in both the \
formal and informal forms:
'Would you like to order a pillow?'
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
Formal: ¿Le gustaría ordenar una almohada?
Informal: ¿Te gustaría ordenar una almohada?
범용 번역기
user_messages = [
"La performance du système est plus lente que d'habitude.", # System performance is slower than normal
"Mi monitor tiene píxeles que no se iluminan.", # My monitor has pixels that are not lighting
"Il mio mouse non funziona", # My mouse is not working
"Mój klawisz Ctrl jest zepsuty", # My keyboard has a broken control key
"我的屏幕在闪烁" # My screen is flashing
]
for issue in user_messages:
prompt = f"Tell me what language this is: ```{issue}```"
lang = get_completion(prompt)
print(f"Original message ({lang}): {issue}")
prompt = f"""
Translate the following text to English \
and Korean: ```{issue}```
"""
response = get_completion(prompt)
print(response, "\n")
Original message (This is French.): La performance du système est plus lente que d'habitude.
English: The system performance is slower than usual.
Korean: 시스템 성능이 평소보다 느립니다.
Original message (This is Spanish.): Mi monitor tiene píxeles que no se iluminan.
English: My monitor has pixels that don't light up.
Korean: 내 모니터에는 불이 켜지지 않는 픽셀이 있습니다.
Original message (This is Italian.): Il mio mouse non funziona
English: My mouse is not working.
Korean: 내 마우스가 작동하지 않습니다.
Original message (This is Polish.): Mój klawisz Ctrl jest zepsuty
English: My Ctrl key is broken.
Korean: 제 Ctrl 키가 고장 났어요.
Original message (This is Chinese (Simplified).): 我的屏幕在闪烁
English: My screen is flickering.
Korean: 내 화면이 깜빡입니다.
톤 바꾸기
비격식 톤(slang)을 격식(business) 톤으로 바꾸기
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
Dear Sir/Madam,
I am writing to bring to your attention a standing lamp that I believe may be of interest to you. Please find attached the specifications for your review.
Thank you for your time and consideration.
Sincerely,
Joe
형식 바꾸기
JSON을 HTML 형식으로 바꿀 수 있다.
data_json = { "resturant employees" :[
{"name":"Shyam", "email":"shyamjaiswal@gmail.com"},
{"name":"Bob", "email":"bob32@gmail.com"},
{"name":"Jai", "email":"jai87@gmail.com"}
]}
prompt = f"""
Translate the following python dictionary from JSON to an HTML \
table with column headers and title: {data_json}
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
결과 :
<table>
<caption>Restaurant Employees</caption>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>Shyam</td>
<td>shyamjaiswal@gmail.com</td>
</tr>
<tr>
<td>Bob</td>
<td>bob32@gmail.com</td>
</tr>
<tr>
<td>Jai</td>
<td>jai87@gmail.com</td>
</tr>
</tbody>
</table>
from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
맞춤법 검사하기
'proofread', 'proofread and correct'와 같은 요청을 통해 텍스트를 교정할 수 있다.
(redlines 라이브러리를 통해 두 텍스트의 차이를 시각화로 비교할 수 있다.)
text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room. Yes, adults also like pandas too. She takes \
it everywhere with her, and it's super soft and cute. One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price. It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"proofread and correct this review: ```{text}```"
response = get_completion(prompt)
print(response)
from redlines import Redlines
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
7. Expanding
자동 이메일 응답 생성하기
응답 생성 함수와 리뷰 예시
# Andrew mentioned that the prompt/ completion paradigm is preferable for this class
def get_completion(prompt, model="gpt-3.5-turbo",temperature=0):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
# given the sentiment from the lesson on "inferring",
# and the original customer message, customize the email
sentiment = "negative"
# review for a blender
review = f"""
So, they still had the 17 piece system on seasonal \
sale for around $49 in the month of November, about \
half off, but for some reason (call it price gouging) \
around the second week of December the prices all went \
up to about anywhere from between $70-$89 for the same \
system. And the 11 piece system went up around $10 or \
so in price also from the earlier sale price of $29. \
So it looks okay, but if you look at the base, the part \
where the blade locks into place doesn’t look as good \
as in previous editions from a few years ago, but I \
plan to be very gentle with it (example, I crush \
very hard items like beans, ice, rice, etc. in the \
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""
prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt)
print("결과 : ")
print(response)
"""
결과 :
Dear valued customer,
Thank you for taking the time to leave a review about our product.
We are sorry to hear that you experienced a price increase and that the quality of the product
did not meet your expectations. We apologize for any inconvenience this may have caused you.
If you have any further concerns or questions, please do not hesitate to reach out to
our customer service team. They will be more than happy to assist you in any way they can.
Thank you again for your feedback. We appreciate your business and hope to have
the opportunity to serve you better in the future.
Best regards,
AI customer agent
"""
Temperature
temperature 조절을 통해 다양한 생성 결과를 얻을 수 있다.
prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""
response = get_completion(prompt, temperature=0.7)
print("결과 : ")
print(response)
"""
결과 :
Dear valued customer,
Thank you for taking the time to leave a review of our product.
We are sorry to hear that you experienced a price increase and that the base of
the system did not meet your expectations. We apologize for any inconvenience this
may have caused.
If you have any further concerns, please do not hesitate to reach out to our customer service team.
They are available to assist you and can be reached by phone or email.
Thank you again for your feedback and for choosing our product.
We appreciate your loyalty and hope to have the opportunity to serve you better in the future.
Best regards,
AI customer agent
"""
8. Chatbot
채팅 형식을 활용하여 특정 작업이나 행동에 맞게 개인화 또는 전문화된 챗봇과 대화하도록 만들 수 있다.
Setup
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"]
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]
messages = [
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},
{'role':'user', 'content':'tell me a joke'},
{'role':'assistant', 'content':'Why did the chicken cross the road'},
{'role':'user', 'content':'I don\'t know'}
response = get_completion_from_messages(messages, temperature=1)
print("결과 : ", response)
"""
결과 : To get to the other side, of course!
"""
messages = [
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'} ]
response = get_completion_from_messages(messages, temperature=1)
print("결과 : ", response)
"""
결과 : Hi Isa, it's nice to meet you! How can I assist you today?
"""
messages = [
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Yes, can you remind me, What is my name?'} ]
response = get_completion_from_messages(messages, temperature=1)
print("결과 : ", response)
"""
결과 : I'm sorry, but as an AI language model, I don't have the capability to recall
your name from previous conversations as all the interactions are independent of each other.
However, you can tell me your name, and I'll be happy to address you by it.
How may I assist you today?
"""
messages = [
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \
Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'} ]
response = get_completion_from_messages(messages, temperature=1)
print("결과 : ", response)
"""
결과 : Your name is Isa.
"""
- 'role':' ~~'과 같은 형식을 통해 역할을 지정할 수 있다.
- 역할을 system으로 지정하고 content로 모델의 행동, 역할(페르소나)을 지정한다.
- 역할을 user로 지정하고 content에 채팅 기록을 남긴다.
- 역할을 assistant로 지정하고 content에 모델이 답변한 기록을 남긴다.
- 이러한 구조를 통해 멀티턴 방식의 챗봇을 구현할 수 있다.
챗봇 만드려고 데이터 수집부터 모델링까지 열심히 했었는데 좋은 경험이었지만 이렇게 챗봇을 손쉽게 만드니 약간 허무하다...
9. Conclusion
Summary
Prompting Principles
1. Write clear and specific instructions - 지시문을 명확하고(길고 자세하게) 구체적으로 작성하기
2. Give the model time to "think" - 모델에게 생각할 시간 제공하기
반복적으로 prompt 개선하기
가능한 작업 : Summarizing, Inferring, Transforming, Expanding, Building a Chatbot
https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/
ChatGPT Prompt Engineering for Developers
What you’ll learn in this course In ChatGPT Prompt Engineering for Developers, you will learn how to use a large language model (LLM) to quickly build new and powerful applications. Using the OpenAI API, you’ll...
www.deeplearning.ai
'AI' 카테고리의 다른 글
모델 경량화 2강 실습 - 작은 모델, 좋은 파라미터 찾기 (0) | 2023.05.25 |
---|---|
ChatGPT : 새로운 자연어 처리기술의 발전과 가능성 (0) | 2023.05.22 |
F Beta-Score? (0) | 2023.05.14 |
모델 경량화 2강 이론 - 작은 모델, 좋은 파라미터 찾기 (1) | 2023.05.02 |
모델 경량화 1강 (0) | 2023.05.01 |