Skip to content

Inerative

当使用 LLM 构建应用程序时,很难在初次尝试中就得到符合预期要求的 Prompt,这不重要,重要是的能对起进行迭代,能对 Prompt 进行改进,最终就能得到一个符合预期的 Prompt

任务——从产品说明书生成一份营销产品描述

首先,我们为 LLM 提供一份完备的产品说明书,类似于 Raw Data,描述了产品的完备细节

python
# 示例:产品说明书
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 还比较模糊,不过没关系,可以根据后续 LLM 的表现来进行优化

python
# 提示:基于说明书生成营销描述
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)

问题一:回答太长了

LLM 似乎给出了一个很好的描述,但是问题是,回答太长了,我们需要在 Prompt 中对其进行限制,不需要这么长的回答

python
# 优化后的 Prompt,要求生成描述不多于 50 词
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)

但是 LLM 对于字数的控制很不理想,一般来说会在我们限定的词数附近波动

问题二:文本关注在错误的细节上

我们会发现的第二个问题是,这个网站并不是直接向消费者销售,它实际上旨在向家具零售商销售家具,他们会更关心椅子的技术细节和材料,在这种情况下,你可以修改这个提示,让它更精确地描述椅子的技术细节

python
# 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面
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)

我可能进一步想要在描述的结尾包括产品 ID。因此,我可以进一步改进这个提示,要求在描述的结尾,包括在技术说明中的每个 7 个字符产品 ID

python
# 更进一步,要求在描述末尾包含 7个字符的产品ID
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)

问题三:需要一个表格形式的描述

在这里我们需要一个更复杂的要求,要求 LLM 将最终的输出组织成为 HTML,并且制定表格的列、表名和格式

python
# 要求它抽取信息并组织成表格,并指定表格的列、表名和格式
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.
All your output should be valid HTML. No more plain text.

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

response = get_completion(prompt)
print(response)

最后更新于:

Released under the MIT License.