一、什么是 Prompt
在人工智能领域,Prompt(提示词)指的是用户给大型语言模型发出的指令。其核心作用是引导模型生成符合预期主题或内容的文本,从而控制生成结果的方向和内容。大模型是根据用户提出的问题来输出下文,因此用户提出的问题的质量在很大程度上影响着模型的输出效果。
由此引发了一门新的学科,叫做提示工程(Prompt Engineering)。用户可以通过提示工程来提高大语言模型的安全性,也可以赋能大语言模型,使其更好地服务于特定任务。有效的 Prompt 设计能够显著提升模型输出的准确性、相关性和可用性。
二、前期准备
在使用 Python 调用大模型 API 之前,需要完成以下环境配置:
- 申请 API Key:访问 OpenAI 官网申请 API 密钥。注意妥善保管密钥,避免泄露。
- 安装开发环境:电脑需安装 Jupyter Notebook,可使用 pip 命令安装:
pip install notebook。 - 创建项目目录:新建一个专门用来存放代码和文件的文件夹,便于管理。
- 安装依赖库:使用 pip 安装 OpenAI Python 库:
pip install openai。 - 配置环境变量:在代码中设置 API Key,建议通过环境变量读取以提高安全性。
定义一个通用的调用函数,封装了与模型交互的逻辑。由于 API 版本更新,此处采用较新的调用方式:
import openai
def get_completion(prompt, model='gpt-3.5-turbo'):
messages = [{'role':'user', 'content':prompt}]
response = openai.chat.completions.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message['content']
注意:调用 OpenAI API 通常是收费服务,实际使用时请根据账户余额合理控制调用次数。若用于学习测试,可寻找官方提供的免费额度或课程实例进行验证。
三、使用准则
在使用 Prompt 时,应遵循两条核心准则:第一条是写出清晰而具体的指示,第二条是给模型思考的时间。这两点能显著提高生成内容的质量。
3.1 写出清晰而具体的指示
清晰不意味着简短。在很多情况下,较长的指示能更详细地说明需求,减少歧义。
3.1.1 使用分隔符明确输入部分
使用分隔符将特定的文本片段和提示的其他部分分开,可以是反引号(```)、单引号(''')、破折号(—)、尖括号(< >),也可以是 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.
```{text}```
"""
response = get_completion(prompt)
print(response)
通过分隔符,模型能更准确地识别需要处理的文本范围,避免混淆指令与数据。
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)
这种结构化要求能确保输出符合预期的数据格式,便于后续代码直接解析。
3.1.3 检查模型条件是否得到满足
如果任务中的假设不一定满足,可以先告诉模型这些假设。如果不被满足,指示模型指出这一点,并在完成任务的过程中停止,避免产生幻觉。
示例代码如下:
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)
可以看出此时模型成功判断出了正确与否,避免了直接比较带来的误导。
模型局限性
尽管模型在训练过程中已经接触了大量的知识,但它并没有完美记住所有信息,对自己的知识边界并不十分了解。这意味着它可能会尝试回答一些艰涩的问题,并给出像那么一回事的回答,这种编造的回答称为'幻觉'。在应用时需保持警惕。
四、工程迭代
并不是每次写 Prompt 就能得到很好的结果,就像写代码一样,总是会涉及到迭代优化。通过不断调整指令,逐步逼近理想输出。
例如,要求从产品情况说明书生成一个产品的营销描述。首先提供原始的技术规格表,然后进行第一次尝试转化。如果输出太长,不适合营销文案,则限制字符数。如果关注的信息不够精准,则改进指令,增加目标受众和侧重点的描述。
示例代码如下:
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)
通过限制字数和明确任务,模型能提取核心观点,忽略冗余细节。
六、推理
可以借助大语言模型从文字中实现情感分析、提取名字或其他实体等信息。这属于自然语言处理(NLP)的典型应用场景。
给一个评论如下:
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 自定义一个自己的聊天机器人。这里不再是单一的输入等待输出,而是需要传入一个信息列表,以帮助模型理解自己扮演的角色和历史上下文。
首先需要另外定义一个辅助函数来处理多轮对话:
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
)
return response.choices[0].message["content"]
然后设定传入的信息列表,system 角色就是告诉它设定的是助理,user 和 assistant 角色记录对话历史。
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)
输出为,这就是它作为助理继续回复的话。基于此原理,我们可以构建一个服务于披萨餐厅接受订单的机器人,自动收集用户的订餐需求并作出回应。
首先定义一个收集信息的函数,这样就能自动把信息传给机器人,维护对话状态。
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}"})
# 此处省略 GUI 渲染代码,仅展示逻辑
return pn.Column()
再设置机器人的系统信息,以及设置前台页面。系统信息中定义了机器人的行为准则、菜单价格和处理流程。
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
"""} ]
运行后,即可得到一个基础的对话系统。通过这种方式,开发者可以快速构建垂直领域的智能助手,无需从零训练模型,只需精心设计 Prompt 即可。
十、总结
本文详细介绍了大语言模型提示词工程的基础知识与实践方法。从 Prompt 的定义出发,讲解了环境搭建、编写准则、迭代优化、文本处理及聊天机器人构建等关键环节。通过代码示例演示了如何利用 Prompt 控制模型输出,提升生成质量与准确性。在实际应用中,开发者应结合具体业务场景,灵活运用上述技巧,不断优化交互体验。同时需注意模型局限性,如幻觉问题,保持人工审核机制,确保输出内容的可靠性与安全性。

