吴恩达ChatGPT网课笔记Prompt Engineering——训练ChatGPT前请先训练自己

news2024/11/24 12:36:30

吴恩达ChatGPT网课笔记Prompt Engineering——训练ChatGPT前请先训练自己

主要是吴恩达的网课,还有部分github的prompt-engineering-for-developers项目,以及部分自己的经验。

一、常用使用技巧

  1. prompt最好是英文的,如果是中文的prompt,大概会是英文的1.5~2倍的token数量。一方面成本提升,另一方面对于较长的prompt可能会超过上下文上限。

原则1:编写简洁和具体的介绍

策略1:使用分隔符

  1. “”"

  2. ```

  3. - - -

  4. < >

  5. XML 标签:<tag> </tag>

下图是一个例子,黄色部分其实也是需要总结的内容,不过如果没用三个单引号的分隔符,可能chatGPThuiba 黄色部分当成是一个命令,而不是要总结的文本:

在这里插入图片描述

策略2:要求结构化输出

可以要求html、JSON这样的。在提问的时候可以说

请帮我生成一列编造的书籍名称,作者,书的id,请用JSON的格式提供他们,以下是他们应该包含的键:book_id,title,author,genre.

策略3:要求模型检查是否满足条件

如果任务存在假设没用满足,我们可以告诉模型首先检查这些假设,还可以考虑一些边界条件。

你将得到一个文本是用"""引用的格式给出的,如果他包含了一系列的解释,请重写为<结构化数据>,如果不包含指示,那么就打印:"没有提供步骤

少量训练提示

在要求模型执行任务之前,首先提供成功执行任务的示例。比如下图的例子,首先提供了一个child想知道耐心的问题,然后我们给出了grandparent应该回答的示范,因为我们要求回答需要一致性风格,所以当child问关于resilience的时候,chatGPT也应该给出相似的答案。

在这里插入图片描述

原则2:给模型思考的时间

策略1:具体完成任务的步骤

如下图的例子,prompt中就给出了他应该执行的步骤。

在这里插入图片描述

下面是第二个例子,要求更具体一些:

在这里插入图片描述

策略2:指示模型在匆忙作出结论之前思考解决方案

比如下面的这个例子,其实学生算的是错的,但是GPT却看不出来:

在这里插入图片描述

然后利用策略2进行改良:

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HG8WdjEm-1683203440333)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504182618587.png)]

经过这么做后,chatGPT就有了“思考能力了”。

模型局限性

幻觉(Hallucination)

即使模型在训练的过程中有大量的专业知识,但是其实它并没有完全记住它所看到的信息,所以它不了解知识边界,所以他可能会尝试回答关于晦涩主题的问题时,编造一些听起来合理但是实际上是错的的内容。

​ 下图是一个例子:

但其实并没有这个产品。

一种减少幻觉的策略是要求模型首先从文本中找到任何相关的引用,然后要求它使用这些引用来回答问题,当然这只能减少,有时候源文档他也会编。。

二、迭代

一般来说,不会第一次尝试就能用到最终在引用程序中采用的提示,一般需要一个良好的迭代过程来改善。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VK4aEhoc-1683203144760)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504183445829.png)]

下面是一个具体的示例,这是一个摘要椅子说明书的示例:

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)

1.期望句子短点

直接运行的时候,会发现其摘要太长了,我们可能希望他短一点:

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)

这里我门通过了Use at most 50 words.来进行了长度的限制,可以通过不同的方法来实现。

2.让结果在我们期望的部分精确

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.

Use at most 50 words.

Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

这里通过下面的来实现的 :

The description is intended for furniture retailers, 
so should be technical in nature and focus on the 
materials the product is constructed from.

三、摘要

下面是一个关于熊猫玩具的评论:

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.
"""

下面是一个生成摘要的评论:

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)

1.生成特定需求的摘要

通过修改提示,可以让生成一个更适用于自己业务中特定团队的摘要。下面的例子是需要一个运输和产品交付有关的摘要要求。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HZ3mPtTP-1683203144761)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504184851720.png)]

2.提取信息的摘要

比如下面的例子,因为我们要求的是提取相关信息,并且也说明是运输部门需要的,所以这里打印的结果没有给其他无用的信息,只说了这个产品的到达比期望的早了一天:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Bh9VkXAD-1683203144761)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504185227612.png)]

3.总结多个评论

我们有对于几种产品的评论:


review_1 = prod_review 

# review for a standing lamp
review_2 = """
Needed a nice lamp for my bedroom, and this one \
had additional storage and not too high of a price \
point. Got it fast - arrived in 2 days. The string \
to the 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. Then I had a \
missing part, so I contacted their support and they \
very quickly got me the missing piece! Seems to me \
to be a great company that cares about their customers \
and products. 
"""

# review for an electric toothbrush
review_3 = """
My dental hygienist recommended an electric toothbrush, \
which is why I got this. The battery life seems to be \
pretty impressive so far. After initial charging and \
leaving the charger plugged in for the first week to \
condition the battery, I've unplugged the charger and \
been using it for twice daily brushing for the last \
3 weeks all on the same charge. But the toothbrush head \
is too small. I’ve seen baby toothbrushes bigger than \
this one. I wish the head was bigger with different \
length bristles to get between teeth better because \
this one doesn’t.  Overall if you can get this one \
around the $50 mark, it's a good deal. The manufactuer's \
replacements heads are pretty expensive, but you can \
get generic ones that're more reasonably priced. This \
toothbrush makes me feel like I've been to the dentist \
every day. My teeth feel sparkly clean! 
"""

# review for a blender
review_4 = """
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.
"""

reviews = [review_1, review_2, review_3, review_4]


我们可以通过一个循环来做提示:

for i in range(len(reviews)):
    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 20 words. 

    Review: ```{reviews[i]}```
    """

    response = get_completion(prompt)
    print(i, response, "\n")

四、推断

下面是一个关于灯的例子:

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!!
"""

1.推断单个情感

然后这里讲写一个提示来进行情绪分类:

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)

Give your answer as a single word, either "positive" \ or "negative".通过这句话使得了给出简洁的结果(positive)。

2.推断多个情感(多标签分类)

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)

3.识别愤怒情感

对于评论的例子来说,可能我们会特别的关注是否有人的评论有特别愤怒倾向的倾斜,那么我们或许需要对这方面做更加特别的注意:

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)

4.信息提取

下面的提示要求模型来提取购买物品和制造物品的公司名称:

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

输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6kHZl0BT-1683203144761)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504190358094-16831982403691.png)]

5.一次提取多个信息(同时做多个任务)

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

结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QalMWvy5-1683203144762)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504190507260.png)]

6.主题推断

下面是一个虚构的报纸文章,讲政府工作人员对他们所工作的机构的感受:

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.
"""

然后,下面的提示可以询问模型确定文本中正在讨论的五个主题:

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)

输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KRUNFtl1-1683203144762)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504190723993.png)]

该应用场景就可以是比如说报纸分类,或者是博客的标签分类,假如我们有这些标签:

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

那么就会得到这个结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XRKGwxfB-1683203144763)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504190918819.png)]

7.新闻提醒

下面的代码是实现了,如果当前的“新闻”是NASA的话,就会提醒,可以说是推断的一种应用吧

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!")

五、转换

LLM非常擅长将输入转换成不同的格式,例如多语种文本翻译、拼写及语法纠正、语气调整、格式转换等。本章节将介绍如何使用编程的方式,调用API接口来实现“文本转换”功能。

1.翻译

下面是一个中文翻译西班牙语的例子:

prompt = f"""
将以下中文翻译成西班牙语: \ 
```您好,我想订购一个搅拌机。```
"""
response = get_completion(prompt)
print(response)

1.2识别语种

prompt = f"""
请告诉我以下文本是什么语种: 
```Combien coûte le lampadaire?```
"""
response = get_completion(prompt)
print(response)

1.3多语种翻译

prompt = f"""
请将以下文本分别翻译成中文、英文、法语和西班牙语: 
```I want to order a basketball.```
"""
response = get_completion(prompt)
print(response)

1.4翻译+正式/非正式语气

prompt = f"""
请将以下文本翻译成中文,分别展示成正式与非正式两种语气: 
```Would you like to order a pillow?```
"""
response = get_completion(prompt)
print(response)

1.5通用翻译器

交流的用户可能来自各个不同的国家,使用不同的语言,因此我们需要一个通用翻译器,识别各个消息的语种,并翻译成目标用户的母语,从而实现更方便的跨国交流。

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"告诉我以下文本是什么语种,直接输出语种,如法语,无需输出标点符号: ```{issue}```"
    lang = get_completion(prompt)
    print(f"原始消息 ({lang}): {issue}\n")

    prompt = f"""
    将以下消息分别翻译成英文和中文,并写成
    中文翻译:xxx
    英文翻译:yyy
    的格式:
    ```{issue}```
    """
    response = get_completion(prompt)
    print(response, "\n=========================================")

打印结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-349xAUh3-1683203144763)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504193143390.png)]

2.语气/风格

写作的语气往往会根据受众对象而有所调整。例如,对于工作邮件,我们常常需要使用正式语气与书面用词,而对同龄朋友的微信聊天,可能更多地会使用轻松、口语化的语气。

prompt = f"""
将以下文本翻译成商务信函的格式: 
```小老弟,我小羊,上回你说咱部门要采购的显示器是多少寸来着?```
"""
response = get_completion(prompt)
print(response)

结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YHHmz2Be-1683203144764)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504193305409.png)]

3.格式转换

在下述例子中,我们有一个包含餐厅员工姓名和电子邮件的列表的JSON,我们希望将其从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"""
将以下Python字典从JSON转换为HTML表格,保留表格标题和列名:{data_json}
"""
response = get_completion(prompt)
print(response)

结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cu7thaRP-1683203144764)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504193352603.png)]

4.拼写及语法纠正(强烈推荐)

这个功能大佬非常推荐

以下给了一个例子,有一个句子列表,其中有些句子存在拼写或语法问题,有些则没有,我们循环遍历每个句子,要求模型校对文本,如果正确则输出“未发现错误”,如果错误则输出纠正后的文本。

text = [ 
  "The girl with the black and white puppies have a ball.",  # The girl has a ball.
  "Yolanda has her notebook.", # ok
  "Its going to be a long day. Does the car need it’s oil changed?",  # Homonyms
  "Their goes my freedom. There going to bring they’re suitcases.",  # Homonyms
  "Your going to need you’re notebook.",  # Homonyms
  "That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
  "This phrase is to cherck chatGPT for speling abilitty"  # spelling
]

那么可以编写如下的提示:

for i in range(len(text)):
    prompt = f"""请校对并更正以下文本,注意纠正文本保持原始语种,无需输出原始文本。
    如果您没有发现任何错误,请说“未发现错误”。
    
    例如:
    输入:I are happy.
    输出:I am happy.
    ```{text[i]}```"""
    response = get_completion(prompt)
    print(i, response)

打印结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yhfgBaWs-1683203144765)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504193553626.png)]

5.基于Readlines的纠错

下图是一个示例,可以看到,通过使用readlines这个库非常的直观。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vYS5nmbr-1683203144765)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504193932653.png)]

6.一个综合样例:文本翻译+拼写纠正+风格调整+格式转换

``

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"""
针对以下三个反引号之间的英文评论文本,
首先进行拼写及语法纠错,
然后将其转化成中文,
再将其转化成优质淘宝评论的风格,从各种角度出发,分别说明产品的优点与缺点,并进行总结。
润色一下描述,使评论更具有吸引力。
输出结果格式为:
【优点】xxx
【缺点】xxx
【总结】xxx
注意,只需填写xxx部分,并分段输出。
将结果输出成Markdown格式。
```{text}```
"""
response = get_completion(prompt)
display(Markdown(response))

打印:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ErecXIKx-1683203144766)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504194051241.png)]

六、扩展

扩展是将短文本,例如一组说明或主题列表,输入到大型语言模型中,让模型生成更长的文本,例如基于某个主题的电子邮件或论文。这样做有一些很好的用途,例如将大型语言模型用作头脑风暴的伙伴。但这种做法也存在一些问题,例如某人可能会使用它来生成大量垃圾邮件。因此,当你使用大型语言模型的这些功能时,请仅以负责任的方式和有益于人们的方式使用它们。

1.定制邮件

我们将根据客户评价和情感撰写自定义电子邮件响应。因此,我们将给定客户评价和情感,并生成自定义响应即使用 LLM 根据客户评价和评论情感生成定制电子邮件。

我们首先给出一个示例,包括一个评论及对应的情感

# 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.
"""
# 我们可以在推理那章学习到如何对一个评论判断其情感倾向
sentiment = "negative"

# 一个产品的评价
review = f"""
他们在11月份的季节性销售期间以约49美元的价格出售17件套装,折扣约为一半。\
但由于某些原因(可能是价格欺诈),到了12月第二周,同样的套装价格全都涨到了70美元到89美元不等。\
11件套装的价格也上涨了大约10美元左右。\
虽然外观看起来还可以,但基座上锁定刀片的部分看起来不如几年前的早期版本那么好。\
不过我打算非常温柔地使用它,例如,\
我会先在搅拌机中将像豆子、冰、米饭等硬物研磨,然后再制成所需的份量,\
切换到打蛋器制作更细的面粉,或者在制作冰沙时先使用交叉切割刀片,然后使用平面刀片制作更细/不粘的效果。\
制作冰沙时,特别提示:\
将水果和蔬菜切碎并冷冻(如果使用菠菜,则轻轻煮软菠菜,然后冷冻直到使用;\
如果制作果酱,则使用小到中号的食品处理器),这样可以避免在制作冰沙时添加太多冰块。\
大约一年后,电机发出奇怪的噪音,我打电话给客服,但保修已经过期了,所以我不得不再买一个。\
总的来说,这些产品的总体质量已经下降,因此它们依靠品牌认可和消费者忠诚度来维持销售。\
货物在两天内到达。
"""

这里我们将根据情感定制回复,这里的指令是:假设你是一个客户服务AI助手,你的任务是为客户发送电子邮件回复,根据通过三个反引号分隔的客户电子邮件,生成一封回复以感谢客户的评价:

prompt = f"""
你是一位客户服务的AI助手。
你的任务是给一位重要客户发送邮件回复。
根据客户通过“```”分隔的评价,生成回复以感谢客户的评价。提醒模型使用评价中的具体细节
用简明而专业的语气写信。
作为“AI客户代理”签署电子邮件。
客户评论:
```{review}```
评论情感:{sentiment}
"""
response = get_completion(prompt)
print(response)

打印:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ocaaTGnz-1683203144767)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504194949096.png)]

2.温度系数的使用(重要参数)

接下来,我们将使用语言模型的一个称为“温度”的参数,它将允许我们改变模型响应的多样性。您可以将温度视为模型探索或随机性的程度

例如,在一个特定的短语中,“我的最爱食品”最有可能的下一个词是“比萨”,其次最有可能的是“寿司”和“塔可”。因此,在温度为零时,模型将总是选择最有可能的下一个词,而在较高的温度下,它还将选择其中一个不太可能的词,在更高的温度下,它甚至可能选择塔可,而这种可能性仅为五分之一。您可以想象,随着模型继续生成更多单词的最终响应,“我的最爱食品是比萨”将会与第一个响应“我的最爱食品是塔可”产生差异。因此,随着模型的继续,这两个响应将变得越来越不同。

一般来说,在构建需要可预测响应的应用程序时,我建议使用温度为零。在所有课程中,我们一直设置温度为零,如果您正在尝试构建一个可靠和可预测的系统,我认为您应该选择这个温度。如果您尝试以更具创意的方式使用模型,可能需要更广泛地输出不同的结果,那么您可能需要使用更高的温度。

示例:

# 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:

prompt = f"""
你是一名客户服务的AI助手。
你的任务是给一位重要的客户发送邮件回复。
根据通过“```”分隔的客户电子邮件生成回复,以感谢客户的评价。
如果情感是积极的或中性的,感谢他们的评价。
如果情感是消极的,道歉并建议他们联系客户服务。
请确保使用评论中的具体细节。
以简明和专业的语气写信。
以“AI客户代理”的名义签署电子邮件。
客户评价:```{review}```
评论情感:{sentiment}
"""
response = get_completion(prompt, temperature=0.7)
print(response)

这里设置了温度,得到了下面的结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xY4jElMO-1683203144767)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504195237697.png)]

在温度为零时,每次执行相同的提示时,您应该期望获得相同的完成。而使用温度为0.7,则每次都会获得不同的输出。

所以,您可以看到它与我们之前收到的电子邮件不同。让我们再次执行它,以显示我们将再次获得不同的电子邮件。

因此,我建议您自己尝试温度,以查看输出如何变化。总之,在更高的温度下,模型的输出更加随机。您几乎可以将其视为在更高的温度下,助手更易分心,但也许更有创造力。

七、聊天机器人

像 ChatGPT 这样的聊天模型实际上是组装成以一系列消息作为输入,并返回一个模型生成的消息作为输出的。虽然聊天格式的设计旨在使这种多轮对话变得容易,但我们通过之前的学习可以知道,它对于没有任何对话的单轮任务也同样有用。

接下来,我们将定义两个辅助函数。第一个是单轮的,我们将prompt放入看起来像是某种用户消息的东西中。另一个则传入一个消息列表。这些消息可以来自不同的角色,我们会描述一下这些角色。

第一条消息是一个系统消息,它提供了一个总体的指示,然后在这个消息之后,我们有用户和助手之间的交替。如果你曾经使用过 ChatGPT 网页界面,那么你的消息是用户消息,而 ChatGPT 的消息是助手消息。系统消息则有助于设置助手的行为和角色,并作为对话的高级指示。你可以想象它在助手的耳边低语,引导它的回应,而用户不会注意到系统消息。

因此,作为用户,如果你曾经使用过 ChatGPT,你可能不知道 ChatGPT 的系统消息是什么,这是有意为之的。系统消息的好处是为开发者提供了一种方法,在不让请求本身成为对话的一部分的情况下,引导助手并指导其回应。

import os
import openai

OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY2")
openai.api_key = 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, # 控制模型输出的随机程度
    )
    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, # 控制模型输出的随机程度
    )
#     print(str(response.choices[0].message))
    return response.choices[0].message["content"]

系统消息说,你是一个说话像莎士比亚的助手。这是我们向助手描述它应该如何表现的方式。然后,第一个用户消息是,给我讲个笑话。接下来的消息是,为什么鸡会过马路?然后最后一个用户消息是,我不知道。

# 中文
messages =  [  
{'role':'system', 'content':'你是一个像莎士比亚一样说话的助手。'},    
{'role':'user', 'content':'给我讲个笑话'},   
{'role':'assistant', 'content':'鸡为什么过马路'},   
{'role':'user', 'content':'我不知道'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
#因为它要去找“母鸡”。哈哈哈!(注:此为英文双关语,"chicken"是鸡的意思,也是胆小的意思;"cross the road"是过马路的意思,也是“破坏规则”的意思。

每次与语言模型的交互都是一个独立的交互,这意味着我们必须提供所有相关的消息,以便模型在当前对话中进行引用。如果想让模型引用或 “记住” 对话的早期部分,则必须在模型的输入中提供早期的交流。我们将其称为上下文。

1.聊天机器人

现在,我们构建一个 “订餐机器人”,我们需要它自动收集用户信息,接受比萨饼店的订单。

下面这个函数将收集我们的用户消息,以便我们可以避免手动输入,就像我们在刚刚上面做的那样。这个函数将从我们下面构建的用户界面中收集提示,然后将其附加到一个名为上下文的列表中,并在每次调用模型时使用该上下文。模型的响应也会被添加到上下文中,所以模型消息和用户消息都被添加到上下文中,因此上下文逐渐变长。这样,模型就有了需要的信息来确定下一步要做什么。

def collect_messages(_):
    prompt = inp.value_input
    inp.value = ''
    context.append({'role':'user', 'content':f"{prompt}"})
    response = get_completion_from_messages(context) 
    context.append({'role':'assistant', 'content':f"{response}"})
    panels.append(
        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
    panels.append(
        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
 
    return pn.Column(*panels)

现在,我们将设置并运行这个 UI 来显示订单机器人。初始的上下文包含了包含菜单的系统消息。请注意,上下文会随着时间的推移而不断增长。

!pip install panel
# 中文
import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

context = [{'role':'system', 'content':"""
你是订餐机器人,为披萨餐厅自动收集订单信息。
你要首先问候顾客。然后等待用户回复收集订单信息。收集完信息需确认顾客是否还需要添加其他内容。
最后需要询问是否自取或外送,如果是外送,你要询问地址。
最后告诉顾客订单总金额,并送上祝福。

请确保明确所有选项、附加项和尺寸,以便从菜单中识别出该项唯一的内容。
你的回应应该以简短、非常随意和友好的风格呈现。

菜单包括:

菜品:
意式辣香肠披萨(大、中、小) 12.95、10.00、7.00
芝士披萨(大、中、小) 10.95、9.25、6.50
茄子披萨(大、中、小) 11.95、9.75、6.75
薯条(大、小) 4.50、3.50
希腊沙拉 7.25

配料:
奶酪 2.00
蘑菇 1.50
香肠 3.00
加拿大熏肉 3.50
AI酱 1.50
辣椒 1.00

饮料:
可乐(大、中、小) 3.00、2.00、1.00
雪碧(大、中、小) 3.00、2.00、1.00
瓶装水 5.00
"""} ]  # accumulate messages


inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
    inp,
    pn.Row(button_conversation),
    pn.panel(interactive_conversation, loading_indicator=True, height=300),
)
dashboard

现在我们可以要求模型创建一个 JSON 摘要发送给订单系统。

所以我们现在追加另一个系统消息,它是另一条prompt,我们说创建一个刚刚订单的 JSON 摘要,列出每个项目的价格,字段应包括1)披萨,包括尺寸,2)配料列表,3)饮料列表,4)辅菜列表,包括尺寸,最后是总价格。这里也可以在这里使用用户消息,不一定是系统消息。

请注意,这里我们使用了一个较低的temperature,因为对于这些类型的任务,我们希望输出相对可预测。

messages =  context.copy()
messages.append(
{'role':'system', 'content':'创建上一个食品订单的 json 摘要。\
逐项列出每件商品的价格,字段应该是 1) 披萨,包括大小 2) 配料列表 3) 饮料列表,包括大小 4) 配菜列表包括大小 5) 总价'},    
)

response = get_completion_from_messages(messages, temperature=0)
print(response)

输出:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3nsDNcR0-1683203144767)(F:\Study\NLP\笔记\博客\ChatGPT吴恩达网课毕节.assets\image-20230504200013220.png)]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/489641.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Day961.老城区前端改造 -遗留系统现代化实战

老城区前端改造 Hi&#xff0c;我是阿昌&#xff0c;今天学习记录的是关于老城区前端改造的内容。 什么是“改造老城区”。改造老城区模式 是指对遗留系统内部的模块进行治理&#xff0c;让模块内部结构合理、模块之间职责清晰的一系列模式。 也就是说&#xff0c;在遗留系统…

【Redis】聊一下Redis基础架构

我们知道学习一个技术&#xff0c;最好的方式就是从全局观出发&#xff0c;然后针对不同的点进行拆分&#xff0c;一个个破解。既可以将学到的和已有的知识联系起来&#xff0c;又可以有一定的深度和目的性。 Redis基础架构 对于一个中间件来说&#xff0c;一个是使用层面&…

GO数组切片-线性数据结构

数据结构 类型 什么是类型 &#xff1f; 内存中的二进制数据本身没有什么区别&#xff0c;就是一串0或1的组合。 内存中有一个字节内容是0x63&#xff0c;他究竟是深恶 字符串?字符&#xff1f;还是整数&#xff1f; 本来0x63表示数字 但是文字必须编码成为0和1的组合 才能记…

【C++】红黑树源码剖析

目录 概述 算法 调整策略 源码 RBTree.h test.cpp 概述 红黑树&#xff0c;是一种二叉搜索树&#xff0c;每一个节点上有一个存储位表示节点的颜色&#xff0c;可以是Red或Black。 通过对任何一条从根到叶子的路径上各个节点着色方式的限制&#xff0c;红黑树确保没有一…

ChatGPT、文心一言、New Bing到底哪个更AI?

Hello 各位小伙伴&#xff0c;要说今年最火爆的 IP 有哪些&#xff0c;那一定少不了人工智能&#xff08;AI&#xff09;&#xff0c;市面上已经相继出现了我们熟知的 ChatGPT&#xff08;OpenAI&#xff09;、ChatGPT 国外镜像网站、文心一言&#xff08;百度&#xff09;、Ne…

MySQL --- DDL图形化工具表结构操作

一. 图形化工具 1. 介绍 前面我们讲解了DDL中关于数据库操作的SQL语句&#xff0c;在我们编写这些SQL时&#xff0c;都是在命令行当中完成的。大家在练习的时候应该也感受到了&#xff0c;在命令行当中来敲这些SQL语句很不方便&#xff0c;主要的原因有以下 3 点&#xff1a;…

redis的介绍和安装

文章目录 一、redis的介绍和安装1.1 初识redis1.1.1 redis是什么&#xff1f;1.1.2 redis能做什么&#xff1f; 1.2 redis的安装配置 一、redis的介绍和安装 1.1 初识redis 1.1.1 redis是什么&#xff1f; Redis是一个开源的内存数据存储系统&#xff0c;也可以被用作数据库、…

阿里云服务器vCPU和CPU有区别吗?

阿里云服务器vCPU是什么&#xff1f;vCPU和CPU有什么区别&#xff1f;CPU是指云服务器的中央处理器&#xff0c;一个CPU可以包含若干个物理核&#xff0c;一台云服务器ECS实例的CPU选项由CPU物理核心数和每核线程数决定&#xff0c;通过超线程HT&#xff08;Hyper-Threading&am…

探索三维世界【3】:Three.js 的 Geometry 几何体 与 Material 材质

探索三维世界【3】&#xff1a;Three.js 的 Material 材质 1、Geometry几何体2、Material 材质3、创建平面与材质4、多平面渲染 1、Geometry几何体 Three.js中的几何体Geometry是构成3D模型的基本单元之一&#xff0c;它定义了一个物体的形状和大小。Geometry包含了物体的顶点…

RK3568平台开发系列讲解(网络篇)网络包的接收过程

🚀返回专栏总目录 文章目录 一、内核接收网络包过程二、用户态读取网络包过程沉淀、分享、成长,让自己和他人都能有所收获!😄 📢本篇我们一起来梳理下网络包的接收过程。 一、内核接收网络包过程 硬件网卡接收到网络包之后,通过 DMA 技术,将网络包放入 Ring Buffer;…

PyTorch数据加载工具:高效处理常见数据集的利器

❤️觉得内容不错的话&#xff0c;欢迎点赞收藏加关注&#x1f60a;&#x1f60a;&#x1f60a;&#xff0c;后续会继续输入更多优质内容❤️ &#x1f449;有问题欢迎大家加关注私戳或者评论&#xff08;包括但不限于NLP算法相关&#xff0c;linux学习相关&#xff0c;读研读博…

HTTP 缓存新鲜度 max-age

新鲜度 理论上来讲&#xff0c;当一个资源被缓存存储后&#xff0c;该资源应该可以被永久存储在缓存中。由于缓存只有有限的空间用于存储资源副本&#xff0c;所以缓存会定期地将一些副本删除&#xff0c;这个过程叫做缓存驱逐。另一方面&#xff0c;当服务器上面的资源进行了更…

使用ControlNet控制Stable-Diffusion出图人物的姿势

概述 在Stable-Diffusion&#xff08;以下简称SD&#xff09;出图中&#xff0c;我们往往需要对出图人物的姿势进行控制&#xff0c;这里我使用一个比较简单上手的方法&#xff0c;通过ControlNet可以很方便地对画面风格&#xff0c;人物姿势进行控制&#xff0c;从而生成更加…

Python —— Windows10下训练Yolov5分割模型并测试

附:Python —— Windows10下配置Pytorch环境、进行训练模型并测试(完整流程,附有视频)   效果 手机拍摄一段工位视频,上传到win10训练了yolov5分割鼠标的样本后推理效果截图。 训练准备 1、查看自己下载的Yolov5源码是否存在"segment"文件夹,该文件夹下存在分…

【Python入门篇】——Python基础语法(字面量注释与变量)

作者简介&#xff1a; 辭七七&#xff0c;目前大一&#xff0c;正在学习C/C&#xff0c;Java&#xff0c;Python等 作者主页&#xff1a; 七七的个人主页 文章收录专栏&#xff1a; Python入门&#xff0c;本专栏主要内容为Python的基础语法&#xff0c;Python中的选择循环语句…

有限等待忙等、让权等待死等、互斥遵循的几大原则——参考《天勤操作系统》,柳婼的博客

参考柳婼的博客 一、 有限等待&&死等 有限等待: 对请求访问的临界资源的进程&#xff0c;应该保证有限的时间进入临界区&#xff0c;以免陷入死等状态。受惠的是进程自己 死等: 进程在有限时间内根本不能进入临界区&#xff0c;而一直尝试进入陷入一种无结果的等待状…

在字节跳动做了6年软件测试,4月无情被辞,想给划水的兄弟提个醒

先简单交代一下背景吧&#xff0c;某不知名 985 的本硕&#xff0c;17 年毕业加入字节&#xff0c;以“人员优化”的名义无情被裁员&#xff0c;之后跳槽到了有赞&#xff0c;一直从事软件测试的工作。之前没有实习经历&#xff0c;算是6年的工作经验吧。 这6年之间完成了一次…

TIM编码器接口

一、知识点 1、Encoder Interface 编码器接口的工作流程 编码器接口可接收增量&#xff08;正交&#xff09;编码器的信号&#xff0c;根据编码器旋转产生的正交信号脉冲&#xff0c;自动控制CNT自增或自减&#xff0c;从而指示编码器的位置、旋转方向和旋转速度 2、编码器接口…

6.2.1邻接矩阵法

接下来我们将认识图的几种存储结构&#xff1a; 邻接矩阵&#xff0c;邻接表&#xff0c;十字链表&#xff0c;邻接多重表 图的存储 1&#xff09;邻接矩阵法 0表示邻接vertex不邻接 只需要一个二位数组就可以实现&#xff1a; 顶点虽然是char类型&#xff0c;但可以存储更加…

搭建vue3+vite工程

搭建vue3vite工程 目录 搭建vue3vite工程 一、官方-文档-快速上手 二、详细截图及步骤 2.1、安装nvm 2.2、 用nvm安装多版本可切换的node.js版本 2.3、 按照官方文档初始化最近版本的vue3 三、脚本配置与调试 3.1、"2.3、"所产生的配置及脚本命令 3.2、脚本…