ChatGPT提示词工程(六):Expanding扩展

一、说明

这是吴恩达 《ChatGPT Prompt Engineering for Developers》 的课程笔记系列。
本文是第七讲的内容:Expanding

二、安装环境

参考: ChatGPT提示词工程(一):Guidelines准则 的第二节
不过本节课中的辅助函数不一样了,增加了一个参数 temperature

def get_completion(prompt, model="gpt-3.5-turbo", temperature=0): # Andrew mentioned that the prompt/ completion paradigm is preferable for this class
    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"]

三、扩展(Expanding)

1. 自定义自动回复客户电子邮件

# 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(response)

客户邮件内容review翻译成中文为:

所以,在11月份,他们仍然有17件制的季节性销售,售价约为49美元,大约减半,但由于某种原因(称之为价格欺诈),在12月的第二周左右,同一种制的价格都上涨到了大约70美元到89美元之间。11件套的售价也比早先29美元的售价上涨了10美元左右。所以看起来还可以,但如果你看看底座,刀片锁定的部分看起来不像几年前的前几版那么好,但我计划对它非常温和(例如,我先在搅拌器里粉碎非常硬的东西,比如豆子、冰、大米等,然后在搅拌器里把它们粉碎成我想要的服务大小,然后切换到搅拌刀片,以获得更好的面粉,在做奶昔时首先使用横切刀片,如果我需要它们更细/更少浆状的话,再使用扁平刀片)。做冰沙时,要特别注意切开和冷冻水果和蔬菜(如果用菠菜–轻轻炖菠菜,让菠菜变软,然后冷冻,直到可以使用–如果做冰糕,用中小型食品加工机),这样你就可以避免在做冰沙时添加太多的冰。大约一年后,马达发出了奇怪的声音。我给客服打了电话,但保修期已经过了,所以我不得不再买一台。仅供参考:这些类型的产品已经实现了整体质量,所以他们在某种程度上依赖于品牌认知度和消费者忠诚度来维持销售。大约两天后就拿到了。

prompt
你是一名客服人工智能助理。
你的任务是向重要客户发送电子邮件回复。
鉴于客户电子邮件由```分隔,
生成回复以感谢客户的评价。
如果情绪是积极的或中性的,感谢他们的评价。
如果情绪是负面的,道歉,并建议他们可以联系到客户服务。
确保使用评价中的具体细节。
用简洁和专业的语气写作。
用“AI customer agent”签名

运行结果:
在这里插入图片描述

上述代码中,sentiment = “negative” 是写死的, 我们可以运用上一课所学(Inferring),让它自己判断客户评价的情绪

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.
What is the sentiment of their review, 
which is delimited with triple backticks?
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}```
"""
response = get_completion(prompt)
print(response)

prompt:添加了一句:
What is the sentiment of their review,
which is delimited with triple backticks?

运行结果:
在这里插入图片描述

2. 提醒模型使用客户电子邮件中的详细信息

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)

prompt:添加了一句 Make sure to use specific details from the review.
get_completion参数temperature允许你改变模型的探索和多样性的程度
运行结果:
在这里插入图片描述

3. 参数 temperature

在这里插入图片描述
参数temperature能使我们改变模型响应的多样性。
比如,我最喜欢的食物,模型预测pizza可能性为53%,sushi的可能性为30%,tacos的可能性为5%
如果temperature = 0 则模型总是给出pizza
如果 temperature > 0 则模型给出的结果可能就会是 sushi 或者 tacos

https://blog.csdn.net/Jay_Xio/article/details/130460267



猜你喜欢

转载自blog.csdn.net/Jay_Xio/article/details/130460267