一,什么是Prompt
在人工智能领域,Prompt指的是用户给大型语言模型发出的指令。作用是引导模型生成符合预期主题或内容的文本,从而控制生成结果的方向和内容。
大模型是根据用户提出的问题来输出下文,所以用户提出的问题的质量也就一定程度影响着模型的输出。
由此就引发了一个新的学科,叫做提示工程(Prompt Engineering),用户可以通过提示工程来提高大语言模型的安全性,也可以赋能大语言模型。
二,前期准备
申请OpenAI API,[申请地址]
电脑安装jupyter notebook,可使用pip安装
新建一个专门用来存放文件地址的文件夹
win+r打开运行,+cmd进入终端运行
进入对应文件夹,并打开jupyter notebook
安装OpenAI python库,可使用pip install openai
调用openai库,设置key
定义一个调用gpt3.5的模型的问答函数(由于他们api在之后进行了更新,所以此处调用的代码我改了下
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']
我在自己终端跑的时候,发现调用openapi是需要收费的,所以我找到他们课程里面的实例去跑看的效果,具体课程地址见【一,什么是Prompt】中课程链接
二,使用准则
在使用prompt的时候要遵循两条准则,第一条是写出清晰而具体的指示,第二条是给模型思考的时间
2.1 写出清晰而具体的指示
清晰不意味着简短,在很多的情况下,较长的指示能更详细的说明
2.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. Don't confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
"""#用```将输入的部分分开
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
```{text}```
"""response = get_completion(prompt)
print(response)
输出为
2.1.2 使用结构化的输出
为了使传递模型的输出更容易,可使用如HTML或者JSON这样的结构化输出
如下代码
# 用带book_id, title, author, genr的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)
输出结果如下
2.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. If you \
like, you can add some sugar or milk to taste. \
And that's it! You've got yourself a delicious \
cup of tea to enjoy.
"""#提示为你将获得有三个引号括起来的文本,如果包含指令,就以下面格式来重写;不包含,就只写出没有提供的步骤
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)
输出为
而如果我们用一段不包含步骤的句子去尝试,代码如下
text_2 = f"""
The sun is shining brightly today, and the birds are \
singing. It's a beautiful day to go for a \
walk in the park. The flowers are blooming, and the \
trees are swaying gently in the breeze. People \
are out and about, enjoying the lovely weather. \
Some are having picnics, while others are playing \
games or simply relaxing on the grass. It's a \
perfect day to spend time outdoors and appreciate the \
beauty of nature.
"""
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_2}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 2:")
print(response)
输出为
2.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)
输出为
2.2 给模型思考的时间
在有些时候,如果直接要求模型快速回答问题,可能会给出质量不高的回答,可以要求模型有一连串的推理,然后再给出答案。所以可以指示模型对一个问题进行更长时间的思考
2.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. \
Though slightly battered, the pair returned home to \
comforting embraces. Despite the mishap, \
their adventurous spirits remained undimmed, and they \
continued exploring with delight.
"""# 要求按照如下方式输出
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)
输出为
2.2.2 指示模型在急于得出结论之前找出自己的解决方法
例如下面案例中
prompt = f"""
Determine if the student's solution is correct or not.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
"""
response = get_completion(prompt)
print(response)
输出的结果为
但其实是错误的,因为维护费用只有10美元每平方英寸$10 / square
这个时候,可以让模型先给出自己的解决方案,然后再和学生的这个方案对比来解决问题
代码如下
#因为和markdowm语法的冲突,```写成了\```#首先很明确的告诉模型第一步是找到自己的解决方案,然后再进行比较
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 \
just 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,
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 foamCOUNTRY 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.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 itemThe 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)
输出为
五,文本转换
使用大语言模型进行文本转换,如语言翻译、拼写等
5.1 翻译
prompt = f"""
Translate the following English text to Spanish: \
```Hi, I would like to order a blender```
"""
response = get_completion(prompt)
print(response)
输出为
5.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)
输出为
5.3 格式转换
进行格式之间的转换
data_json = { "resturant employees" :[ {"name":"Shyam", "email":"shyamjaiswal@gmail.com"},{"name":"Bob", "email":"bob32@gmail.com"},{"name":"Jai", "email":"jai87@gmail.com"}
]}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)from IPython.display import display, Markdown, Latex, HTML, JSON
display(HTML(response))
输出为
六,文本扩展
可以将要求文本扩展成需要的文本,如根据每个用户的评论自定义单独的回复邮件
设定客户评论如下
# given the sentiment from the lesson on "inferring",
# and the original customer message, customize the email
sentiment = "negative"# review for a blender
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 (example, I crush \
very hard items like beans, ice, rice, etc. in the \
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""
进行扩展
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)
# print(str(response.choices[0].message))return response.choices[0].message["content"]
然后设定传入的信息列表,system就是告诉它设定的是助理
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_inputinp.value = ''context.append({'role':'user', 'content':f"{prompt}"})response = get_completion_from_messages(context) context.append({'role':'assistant', 'content':f"{response}"})panels.append(pn.Row('User:', pn.pane.Markdown(prompt, width=600)))panels.append(pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))return pn.Column(*panels)
再设置机器人的系统信息,以及设置前台页面
import panel as pn # GUI
pn.extension()panels = [] # collect display 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 \
"""} ] # accumulate messagesinp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")interactive_conversation = pn.bind(collect_messages, button_conversation)dashboard = pn.Column(inp,pn.Row(button_conversation),pn.panel(interactive_conversation, loading_indicator=True, height=300),
)dashboard
运行后,即可得到如下的对话系统
然后我稍微对话了一下,如下所示
大模型&AI产品经理如何学习
求大家的点赞和收藏,我花2万买的大模型学习资料免费共享给你们,来看看有哪些东西。
1.学习路线图
第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;
第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;
第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;
第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;
第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;
第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;
第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。
2.视频教程
网上虽然也有很多的学习资源,但基本上都残缺不全的,这是我自己整理的大模型视频教程,上面路线图的每一个知识点,我都有配套的视频讲解。
(都打包成一块的了,不能一一展开,总共300多集)
因篇幅有限,仅展示部分资料,需要点击下方图片前往获取
3.技术文档和电子书
这里主要整理了大模型相关PDF书籍、行业报告、文档,有几百本,都是目前行业最新的。
4.LLM面试题和面经合集
这里主要整理了行业目前最新的大模型面试题和各种大厂offer面经合集。
👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;
• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;
• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;
• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。
1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集
👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓