吴恩达 Chatgpt prompt 工程--1.Guidelines

课程链接

Setup

#安装
!pip install openai
#设置key
!export OPENAI_API_KEY='sk-...'
# or
#import openai
#openai.api_key = "sk-..."
import openai
import osfrom dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())openai.api_key  = os.getenv('OPENAI_API_KEY')
# https://platform.openai.com/docs/guides/chat
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"]

Prompting 原则

  • 原则1: Write clear and specific instructions(写清楚具体的说明)
  • 原则 2: Give the model time to “think”(给模型时间“思考”)

策略(原则1)

策略 1: Use delimiters to clearly indicate distinct parts of the input(使用分隔符清楚地指示输入的不同部分)

分隔符可以是任何符号,比如

```, """, < >, <tag> </tag>, :
ext = 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.
策略 2: Ask for a structured output(要求结构化输出)
  • 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 Life of Bees","author": "Lila Rose","genre": "Romance"}
]
策略 3: Ask the model to check whether conditions are satisfied(让模型检查条件是否满足)
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!
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.
策略 4: “Few-shot” prompting
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)
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)
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.

策略(原则2)

策略 1: Specify the steps required to complete a task(指定完成任务所需的步骤)
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 French.
3 - List each name in the French summary.
4 - Output a json object that contains the following \
keys: french_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:
Two siblings, Jack and Jill, go on a quest to fetch water from a hilltop well, but misfortune strikes as they both fall down the hill, yet they return home slightly battered but with their adventurous spirits undimmed.Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits au sommet d'une colline, mais ils tombent tous les deux et retournent chez eux légèrement meurtris mais avec leur esprit d'aventure intact. 
Noms: Jack, Jill.{
"french_summary": "Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits au sommet d'une colline, mais ils tombent tous les deux et retournent chez eux légèrement meurtris mais avec leur esprit d'aventure intact.",
"num_names": 2
}
Ask for output in a specified format(要求以指定格式输出)
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 French.
3 - List each name in the French summary.
4 - Output a json object that contains the following keys: french_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)
Completion for prompt 2:
Summary: Jack and Jill go on a quest to fetch water, but misfortune strikes and they tumble down the hill, returning home slightly battered but with their adventurous spirits undimmed. 
Translation: Jack et Jill partent en quête d'eau, mais un malheur frappe et ils tombent de la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.
Names: Jack, Jill
Output JSON: {"french_summary": "Jack et Jill partent en quête d'eau, mais un malheur frappe et ils tombent de la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.", "num_names": 2}
策略 2: Instruct the model to work out its own solution before rushing to a conclusion(指导模型在匆忙得出结论之前制定自己的解决方案)
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.

请注意,学生的解决方案实际上是不正确的。

我们可以通过指示模型首先制定自己的解决方案来解决这个问题。
在这里插入图片描述

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

模型限制:一本正经的胡说八道(Hallucinations)

  • Boie 是一家真实存在的公司,但是产品不是真的
prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)
The AeroGlide UltraSlim Smart Toothbrush by Boie is a high-tech toothbrush that uses advanced sonic technology to provide a deep and thorough clean. It features a slim and sleek design that makes it easy to hold and maneuver, and it comes with a range of smart features that help you optimize your brushing routine.One of the key features of the AeroGlide UltraSlim Smart Toothbrush is its advanced sonic technology, which uses high-frequency vibrations to break up plaque and bacteria on your teeth and gums. This technology is highly effective at removing even the toughest stains and buildup, leaving your teeth feeling clean and refreshed.In addition to its sonic technology, the AeroGlide UltraSlim Smart Toothbrush also comes with a range of smart features that help you optimize your brushing routine. These include a built-in timer that ensures you brush for the recommended two minutes, as well as a pressure sensor that alerts you if you're brushing too hard.Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie is a highly advanced and effective toothbrush that is perfect for anyone looking to take their oral hygiene to the next level. With its advanced sonic technology and smart features, it provides a deep and thorough clean that leaves your teeth feeling fresh and healthy.

PS

使用反斜杠使文本适合屏幕,而不插入换行符“\n”。
无论是否插入换行符,GPT-3都不会真正受到影响。但是,在一般使用LLM时,可能会考虑提示中的换行符是否会影响模型的性能。

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

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

相关文章

使用 ChatGPT 生成代码的提示

ChatGPT 是一个真正的超能力&#xff0c;不仅在编写方面&#xff0c;而且在生成代码方面。开始使用 ChatGPT 很简单&#xff0c;但如果你想充分利用它&#xff0c;那么你需要学习如何制作好的提示。 提示是您为 ChatGPT AI 模型提供的说明或讨论主题&#xff0c;用于响应。它可…

招聘海报制作可以用什么软件,有什么免费模板?

眼看六月毕业季就来了&#xff0c;对于企业来说无疑是新的招聘黄金时间&#xff0c;制作一张合格的招聘海报尤为重要。一张专业的招聘海报所用的软件最佳推荐无疑就是photoshop了&#xff0c;但对于大多数人来说&#xff0c;photoshop并非一朝一夕就能熟练运用的。 因此易图网…

推荐几个好用的文字生成创意绘画软件,帮你轻松拓展创意

如果我们是一位设计师&#xff0c;可能需要为客户制作一些创意图画&#xff0c;但是如果没有足够的绘画技巧和经验&#xff0c;那么我们长时间的进行绘画创作可能不仅体现不出创意&#xff0c;还不能够让客户满意。而且有些时候会觉得自己的绘画风格比较的局限&#xff0c;难以…

4款超实用绘图软件

对于新手来说&#xff0c;很多人认为绘图软件需要一定基础的设计功底&#xff0c;但其实也不完全是&#xff0c;在网上有很多非常实用的绘图软件可以迅速的帮助我们解决工作中的绘图问题&#xff0c;以下4款是这两年我的小伙伴们用的最多的&#xff0c;推荐给大家。 亿图图示 …

这 7 款程序员免费在线画图工具,贼好用!

点击关注公众号&#xff1a;互联网架构师&#xff0c;后台回复 2T获取2TB学习资源&#xff01; 上一篇&#xff1a;Alibaba开源内网高并发编程手册.pdf 都说一图胜千言&#xff0c;一个程序员如果能画的一手好图&#xff0c;无论是在产品分析、方案选项、还是技术交流&#xff…

智能写作软件-免费智能写作文章内容软件

智能写作软件&#xff0c;什么是智能写作软件。随着互联网的发展&#xff0c;人工智能的崛起市面上出现了不少的智能写作软件&#xff0c;但是不少的智能写作软件的原创度基本都低于百分之30&#xff0c;今天给大家分享一款免费的人工智能写作软件&#xff0c;支持自动全网采集…

推荐一款免费在线高效作图工具

作者&#xff1a; kim 来源&#xff1a; kimshareclub微信公众号 今天要分享的这款工具那个叫厉害了&#xff0c;从标题就可以看得出来&#xff0c;这也是目前最走心的标题了。为了博取一点流量也是各种操碎了心&#xff0c;希望大家纯粹是因为看到标题而进来的。说句实在话&a…

独家 | AI教父Geoffery Hinton:我开发的技术,为什么现在让我如此害怕

作者&#xff1a;Will DOuglas Heaven翻译&#xff1a;殷之涵 校对&#xff1a;孙韬淳本文约4500字&#xff0c;建议阅读9分钟 本文为你分享 AI 教父在谷歌工作了十年之后决定辞职的原因。 那天&#xff0c;我在Geoffrey Hinton的家中&#xff08;位于北伦敦的一条漂亮街道&am…

【人工智能】AI 教父 Hinton MIT 万字访谈: 人类 可能只是 AI 演化过程中的一个过渡阶段

目录 离开谷歌是为了向世人警醒AI风险 离开谷歌的原因: GPT-4已经具

苹果公司注册成立 | 历史上的今天

整理 | 王启隆 透过「历史上的今天」&#xff0c;从过去看未来&#xff0c;从现在亦可以改变未来。 今天是 2023 年 1 月 3 日&#xff0c;在 2001 年的今天&#xff0c;英特尔发布 Intel 1.3GHz Pentium 4 处理器&#xff1b;英特尔的 Pentium 4 系列曾在最初发布时表现令人失…

颠覆美国科技界的华裔天才出生 | 历史上的今天

整理 | 王启隆 透过「历史上的今天」&#xff0c;从过去看未来&#xff0c;从现在亦可以改变未来。 今天是 2023 年 2 月 7 日&#xff0c;在 1834 年的今天&#xff0c;元素周期表的发现者德米特里门捷列夫诞辰。他的名著、伴随着元素周期律而诞生的《化学原理》&#xff0c;在…

【温故而知新】阶段总结!我在技术成长过程中的收获!

时间&#xff1a;2023年05月31日 作者&#xff1a;小蒋聊技术 邮箱&#xff1a;wei_wei10163.com 微信&#xff1a;wei_wei10 【20230531】【温故而知新】阶段总结&#xff01;我在技术成长过程中的收获&#xff01;_小蒋聊技术_免费在线阅读收听下载 - 喜马拉雅手机版欢迎…

阻碍国内团队研究 ChatGPT 这样产品的障碍有哪些,技术,钱,还是领导力?

夕小瑶(机器学习优秀答主&#xff09;回答&#xff1a; 我觉得最大的障碍在于文化&#xff0c;或者说意识。 据我观测&#xff0c;每当国外有新的技术突破的时候&#xff0c;咱们国内手握大量资源的团队第一反应往往是—— 咱们赶紧做个中文版的出来&#xff01;咱们要做出中国…

chatgpt赋能python:恶搞Python程序代码:让编程更有趣

恶搞Python程序代码&#xff1a;让编程更有趣 Python是一种高级编程语言&#xff0c;可以用于解决各种问题&#xff0c;从科学计算到网站开发。但是&#xff0c;Python程序并不一定总是严肃而无趣的。在本文中&#xff0c;我们将展示一些恶搞Python程序代码&#xff0c;这些代…

上海AI lab提出VideoChat:可以与视频对话啦

夕小瑶科技说 原创 作者 | 小戏、ZenMoore 视频相比语言、图像&#xff0c;是更复杂更高级的一类表征世界的模态&#xff0c;而视频理解也同样是相比自然语言处理与计算机视觉的常见工作更复杂的一类工作。在当下大模型的洪流中&#xff0c;自然而然的想法就是大规模语言模型…

ChatGPT 整合到Laravel项目中使用

之前写的《ChatApI 本地如何调用》是直接下载ChatGPT SDK当做单独项目开发使用的&#xff0c;这次写的是整合到当前项目中使用ChatGPT 合并为一个项目去使用&#xff1b;具体操作方法也有一定区别。 目录 1.安装ChatGPT SDK 2.加载ChatGPT类库 3.安装guzzlehttp 4.代码实操…

Midjourney绘画2尾图,是什么体验

​ 引言&#xff1a; 你可能已经见过很多普通的二维码&#xff0c;它们通常是黑白的方块&#xff0c;用来存储一些链接或信息。但是&#xff0c;你有没有想过&#xff0c;二维码也可以变成艺术品呢&#xff1f;上面这张图看起来很普通&#xff0c;但是当你用手机扫描它时&…

对话人工智能 |新时代AI如何“落地“

前言&#xff1a; Comate代码助手推出&#xff0c;现场生成了贪吃蛇游戏&#xff0c;我们距离AI自动编程还有多远&#xff1f; 文章目录 序章正文背景基础坚实文心大模型飞浆深度学习框架 Comate的出现优质的智能助理和伙伴多场景适用优势特征Demo演示视频&#xff1a; 总结 序…

对标微软?我们上手试了试 WPS AI。

微软的 Copilot 来了, “ 其他办公软件们 ” 得咋办啊&#xff1f; 咱说实话&#xff0c;前不久微软发布 Microsoft 365 Copilot 时&#xff0c;差评君还真想过这个问题。 毕竟微软的那波升级&#xff0c;怎么看&#xff0c;都像在办公软件圈儿献上了一记绝杀&#xff0c;直接…

图文实录|澜舟科技合伙人李京梅:基于预训练模型的 AIGC 技术与应用实践

2023年1月6日&#xff0c;由稀土掘金技术社区与 Intel 联合发起的第一届「掘金未来大会」在北京成功举行。 大会上&#xff0c;澜舟科技合伙人、首席产品官李京梅介绍了澜舟科技的孟子轻量化预训练模型&#xff0c;及其已经在 GitHub、 Hugging Face 以及 ModelScope 等多个社…