🧑🏫
DeepLearning.AIとOpenAIが公開したChatGPT Prompt Engineering(5-変換、拡大)
はじめに
ChatGPT Prompt Engineeringを受講したメモです。
全体の目次はこちらをご覧ください。
今回は、変換(Transforming)と拡大(Expanding)です。
翻訳、口調の変換、フォーマットの変換が紹介されています。
翻訳などはもちろんですが、フォーマットの変換も便利ですね。
Redlinesの差分表示は知らなかったので勉強になりました。
拡大は、temperatureをあげて、バリエーションをだす方法です。
翻訳
スペイン語に翻訳
prompt = f"""
Translate the following English text to Spanish: \
```Hi, I would like to order a blender```
"""
言語を推測
prompt = f"""
Tell me which language this is:
```Combien coûte le lampadaire?```
"""
複数言語出力
prompt = f"""
Translate the following text to French and Spanish
and 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?'
"""
複数の言語をまとめて翻訳
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")
口調の変換
スラングからフォーマルへの変換です。
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
フォーマットの変換
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}
"""
出力
<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>
HTML表示
from IPython.display import display, HTML
display(HTML(response))
校正と修正
校正と修正は以下のプロンプトで行えます。
prompt = f"proofread and correct this review: ```{text}```"
条件わけ
prompt = f"""Proofread and correct the following text
and rewrite the corrected version. If you don't find
and errors, just say "No errors found". Don't use
any punctuation around the text:
```{t}```"""
修正との差分を表示
Redlinesを使って、修正した文章との差分を表示しています。
from IPython.display import display, Markdown
from redlines import Redlines
diff = Redlines(text,response)
display(Markdown(diff.output_markdown))
拡大
0だと決まった返答になり、1に近づくほど、バリエーションがでます。
下のE-mailの返信例は、temperatureが0.7なので、毎回違うメッセージを返しやすくなっています。
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(response)
Discussion