吴恩达联手OpenAI推出Prompt Engineering 课程2

4. Iterative

吴恩达联手 OpenAI 推出 Prompt Engineering 课程 2

本节遵循上图所提到的 Prompt guideline 四个循环迭代的步骤来构建完善的 Prompt。

Iterative 小节 “Generate a marketing product description from a product fact sheet” 来演示,怎么逐步完善复杂的 Prompt 来获取 reponse。

  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.

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

我们看到输出太长了,没什么重点。

  1. 添加字数或者句数限制,比如这里 Use at most 50 words., 如果你用len(response.split(" ")) 看看输出长度的话应该在 50 左右。
  2. 但是这时可能还是聚焦于错误的细节,继续添加指令 The description is intended for furniture retailers, so should be technical in nature and focus on the materials the product is constructed from. 面向零售商,描述注重技术。
  3. 如果还有要求,比如要添加 Product ID,也可添加指令。
  4. 最后实现我们最初的复杂想法,输出产品描述给网站,格式是 HTML。Format everything as HTML that can be used in a website.Place the description in a element.

完整的流程代码 Prompt 和 text 如下:

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.

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

At the end of the description, include every 7-character 
Product ID in the technical specification.

After the description, include a table that gives the 
product's dimensions. The table should have two columns.
In the first column include the name of the dimension. 
In the second column include the measurements in inches only.

Give the table the title 'Product Dimensions'.

Format everything as HTML that can be used in a website. 
Place the description in a <div> element.

Technical specifications: ```{fact_sheet_chair}```
"""

response = get_completion(prompt)
print(response)

5. Summarizing

这部分就是对文本和句子进行总结,当然你可以限定面对的对象,限制字数。另外 extractsummarize是有区别的。下面是多个评价进行总结的 prompt:

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

6. Inferring

对于推理能力,比如对评价进行正负情感推理。既可以进行正负评价推理,又可以推理具体情感如,happy, satisfied, grateful, impressed, content。这些都是 LLM 的无限可能性。先看看正负情感推理。

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

再看看具体情绪推理:

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)

接下来还示范了如何进行产品和品牌抽取,多个任务混合的 Prompt 以及 topic 推断(限制主题字数)等。

7. Transforming

Transforming 包括,

  1. 翻译
  2. Tone Transformation
  3. Format Conversion(比如 json->html)
  4. Spellcheck/Grammar check,这个有点挤压 Grammarly 类似产品了。

简单的翻译 prompt 如下,在 user_messages 中添加你要翻译的文本,你就能获取到中文翻译了。

user_messages = [
  "An anonymous letter alerted police to the possibility of a terrorist attack at the airport.",      
  "Some NLP tasks can be solved in a fully unsupervised fashion by providing a pretrained languagemodel with“task descriptions”in naturallanguage",              

] 

for issue in user_messages:
    prompt = f"""Translate the following  text to Chinese \: ```{issue}```"""
    response = get_completion(prompt)
    print(response, "\n")

8. Expanding

Expanding 是将短文本拓展成长文本,或者根据 topic 进行创作,就像平常公司的头脑风暴。

示例是对顾客的评价,进行自动回复,效果还挺好的。

这里注意下 openai model 中 temperature 参数意义。如下调用函数中(这也是 openai 包使用的基本模版),temperature表示输出随机性。

import openai
import os
openai.api_key  = 'sk-'

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, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message["content"]

具体来说如下图所示,对于输入 my favorite food is 给 ChatGPT 模型,会输入下一个词和其概率,如果你设置temperature=0, 就只会输出最大概率那个。而设置temperature=0.3, 会有 pizza 和 sushi 两个输出,再从中随机选取一个作为 response。设置 0.7 就有 3 个输出了,可以总结为:

  1. 对于稳定性、要求可预测的任务选取temperature=0
  2. 对于需要变化性任务选取temperature=0.3, 如果要求随机性高,有创造性的 response 选取 0.7。

吴恩达联手 OpenAI 推出 Prompt Engineering 课程 2

9. Chatbot

ChatGPT 用对话数据训练过,所以可以设置 role 来定制 Chatbot, 使用的 help function 跟以前不一样,需要传入 role 信息。

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

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, # this is the degree of randomness of the model's output
    )
#     print(str(response.choices[0].message))
    return response.choices[0].message["content"]

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Yes,  can you remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

还可以创建订单助手 bot,示例就是使用 panel 构建了 GUI 的连续型对话助手。

10. 总结

Prompt Engineer 课程总结如下:

  • 准则
  • 写清晰明确的指令
  • 给模型时间”思考“
  • 迭代进行 prompt 开发
  • 能力: 总结,推断,变形,拓展
  • 构建 chatbot
正文完
 
admin
版权声明:本站原创文章,由 admin 2023-11-26发表,共计6302字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请联系tensortimes@gmail.com。
评论(没有评论)
验证码