想和大家分享一下最近学习的Deeplearning.AI和openai联合打造ChatGPT Prompt Engineering在线课程.以下是我写的关于该课程的前四篇博客:
ChatGPT Prompt Engineering(一)
ChatGPT Prompt Engineering(二)
ChatGPT Prompt Engineering(三)
ChatGPT Prompt Engineering(四)
今天我们来学习第五部分内容:扩展(Expanding)。
今天我们让ChatGPT成为一个AI助理来帮助人类自主回复客户邮件。首先我们需要设置通过API来访问ChatGPT的主要代码:
import openai
openai.api_key ='YOUR_OPENAI_API_KEY'
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,
)
return response.choices[0].message["content"]
自定义对客户电子邮件的自动回复
这里我们会给出一段客户关于商品的负面评论,和一个事先定义好的sentiment等于negative的情绪变量, 然后我们要求ChatGPT根据这段负面评论和情绪变量来写一份客户邮件,以表达对客户的歉意。
#情绪变量
sentiment = "negative"
# 关于搅拌器的客户评论
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.
"""
#翻译版本
review1="""
因此,他们在11月份仍然以49美元左右的价格出售17套系统,大约是半价,
但由于某种原因(称之为价格欺诈),在12月的第二周左右,同一套系统的价格
都上涨到了70美元到89美元之间。11件套装的价格也从之前的29美元上涨了10美元左右。
所以看起来不错,但如果你看看基地,刀片锁到位的部分看起来不一样从几年前在以前的版本,
但我计划很温柔(示例中,我很努力粉碎物品,如豆类,冰,大米,等搅拌机第一然后粉碎他们的
用量我希望在搅拌机然后切换到细面粉搅拌叶片,并使用交叉刀片首先做冰沙时,然后如果
我需要更细/更少的浆糊,使用平刀片)。制作冰沙时的特别提示,将水果和蔬菜切成细碎
并冷冻(如果用菠菜,稍微炖一下,使菠菜变软,然后冷冻直到可以使用;如果做冰沙,
使用小型到中型的食品加工机),你打算用这种方法来避免在制作冰沙时添加太多的冰。
大约过了一年,发动机发出了一种奇怪的声音。我打电话给客服,但是保修期已经过了,
所以我不得不再买一个。仅供参考:这类产品的整体质量已经不行了,所以
他们依靠品牌认知度和消费者忠诚度来维持销售。大概两天就拿到了。
"""
从这段用户评论中我们不难发现客户是在抱怨商品存在质量问题。接下来我们要告诉ChatGPT它的角色是什么(这个很重要),因为只有当ChatGPT知道自己的角色是什么以后,它的回复才会显得更加专业和更有针对性。然后我们要求ChatGPT根据情绪变量的值来做相应的内容回复,如果情绪变量是正面的,则表达感谢,如果情绪变量是负面的则表达歉意,并且在回复客户时需要引用客户评论中的一些细节,以及需要使用简洁和专业的风格来回复客户,最后以“AI客服助理”作为签名。
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}
"""
#翻译版本
prompt1="""
你是一位客服AI助理。
您的任务是向尊贵的客户发送电子邮件回复。
给定由 ``` 分隔的客户电子邮件,\
生成回复以感谢客户的评论。
如果情绪是积极的或中立的,感谢他们的评论。
如果情绪是负面的,请道歉并建议他们联系客户服务。
确保使用评论中的具体细节。
以简洁和专业的语气写作。
将电子邮件签名为“AI 客户代理”。
客户评论:```{review}```
评论情绪:{sentiment}
"""
response = get_completion(prompt)
print(response)
下面是客服AI助理回复email的翻译版本:
这里我们告知ChatGPT它的角色是:一位客服AI助理,因此ChatGPT的回复将会带有客服AI助理的风格和语气。从回复的内容质量上来看,ChatGPT表现的还是非常的专业,完全按照我们对它的要求来做回复。
提醒模型使用客户电子邮件中的详细信息
我们在调用ChatGPT模型时会使用一个默认温度参数:temperature,该参数的默认值我们把它定义为0,0表示ChatGPT每次只从所有可能的结果中获取一个可能性最高的结果,或者是概率最高的结果。该参数的取值范围为0-1之间,为此我们可以调节temperature参数,temperature的值越大,则回复内容的随机性就越大,反之则随机性越小,因此如果我们想让ChatGPT回复有一定的随机性,可以将temperature值设置为0.5或者0.7,这里我们将temperature设置为0.7,看看ChatGPT的回复和之前有什么差异:
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)
下面我们将该程序执行多次,看看每次回答的差异:
从上面回复的内容上看,ChatGPT每次回复的内容侧重点都有所不同,但主要还是以表达歉意为主要内容,因此如果我们需要让ChatGPT每次回复的时候都有一定的多样性(随机性),那么可以调节温度参数temperature,调高temperature会让回复有更多的随机性。
尝试做一些改变
前面我们的定义了一个情绪变量sentiment=“negative”,那如果我们把sentiment定义为“positive”,结果会怎么样呢?
sentiment = "positive"
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)
下面是回复翻译版本:
与之前将sentiment定义为“negative”的结果相比,"positive"版的回复多了很多感谢的话语,但同时也提到了产品质量的问题并向客户表达了歉意,从大体上看是正面和反面的话语各占一般,这可能是因为评论本身就是负面的,但是我们又要求ChatGPT以正面的立场来做回复,因此实在是为难ChatGPT了,ChatGPT只能选择部分正面部分是反面的方式来做回复。
接下来我们让ChatGPT来自己识别评论的情绪,然后更加识别的结果来做出回复,这就需要我们修改一下prompt:
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.
Identify the sentiment of 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}```
"""
response = get_completion(prompt)
print(response)
下面是回复的翻译版本:
这里我们在prompt中告知ChatGPT要首先识别客户评论中的情绪“Identify the sentiment of their review”,然后根据识别出来的情绪做相应的回复。从ChatGPT回复的内容上看,主要是以表达歉意和道歉为主要内容,这说明ChatGPT已经识别出评论的情绪是负面的,所以回复的内容侧重点在表达歉意上面。
总结
今天我们学习了如果让ChatGPT来代替人类来回复客户邮件,根据客户邮件的内容做出恰当的回复,如果客户评论是正面的,则回复以表达感谢为主,如果客户评论是负面的,则回复以表达歉意为主。同时我们还学习了通过调整temperature参数来使得chatGPT的回复更具多样性(随机性)。
参考资料
Courses - DeepLearning.AI