OpenAI官方提示词课(一)书写提示词的基本原则

下面是调用openai的completion接口的函数。但在本文中并不是重点。了解一下就好。

import openai
import osfrom dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())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"]

下面我们来说说,书写提示词的基本原则。

提示词的基本原则

  • 提示词的书写要清晰,带有明确的指令
  • 给模型时间去思考,即指明模型的思考过程

原则一:提示词的书写要清晰,带有明确的指令

技巧一:使用分隔符清楚地指示输入的不同部分

分隔符可以是```, “”", < >, , :

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)

回答:

Clear and specific instructions should be provided to guide a model towards the desired output, and longer prompts can provide more clarity and context for the model, leading to more detailed and relevant outputs.

这个例子中需要处理的内容和处理指令是区分开的。这样便于维护。

技巧二:指明固定格式输出结果,如JSON, HTML

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)

回答:

[{"book_id": 1,"title": "The Lost City of Zorath","author": "Aria Blackwood","genre": "Fantasy"},{"book_id": 2,"title": "The Last Survivors","author": "Ethan Stone","genre": "Science Fiction"},{"book_id": 3,"title": "The Secret of the Haunted Mansion","author": "Lila Rose","genre": "Mystery"}
]

技巧三:让模型检测条件是否被满足

示例1:

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)

回答:

Completion for Text 1:
Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Once the water is hot enough, pour it over the tea bag.
Step 4 - Let it sit for a bit so the tea can steep.
Step 5 - After a few minutes, take out the tea bag.
Step 6 - Add some sugar or milk to taste.
Step 7 - Enjoy your delicious cup of tea!

这里设定的条件是,如果文本中有指令步骤就将其按步骤列出。同时,这里也写明了输出的格式,正如技巧二中提到的那样。

示例2:

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)

回答:

Completion for Text 2:
No steps provided.

示例二中的文本没有出现类似指令步骤的文字,模型也正确判断出来了。这说明模型是有推理能力的。

技巧四:少样本学习

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)

回答:

<grandparent>: Resilience is like a tree that bends with the wind but never breaks. It is the ability to bounce back from adversity and keep moving forward, even when things get tough. Just like a tree that grows stronger with each storm it weathers, resilience is a quality that can be developed and strengthened over time.

通过给模型一个示例去学习,让模型在接下来的回答中参照示例来回答。

原则二:给模型时间去思考

技巧一:指定完成任务所需的步骤

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.
"""
# example 1
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)

回答:

Completion for prompt 1:
1 - Siblings Jack and Jill go on a quest to fetch water from a hilltop well, but misfortune strikes as Jack trips and tumbles down the hill, with Jill following suit, yet they return home slightly battered but undeterred in their adventurous spirits. 2 - 兄妹杰克和吉尔出发去从山顶井中取水,但不幸的是,杰克绊倒了,滚下了山坡,吉尔也跟着滚下来,但他们稍微受了点伤,回到家中得到了安慰的拥抱,尽管发生了不幸,他们的冒险精神仍然不减,继续愉快地探索。3 - 杰克,吉尔。4 - 
{"chinese_summary": "兄妹杰克和吉尔出发去从山顶井中取水,但不幸的是,杰克绊倒了,滚下了山坡,吉尔也跟着滚下来,但他们稍微受了点伤,回到家中得到了安慰的拥抱,尽管发生了不幸,他们的冒险精神仍然不减,继续愉快地探索。","num_names": 2
}

模型按照我们制定的步骤输出了内容。

当我们重新制定输出格式

prompt_2 = f"""
Your task is to perform the following actions: 
1 - Summarize the following text delimited by <> 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.Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in Italian summary>
Output JSON: <json with summary and num_names>Text: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)

注意:这里的text指的也是上面例子中的text。

回答:

Completion for prompt 2:
Summary: Jack and Jill go on a quest to fetch water, but misfortune strikes and they tumble down a hill, returning home slightly battered but with undimmed adventurous spirits. 
Translation: Jack 和 Jill 去取水,但不幸的是他们跌倒了,回家时略有些受伤,但他们的冒险精神仍然不减,继续愉快地探索。
Names: Jack, Jill
Output JSON: {"chinese_summary": "Jack 和 Jill 去取水,但不幸的是他们跌倒了,回家时略有些受伤,但他们的冒险精神仍然不减,继续愉快地探索。", "num_names": 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)

回答:

The student's solution is correct.

这里学生的回答是错误的。模型却判断为正确。看样子它算数是真不好。

补救措施来了。先让模型自己找出一个方案,然后让它去和学生的解决方案进行对比。最后判断一下学生的方案是否正确。

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. 
- 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)

回答:

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 + 10xTotal cost: 100x + 250x + 100,000 + 10x = 360x + 100,000Is the student's solution the same as actual solution just calculated:
NoStudent grade:
Incorrect

通过这么一顿操作,模型似乎领悟到了什么。它发现了学生在Maintenance cost中错误地将10x写成了100x

总结一下该原则,通过设置思考过程,让模型按步骤思考通常能得到更为准确的回答。

大语言模型的限制

prompt = f"""
介绍一下首飞牌电动牙刷的功能
"""
response = get_completion(prompt)
print(response)

回答:

首飞牌电动牙刷是一款高科技的电动牙刷,具有多种功能,包括:1. 高速震动清洁:首飞牌电动牙刷采用高速震动技术,每分钟可达到40000次的震动频率,能够深入清洁牙齿,有效去除牙菌斑和牙渍。2. 智能感应:该电动牙刷配备了智能感应技术,能够自动识别不同的牙齿区域,根据不同的清洁需求进行智能调节,让你的牙齿更加干净。3. 多种清洁模式:首飞牌电动牙刷提供了多种清洁模式,包括标准模式、深层清洁模式、按摩模式等,可以根据个人需求进行选择。4. 长续航时间:该电动牙刷采用高效节能的电池技术,一次充电可以使用长达30天,让你无需频繁充电。5. 人性化设计:首飞牌电动牙刷采用人性化设计,握感舒适,操作简单,适合各种年龄段的人使用。总之,首飞牌电动牙刷具有高速震动清洁、智能感应、多种清洁模式、长续航时间和人性化设计等多种功能,是一款高性能的电动牙刷,可以帮助你轻松保持口腔健康。

事实上,并没有首飞牌电动牙刷,但模型描述的挺欢快的。一本正经地胡编乱造。这就是模型表现出来的幻想。要尽量避免这种情况,就需要将提示词写的准确,清晰和指令化。

参考:

https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/2/guidelines


觉得有用就点个赞吧!

我是首飞,做有趣的事情,拿出来分享。

我也准备了一份提示词的文档。有涉及到各个领域的提示词模板。
在这里插入图片描述
您可以在《首飞》公众号中回复“ 提示词 ” 获取该文档。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/12325.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

美容行业的翻身仗:ChatGPT带你玩转营销活动

&#xff08;今天给大家带来一个 用ChatGPT给美容院、美容店、美容行业的商家 出一个营销策划活动方案的实例&#xff09; 在这个竞争激烈的美容行业&#xff0c;想要脱颖而出&#xff0c;你得有点与众不同的手段。别再拿那些陈词滥调的营销策略来忽悠我了&#xff0c;今天我要…

ChatGPT热度不减!华为宣布入局,盘古GPT能否大杀四方!

ChatGPT热度不减 六月份了&#xff0c;朋友们&#xff0c;来到六月份了已经&#xff0c;ChatGPT的热度依旧不减&#xff0c;各大论坛网站的榜单上还飘着ChatGPT相关话题的文章&#xff0c;且排名靠前。由此可见&#xff0c;这ChatGPT这股子热潮还得持续一段时间呢。 而且ChatG…

midjourney最新使用方法教程指令关键词

自ChatGPT走红以来&#xff0c;以“聊天”为核心的人工智能协作工具们受到了各行各业的关注&#xff0c;让AI写首诗、发封邮件&#xff0c;或是做一份计划书&#xff0c;这些工作已经成为AI聊天机器人的“日常需求”。但似乎&#xff0c;人们遗忘了AI还具有创作文字以外的内容&…

如何复刻Midjourney的成功?

Midjourney是如何火起来的&#xff1f; AI绘画的大模型和应用非常多&#xff0c;但最有名的非Stable Diffusion和Midjourney莫属&#xff0c;其中&#xff0c;尤其是Midjourney(以下简称MJ)&#xff0c;仅11位成员&#xff0c;8个研发人员中的一半都是尚未毕业的本科生,从未融…

DNSPod十问周艺超:AI如何成为当代毕加索?

本期嘉宾 周艺超 腾讯QQ影像中心工程师 周艺超&#xff0c;腾讯QQ影像中心资深应用开发工程师。入职腾讯后负责超级QQ秀的智能捏脸相关的研发工作&#xff0c;2022年开始负责小世界AIGC相关的技术研发和产品落地&#xff0c;主要负责《异次元的我》、《魔法画室》等活动的算法…

那年马斯克在苏联

丰色 萧箫 发自 凹非寺量子位 | 公众号 QbitAI 这两天&#xff0c;一批上世纪照片被解禁&#xff0c;当年不为人知的真相浮出水面。 传闻来美国前&#xff0c;马斯克曾是一名苏联工程师。 他贿赂当地的社会名流&#xff0c;并对外自称是Yaroslavl汽车厂的创始人。 彼时在国内&a…

AIGC:【LLM(二)】——LangChain:由LLMs驱动的应用开发框架

文章目录 一.背景介绍二.LangChain简介2.1 常见应用场景 三.LangChain特点3.1 优点3.2 不足 四.LangChain功能4.1 基础功能4.2 功能模块4.2.1 LLM和Prompts4.2.2 Chain4.2.3 Agent4.2.4 Memory4.2.5 Embedding4.2.6 Models4.2.7 Indexes 五.实战案例5.1 背景需求5.2 数据准备5.…

SEO 已死,LLMO 万岁

“北风那个吹&#xff0c;雪花那个飘”&#xff0c;我手捧一杯咖啡&#xff0c;听着白毛女。朋友坐在对面高谈阔论&#xff1a;“有了 ChatGPT&#xff0c;我再也不缺内容了&#xff0c;SEO 的春天就要来了&#xff01;” 然而他没有看到真相是 —— ChatGPT 直接颠覆了 SEO 行…

道阻且长,未来可期,从GPT-4窥得通用人工智能时代的冰山一角!

大家这两天是不是又被满屏的ChatGPT相关的文章信息给轰炸得不轻&#xff0c;说实话&#xff0c;我真的对ChatGPT的热度如此经久不衰这个问题非常感兴趣。从去年刚面世时&#xff0c;小范围内造成的行业震荡&#xff0c;到今年二月份铺天盖地得铺舆论造势&#xff0c;引发全民热…

【GPT4】微软 GPT-4 测试报告(1)总体介绍

欢迎关注【youcans的AGI学习笔记】原创作品&#xff0c;火热更新中 微软 GPT-4 测试报告&#xff08;1&#xff09;总体介绍 微软 GPT-4 测试报告&#xff08;2&#xff09;多模态与跨学科能力 微软 GPT-4 测试报告&#xff08;3&#xff09;编程能力 微软 GPT-4 测试报告&…

【GPT4】微软 GPT-4 测试报告(2)多模态与跨学科的组合

欢迎关注【youcans的AGI学习笔记】原创作品&#xff0c;火热更新中 微软 GPT-4 测试报告&#xff08;1&#xff09;总体介绍 微软 GPT-4 测试报告&#xff08;2&#xff09;多模态与跨学科能力 微软 GPT-4 测试报告&#xff08;3&#xff09;编程能力 微软 GPT-4 测试报告&…

第四次工业革命的里程碑-chatgpt

文章目录 一、 介绍二、 训练数据方法、数据来源三、 能帮你做什么做表格论文降重写文案、周报写代码改bug写注释写作业制作游戏策划方案 四、 搭建自己的chatgpt方法五、 安全、安全试用chatgpt的方法六、 几款类似chatgpt的工具七、 优点八、 缺点九、下一步的期待十、 总结 …

OpenAI-ChatGPT最新官方接口《文本交互》全网最详细中英文实用指南和教程,助你零基础快速轻松掌握全新技术(一)(附源码)

Text completion 文本交互 前言Introduction 导言Prompt design 提示设计Basics基础知识TroubleshootingClassificationImproving the classifiers efficiency 提高分类器的效率Generation 总结Conversation 对话Transformation 变化Translation 翻译Conversion 转化Summarizat…

如何用ChatGPT搭建品牌文本体系?(品牌名+slogan+品牌故事)

【ChatGPT】前些天发现了一个巨牛的人工智能学习电子书&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;无广告&#xff0c;忍不住分享一下给大家。&#xff08;点击查看学习资料&#xff09; 该场景对应的关键词库&#xff08;26个&#xff09;&#xff1a; 品牌名、奶…

【CV作业04】图像分割+UI

目录 作业要求 segment.py 人工阈值法 聚类法&#xff0c;半自动阈值分割 OSTU&#xff0c;全自动阈值确定 yen分割 ui_sys.py start.py 作业要求 实现yen方法&#xff1a;50分 &#xff08;直接调用库最多25分&#xff09; 实现聚类法&#xff1a;10分 实现ostu&#…

【AI理论学习】深入理解Prompt Learning和Prompt Tuning

深入理解Prompt Learning和Prompt Tuning 背景Prompt Learning简介1. Prompt是什么&#xff1f;2. 为什么要使用Prompt&#xff1f;3. Prompt Learning的形式&#xff08;举例&#xff09;4. 有哪些Pre-training language model&#xff1f;5. 常见的Prompt Learning的方法 Pro…

行业报告 | 2022文化科技十大前沿应用趋势(上)

文 | BFT机器人 前言 Introduction 文化科技是文化科技融合过程中诞生的系列新技术成果&#xff0c;是文化强国和科技强国两大战略的交又领域。2012 年 8月&#xff0c;科技部会同中宣部、财政部、文化部、广电总局、新闻出版总署发布《文化科技创新工程纲要》&#xff0c;开启…

灵活利用ChatAI,帮助你编写文章/故事,提高创作

前言 ChatAI 对于编写文章/故事提供了广泛且有益的帮助。它可以激发灵感、提供背景知识、检查错误并提供实时反馈等功能&#xff0c;在创作过程中起到了重要辅助作用。然而&#xff0c;在使用过程中需要保持适度&#xff0c;并将其视为一个有益工具而非完全取代人类创作的替代…

26岁财富自由,30岁已经历成功人士的一生,OpenAI掌门人的36条人生经验

Altman30岁就经历了成功人士的一生:程序员、创始人、投资人、CEO、慈善家。 编辑丨沃特敦 OpenAI掌门人Sam Altman是美国现在最炙手可热的企业家。上周,Altman刚刚度过38岁生日。今天分享的是他30岁时总结的人生经验。那时,Altman已经是硅谷最具人气的创投明星,年纪轻轻就能…

chatgpt赋能python:Python整人代码:开发有趣的恶作剧工具

Python整人代码&#xff1a;开发有趣的恶作剧工具 Python是一种高级编程语言&#xff0c;它有着众多功能库和API&#xff0c;能够用于各种不同的领域。但是&#xff0c;Python也可以用来编写有趣的恶作剧代码&#xff0c;搞乐一下&#xff01;在这篇文章中&#xff0c;我们将介…