使用分隔符将特定的文本片段和提示的其他部分分开,可以是反引号(```)、单引号(''')、破折号(—)、尖括号(< >),也可以是 XML 标签等。这有助于模型区分指令区和数据区。
示例代码如下:
text = f"""
You should express what you want a model to do by
providing instructions that are as clear and
specific as you can possibly make them.
This will guide the model towards the desired output,
and reduce the chances of receiving irrelevant
or incorrect responses.
"""# 用 ``` 将输入的部分分开
prompt = f"""
Summarize the text delimited by triple backticks
into a single sentence.
``````
"""
response = get_completion(prompt)
(response)
{text}
print
通过分隔符,模型能更准确地识别需要处理的文本范围,避免混淆指令与数据。
3.1.2 使用结构化的输出
为了使传递模型的输出更容易解析,可使用如 HTML 或 JSON 这样的结构化输出格式。这对于程序化处理结果非常关键。
示例代码如下:
# 用带 book_id, title, author, genre 的 JSON 格式输出三个编造的书名、作者以及流派的列表
prompt = f"""
Generate a list of three made-up book titles along
with their authors and genres.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)
text_1 = f"""
Making a cup of tea is easy! First, you need to get some
water boiling. While that's happening, grab a cup and put
a tea bag in it. Once the water is hot enough, just pour
it over the tea bag. Let it sit for a bit so the tea can
steep. After a few minutes, take out the tea bag.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, re-write those
instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions,
then simply write "No steps provided."
"""{text_1}"""
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
当输入不包含步骤时,模型会正确返回提示信息,体现了逻辑判断能力。
3.1.4 保持原有风格
可以让模型学习保持原有对话风格,例如模仿特定角色的语气。这有助于生成更具个性化或符合场景的内容。
示例代码如下:
prompt = f"""
Your task is to answer in a consistent style.
<child>: Teach me about patience.
<grandparent>: The river that carves the deepest
valley flows from a modest spring; the
grandest symphony originates from a single note;
the most intricate tapestry begins with a solitary thread.
<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)
模型会根据上下文模仿长辈的语气,保持风格一致性。
3.2 给模型思考的时间
在有些时候,如果直接要求模型快速回答问题,可能会给出质量不高的回答。可以要求模型有一连串的推理过程,然后再给出答案。指示模型对一个问题进行更长时间的思考,通常被称为思维链(Chain of Thought)。
3.2.1 指定完成一项任务所需的步骤
告诉模型完成一项任务需要先做什么,分步执行。这能有效降低复杂任务的出错率。
示例代码如下:
text = f"""
In a charming village, siblings Jack and Jill set out on
a quest to fetch water from a hilltop well. As they climbed,
singing joyfully, misfortune struck—Jack tripped on a stone
and tumbled down the hill, with Jill following suit.
"""
prompt_1 = f"""
Perform the following actions:
1 - Summarize the following text delimited by triple backticks
with 1 sentence.
2 - Translate the summary into Chinese.
3 - List each name in the Chinese summary.
4 - Output a json object that contains the following keys:
Chinese_summary, num_names.
Separate your answers with line breaks.
Text:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
通过明确步骤,模型能按顺序处理信息,输出更完整的结果。
3.2.2 指示模型先找出自己的解决方法
让模型先独立解决问题,再与给定方案对比。这种方法能纠正模型可能存在的错误认知。
示例代码如下:
prompt = f"""
Your task is to determine if the student's solution
is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem including the final total.
- Then compare your solution to the student's solution
and evaluate if the student's solution is correct or not.
Don't decide if the student's solution is correct until
you have done the problem yourself.
Use the following format:
Question:
\`
question here
\`
Student's solution:
\`
student's solution here
\`
Actual solution:
\`
steps to work out the solution and your solution here
\`
Is the student's solution the same as actual solution calculated:
\`
yes or no
\`
Student grade:
\`
correct or incorrect
\`
Question:
\`
I'm building a solar power installation and I need help
working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost
me a flat $100k per year, and an additional $10 / square foot
What is the total cost for the first year of operations
as a function of the number of square feet.
\`
Student's solution:
\`
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
\`
Actual solution:
"""
response = get_completion(prompt)
print(response)
fact_sheet_chair = """
OVERVIEW
- Part of a beautiful family of mid-century inspired office furniture...
DIMENSIONS
- WIDTH 53 CM | 20.87"
..."""# 第一次尝试
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)
# 第二次尝试:限制字数
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)
# 第三次尝试:针对特定受众
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)
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)
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?
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 = 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)
这些功能展示了模型在语义理解和信息抽取方面的强大能力。
七、文本转换
使用大语言模型进行文本转换,如语言翻译、拼写纠错、风格改写等。
7.1 翻译
prompt = f"""
Translate the following English text to Spanish:
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)
7.2 改写
用另一种方式改写原文本,例如将口语转为正式商务信函。
prompt = f"""
Translate the following from slang to a business letter:
'Dude, This is Joe, check out this spec on this standing lamp.'
"""
response = get_completion(prompt)
print(response)
7.3 格式转换
进行不同数据格式之间的转换,如 JSON 转 HTML 表格。
data_json = { "resturant employees" :[
{"name":"Shyam", "email":"[email protected]"},
{"name":"Bob", "email":"[email protected]"},
{"name":"Jai", "email":"[email protected]"}
]}
prompt = f"""
Translate the following python dictionary from JSON to an HTML
table with column headers and title: {data_json}
"""
response = get_completion(prompt)
print(response)
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...
"""
进行扩展回复:
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)
九、聊天机器人
可以借助 OpenAI 的 API 自定义一个自己的聊天机器人。这里不再是单一的输入等待输出,而是需要传入一个信息列表,以帮助模型理解自己扮演的角色和历史上下文。
首先需要另外定义一个辅助函数来处理多轮对话:
defget_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
)
return response.choices[0].message["content"]
messages = [
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},
{'role':'user', 'content':'tell me a joke'},
{'role':'assistant', 'content':'Why did the chicken cross the road'},
{'role':'user', 'content':'I don\'t know'} ]
response = get_completion_from_messages(messages, temperature=1)
print(response)
context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant.
You first greet the customer, then collects the order,
and then asks if it's a pickup or delivery.
You wait to collect the entire order, then summarize it and check for a final
time if the customer wants to add anything else.
If it's a delivery, you ask for an address.
Finally you collect the payment.
Make sure to clarify all options, extras and sizes to uniquely
identify the item from the menu.
You respond in a short, very conversational friendly style.
The menu includes
pepperoni pizza 12.95, 10.00, 7.00
cheese pizza 10.95, 9.25, 6.50
eggplant pizza 11.95, 9.75, 6.75
fries 4.50, 3.50
greek salad 7.25
Toppings:
extra cheese 2.00,
mushrooms 1.50
sausage 3.00
canadian bacon 3.50
AI sauce 1.50
peppers 1.00
Drinks:
coke 3.00, 2.00, 1.00
sprite 3.00, 2.00, 1.00
bottled water 5.00
"""} ]