https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/
ChatGPT Prompt Engineering for Developers
4 - Inferring ์ ๋ฆฌ
Setup
import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')
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"]
Inferring - ์ถ๋ก ํ๊ธฐ
1) Sentiment ๊ฐ์ ๋ถ์
๋ก์งํ ์ MX-KEYS ํค๋ณด๋ ๋ฆฌ๋ทฐ์ ๋ํ ์์ฑ์์ ๊ฐ์ ์ ๋ถ์ํด๋ณด์.
Prompt Task ์์ฑ
1) What is the sentiment of the following product review,
2) Which is delimited with triple backticks?
3) Translate in korean.
๊ฒฐ๊ณผ ํฌ๋ฉง
Review text: ```{review}```
review_1 = """
๋น๊ต์ ๋ด๊ตฌ์ฑ์ด ๋ฎ์ ํํ๊ทธ๋ํ๋ฅผ ์ ์ ํ๋ค๊ฐ ์ด ์ ํ์ ์ ์ฐฉํ๋ค๋ ํ๊ธฐ๊ฐ ๋ง์ ๋ก์งํ
์ MX Keys์
๋๋ค.
ํน์ ์ ์กฐ์ฉ์กฐ์ฉํ ํ๊ฒฉ๊ฐ๊ณผ ๋ฉํ ๋ฐ๋์ ๋ธ๋, ๊ทธ๋ ์ด๊ฐ ์์ธ ๊ณ ๊ธ์ง ๋์์ธ์ผ๋ก ์ฌ๋ฌด์ค ์ฑ
์์์ ์กด์ฌ๊ฐ์ ๋๋ฌ๋
๋๋ค. ์ค๋ชฉํ ํค์บก ๋์์ธ์ผ๋ก ํค ๊ตฌ๋ถ๊ฐ์ด ์ข์ ์คํ๊ฐ ์ค์ด๋ค์๋ค๋ ์๊ฒฌ๋ ๋ง์ ์ ํ์
๋๋ค.
๋ธ๋ฃจํฌ์ค๋ก ๋ฉํฐํ์ด๋ง์ด ๊ฐ๋ฅํ๋ฉฐ, ์ฐ๊ฒฐ๋ OS๋ฅผ ์ธ์ํ์ฌ ์๋์ผ๋ก ๊ธฐ๋ฅํค ๋ฐ ๋จ์ถํค๋ฅผ ์ธ์ํ๊ธฐ ๋๋ฌธ์ ๊ธฐ๊ธฐ๋ฅผ ๋ฐ๊ฟ ๋๋ง๋ค ๋ฐ๋ก ์ค์ ํ์ง ์์๋ ๋์ด ํธ๋ฆฌํฉ๋๋ค.
๊ฐ๊ฒฉ์ ๋ค์ ๋น์ธ์ง๋ง ์ ์ํ ํค๊ฐ์ ๊ณ ๊ธ์ค๋ฌ์ด ์ฌ๋ฌด์ค์ฉ ํค๋ณด๋๋ฅผ ์ฐพ์ผ์๋ ๋ถ๊ป ์ถ์ฒ๋๋ฆฝ๋๋ค.
๋ค๋ง, ์คํ์ด์ค๋ฐ ๊ธธ์ด๊ฐ ๊ธธ์ด ์ ์ํ๊ธฐ ํ๋ค๋ค๋ ์๊ฒฌ๋ ์์ผ๋ฉฐ, ๋ก์งํ
์ ์ต์ ์ฐ๊ฒฐ๋ฐฉ์์ธ ๋ก์ง๋ณผํธ๋ฅผ ์ง์ํ์ง ์์, ๋ธ๋ฃจํฌ์ค๋ฅผ ์ง์ํ์ง ์๋ ๊ธฐ๊ธฐ์์ ๋ก์งํ
๋ง์ฐ์ค์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ USB ๋๊ธ์ 2๊ฐ ๊ฝ์์ผ ํ๋ค๋ ์ ์ด ์์ฝ์ต๋๋ค.
"""
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
translate in korean.
Review text: ```{review_1}```
"""
response = get_completion(prompt)
print(response)
+ label๋ก ์ฌ์ฉํ๊ธฐ ์ํด positive or negative ์ง์
Prompt Task ์์ฑ
1) What is the sentiment of the following product review,
2) Which is delimited with triple backticks?
3) Give your answer as a single word, either “positive” or ”negative”
4) Translate in korean.
๊ฒฐ๊ณผ ํฌ๋ฉง
Review text: ```{review}```
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: ```{review_1}```
"""
response = get_completion(prompt)
print(response)
2) Extract product and company name from customer reviews (ํ๋ชฉ ์ ๋ณด ์ถ์ถ)
Prompt Task ์์ฑ
1) Identify the following items from the review text:
- Item purchased by reviewer -- (ํ๋ชฉ ๋ช ์)
- Company that made the itemFormat your answer as a list of lower-case words (์ ์กฐ์ฌ ๋ช ์)
2) 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. (output format json)
3) Make your response as short as possible.
4) Translate in korean.
๊ฒฐ๊ณผ ํฌ๋ฉง
Review text: ```{review}```
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.
translate in korean.
Review text: '''{review_1}'''
"""
response = get_completion(prompt)
print(response)
##
3) Doing multiple tasks at once
Prompt Task ์์ฑ
1)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
2) 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.
3) Make your response as short as possible.
4) Format the Anger value as a boolean.
5) Translate in Koreans.
๊ฒฐ๊ณผ ํฌ๋ฉง
Review text: ```{review}```
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.translate in korean.
Review text: '''{review_1}'''
"""
response = get_completion(prompt)
print(response)
4) Inferring topics
'ํผ๋ ธํค์ค' ์ด์ผ๊ธฐ๋ฅผ ๋ณด๊ณ 5๊ฐ์ง ์ฃผ์ ๋ฅผ ๋ฝ์๋ด๋ณด์
Prompt Task ์์ฑ
1) Determine five topics that are being discussed in the following text, which is delimited by triple backticks.
2) Make each item one or two words long.
3) Format your response as a list of items separated by comas.
4) Translate in korean.
๊ฒฐ๊ณผ ํฌ๋ฉง
Text sample: ```{story}```
story = "ํผ๋
ธํค์ค๋ ํ๊ต๋ก ๊ฐ๋ ๊ธธ์ ์์ปค์ค๋จ๊ณผ ๋ง์ฃผ์ณค๊ณ , ์ ๋ฐ์ ๋์ ธ ์ ์ ์ณ์ ํ๊ต๋ก ๊ฐ์ง ์์ปค์ค๋ฅผ ๋ณด๋ฌ ๊ฐ์ง ๊ฒฐ์ ํ๋ค. ํ๊ต๋ก ๊ฐ๋ ์ ์ด ๋์์ง๋ง, ๋ช๋ฒ์ด๋ ๊ณ์ ์ ์ ์ณ ์์ปค์ค๋ก ๊ฐ๋ ์ ๊ด๊ฐ ๋์ค์ ๊ต๊ณผ์๋ฅผ ํ ๋์ผ๋ก ์์ปค์ค ๊ด๋๋ฃ๋ฅผ ๋ด๊ณ ์
์ฅํ๋ค. ์์ปค์ค๋จ์ ๋ง์นจ ์ธํ๊ทน์ ๊ณต์ฐํ๊ณ ์์๋ค. ๊ทธ๋ฐ๋ฐ ๋๋ง์นจ ๋ค์ด์จ ํผ๋
ธํค์ค๋ฅผ ๋ณด๊ณ , ๊ด๊ฐ๋ค์ ์ธํ๊ทน์ ๋ฌด์ํ์ฑ ํผ๋
ธํค์ค๋ฅผ ๋ฌด๋๋ก ๋ฐ๋ ค์ ๊ปด์์ผ๋ฉฐ ์ฆ๊ฑฐ์ํ๋ค. ๊ทธ ๋๋ถ์ ์ธํ๊ทน์ ์๋ง์ด ๋๊ณ ํ๊ฐ ๋จธ๋ฆฌ๋๊น์ง ๋ ์ธํ ๋์๊พผ์ ํผ๋
ธํค์ค๋ฅผ ์ก์์ ๊ธฐ๋ฅ์ ๋งค๋ฌ์ ๋ฒ๋ ธ๋ค. ์ธํ ๋์๊พผ์ ์ ๋
๋ฐฅ์ผ๋ก ๋จน์ ์๊ณ ๊ธฐ๋ฅผ ๊ตฝ๋ ํ๋์ ํผ๋
ธํค์ค๋ฅผ ์ฅ์์ผ๋ก ์ง์ด๋ฃ์ผ๋ ค ํ๋ค. ํผ๋
ธํค์ค๋ ์๋น๋ฅผ ๊ตฌํ์, ์ธํ ๋์๊พผ์ ํผ๋
ธํค์ค ๋์ ๋ค๋ฅธ ์ธํ์ ์ฅ์์ผ๋ก ์ฐ๊ฒ ๋ค๊ณ ๋งํ๋ค. ๊ทธ๋ฌ์ ํผ๋
ธํค์ค๋ ์์ ์ ๋์ ๋๋ผ๊ณ ๋์ฐ๊ณ , ์ธํ ๋์๊พผ์ ํผ๋
ธํค์ค์ ์ฉ๊ธฐ์ ๊ฐํํด ๋ ์ธํ์ ๋ชจ๋ ์ด๋ ค ์ฃผ๊ณ ๋ ํผ๋
ธํค์ค์๊ฒ ๊ธํ ๋ค์ฏ ๋ข์ ์ฃผ๊ณ ์ง์ผ๋ก ๋๋ ค๋ณด๋๋ค."
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. translate in korean.
Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)
response.split(sep=',')
+ ์ํ๋ ์ฃผ์ ์ ๊ธ์ธ์ง ํ๋ณํ๋ classification ์ถ๊ฐ
Make a story for certain topics
Prompt Task ์์ฑ
1) Determine whether each item in the following list of topics is a topic in the text below, which is delimited with triple backticks.
2) Give your answer as list with 0 or 1 for each topic
3) List of topics: {",".join(topic_list)}
4) Translate in Koreans.
๊ฒฐ๊ณผ ํฌ๋ฉง
Text sample: ```{story}```
topic_list = ['ํผ๋
ธํค์ค', ' ์์ปค์ค', ' ์ธํ๊ทน', ' ์๋น', ' ๊ธํ']
###
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(response)
topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')}
if topic_dict['ํผ๋
ธํค์ค'] == 1:
print("ํผ๋
ธํค์ค ์ด์ผ๊ธฐ!")
'๐ ๏ธ Tools > ๐ค ChatGPT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LLM] ChatGPT Prompt Engineering for Developers - Expanding (0) | 2023.05.06 |
---|---|
[LLM] ChatGPT Prompt Engineering for Developers - Transforming (0) | 2023.05.05 |
[LLM] ChatGPT Prompt Engineering for Developers - Summarizing (0) | 2023.05.04 |
[LLM] ChatGPT Prompt Engineering for Developers - Iterative (0) | 2023.05.03 |
[LLM] ChatGPT Prompt Engineering for Developers - Guidelines (0) | 2023.05.02 |