1 前言
- 我们很难在初次尝试中就设计出最佳的提示,因此需要根据ChatGPT的反馈进行分析,分析输出具体在哪里不符合期望,然后不断思考和优化提示。
- 如果有条件的话,最好是利用批量的样本来改善提示,这样可以对你的优化结果有一个较为直观的体现。
准备代码和之前的一样,就不加注释了,详情见: 大模型笔记:吴恩达 ChatGPT Prompt Engineering for Developers(1) prompt的基本原则和策略-CSDN博客
import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.getenv('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, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
2 举例:根据货物单,生成一个货品描述
2.0 货物单:
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
"""
2.1 prompt 版本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)
'''
Introducing the SWC-100/SWC-110 Office Chair, a sleek and stylish addition to any workspace. This chair is part of a stunning collection of mid-century inspired office furniture, offering a cohesive look with filing cabinets, desks, bookcases, meeting tables, and more.
Customize your chair with several options for shell color and base finishes to suit your personal style. Choose between plastic back and front upholstery or full upholstery in a variety of fabric and leather options. The base finish options include stainless steel, matte black, gloss white, or chrome, allowing you to create the perfect look for your space.
Designed for both home and business settings, this chair is suitable for contract use and offers comfort and functionality. The 5-wheel plastic coated aluminum base provides stability, while the pneumatic chair adjust allows for easy raise/lower action.
With dimensions of 53 cm in width, 51 cm in depth, and 80 cm in height, this chair offers a comfortable seat height of 44 cm and seat depth of 41 cm. Choose between soft or hard-floor caster options and two seat foam densities for personalized comfort. The chair is available with or without armrests, with the option for 8 position PU armrests.
Constructed with high-quality materials, including a cast aluminum shell with modified nylon coating and HD36 foam seat, this chair is both durable and stylish. Made in Italy, you can trust in the craftsmanship and design of this office chair.
Elevate your workspace with the SWC-100/SWC-110 Office Chair, a versatile and sophisticated seating solution for any environment.
'''
2.2 prompt 版本2:限制 生成文本的长度
+ Use at most 50 words.
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)
'''
Introducing our versatile and stylish office chair, part of a
mid-century inspired furniture collection. Available in various
colors and finishes, with customizable upholstery options.
Designed for comfort and durability, suitable for both home
and business use. Made in Italy with high-quality materials.
Elevate your workspace with this modern chair.
'''
len(response.split()) #49
2.3 prompt 版本3:着重在听众需要的信息上
+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.
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.
Use at most 50 words.
Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
'''
Introducing our versatile office chair, part of a stylish mid-century
inspired collection. Choose from a variety of shell colors and base
finishes to suit your space. Constructed with a durable aluminum base
and high-density foam seat for comfort. Perfect for home or office use.
Product IDs: SWC-100, SWC-110.
'''
2.4 prompt 版本4:添加产品各维度数据的表格
+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'.
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'.
Use at most 50 words.
Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)
'''
Introducing our versatile and stylish office chair, part of a
mid-century inspired furniture collection. Constructed with a
durable cast aluminum shell and base glider coated with modified
nylon. The seat features high-density foam for comfort. Available
in various colors and finishes. Perfect for home or office use.
Product IDs: SWC-100, SWC-110
Product Dimensions:
| Dimension | Measurement |
|--------------|-------------|
| Width | 20.87" |
| Depth | 20.08" |
| Height | 31.50" |
| Seat Height | 17.32" |
| Seat Depth | 16.14" |
Shop now and elevate your workspace with this Italian-made chair.
'''
2.5 prompt 版本5:输出内容改成HTML格式
+Format everything as HTML that can be used in a website. Place the description in a <div> element.
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)
'''
<div>
<p>This mid-century inspired office chair is a stylish and functional
addition to any workspace. The chair is available in a variety of shell
colors and base finishes to suit your aesthetic preferences. You can
choose between plastic back and front upholstery or full upholstery in
a range of fabric and leather options. The chair is constructed with a
durable 5-wheel plastic coated aluminum base and features a pneumatic
adjust for easy height customization. Whether you need a chair for your
home office or a business setting, this chair is a versatile and comfortable
choice. Made in Italy, this chair is designed for both style and durability.</p>
<p>Product IDs: SWC-100, SWC-110</p>
<table>
<caption>Product Dimensions</caption>
<tr>
<th>Dimension</th>
<th>Measurements (inches)</th>
</tr>
<tr>
<td>Width</td>
<td>20.87"</td>
</tr>
<tr>
<td>Depth</td>
<td>20.08"</td>
</tr>
<tr>
<td>Height</td>
<td>31.50"</td>
</tr>
<tr>
<td>Seat Height</td>
<td>17.32"</td>
</tr>
<tr>
<td>Seat Depth</td>
<td>16.14"</td>
</tr>
</table>
</div>
'''