ChatGPT提示词工程(一):Guidelines准则

这篇具有很好参考价值的文章主要介绍了ChatGPT提示词工程(一):Guidelines准则。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、说明

这是吴恩达 《ChatGPT Prompt Engineering for Developers》 的课程笔记系列。
本文是第二讲的内容:Guidelines
课程主讲:Andrew Ng,Isa Fulford
Isa Fulford也是《OpenAI Cookbook》的主要贡献者之一

二、安装环境

1. 下载openai

pip install openai 

如果是在jupyter notebook上安装,前面需要带英文感叹号(!),之后需要执行bash指令的地方都这样

!pip install openai 

2. 导入openai,并配置openai api key

import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key = os.getenv('OPENAI_API_KEY')

这里的OPENAI_API_KEY是设置在环境变量里的值,为你的openai api key
设置环境变量bash指令:

!export OPENAI_API_KEY='sk-...'		

或者,在代码中直接这样写:

openai.api_key = 'sk-...'    

3. 辅助函数
调用openai接口

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"]


三、Guidelines

准则一:写出明确而具体的说明

方法1:使用分隔符清楚地表示输入的不同部分

分隔符可以是:

 ```,  """,  < >,  <tag> </tag>,  :   

它可以防止prompt注入,以免给模型产生混乱的理解

例子:

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)

{text} 就是使用符合 ```分隔的内容
以上代码输出的结果为,打印的一句话总结的text的结果:
ChatGPT提示词工程(一):Guidelines准则

方法2:用结构化输出:如直接要求它以HTML或者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)

代码中,要求生成三个虚拟的图书,以JSON格式输出,运行结果:
ChatGPT提示词工程(一):Guidelines准则

方法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的内容是泡一杯茶的步骤,prompt要求模型理解这段内容,告知是否能把它分解成一步一步的步骤的结构,如果能,则按照步骤描述重写,如果不能则给出则返回No steps provided。代码输出结果:
ChatGPT提示词工程(一):Guidelines准则
下面这个例子则给出的是不能分解成步骤的一段话:

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)

ChatGPT提示词工程(一):Guidelines准则

方法4:Prompt中包含少量样本

例子:

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)

代码中,给了一个child和grandparent对话的样本,要求再次按照这个样本给出grandparent的答复,运行结果:
ChatGPT提示词工程(一):Guidelines准则

https://blog.csdn.net/Jay_Xio/article/details/130450026



准则二:给模型一些思考的时间

方法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 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)

代码中,prompt给出模型要执行任务的步骤
步骤1,用一句话总结text内容
步骤2,翻译成法语
步骤3,列出名字
步骤4,以JSON格式输出
执行代码,模型按照这个步骤输出:
ChatGPT提示词工程(一):Guidelines准则

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

ChatGPT提示词工程(一):Guidelines准则

方法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)

代码中,要求模型判断学生的结题是否正确,运行结果:
ChatGPT提示词工程(一):Guidelines准则
明显第3部,MainTenance const: 应该是 100000 + 10x,而学生给出的是错误的,模型没有判断出这个步骤有误,因为它只判断了 Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000 是正确的,就给出了正确的结论。

下面的示例,要求模型先自己按照步骤一步一步给出解题步骤,然后再判断学生的解题步骤是否正确:

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)

运行结果: 给出了正确解法,并判断学生的是错误的
ChatGPT提示词工程(一):Guidelines准则

https://blog.csdn.net/Jay_Xio/article/details/130450026



四、模型的限制

prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)

要求模型介绍Boie这个公司的电动牙刷,其实这个公司不存在,产品也不存在,但是模型会煞有其事的介绍
ChatGPT提示词工程(一):Guidelines准则
这种模型的限制,称为模型的幻觉。

要减少这种幻觉,需要模型先从文本中找到任何相关的引用,然后请它使用这些引用来回答问题,并且把回答追溯到源文件 文章来源地址https://www.toymoban.com/news/detail-439159.html

到了这里,关于ChatGPT提示词工程(一):Guidelines准则的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 学习笔记:吴恩达ChatGPT提示工程

    以下为个人笔记,原课程网址Short Courses | Learn Generative AI from DeepLearning.AI 1.1 基础LLM 输入 输出 输入 输出 之所以这样输出的原因是,基础LLM的输出基于它的训练数据,可能会产生我们不想得到的信息 1.2 指令微调型LLM 输入 输出 首先使用1.1中经过大量文本数据训练过的基础L

    2024年02月08日
    浏览(48)
  • 与ChatGPT更高效的沟通:提示工程

    芝士AI吃鱼 随着生成式人工智能(尤其是 ChatGPT)的爆炸性普及,提示对于人工智能领域的人们来说已成为一项越来越重要的技能。制作提示,即与 ChatGPT 等大型语言模型 (LLM) 交互的机制,并不是乍看起来那么简单的语法任务。在第一次与 ChatGPT 交流的新鲜感之后,很明显需

    2024年02月12日
    浏览(42)
  • 新的 ChatGPT 提示工程技术:程序模拟

    即时工程的世界在各个层面上都令人着迷,并且不乏巧妙的方法来推动像 ChatGPT 这样的代理生成特定类型的响应。思想链 (CoT)、基于指令、N-shot、Few-shot 等技术,甚至奉承/角色分配等技巧都是充满提示的库背后的灵感,旨在满足各种需求。 在本文中,我将深入研究一项技术

    2024年02月09日
    浏览(34)
  • 【ChatGPT】吴恩达『提示工程』课程完全笔记下载

    版权说明:『ChatGPT Prompt Engineering for Developers』是DeepLearning.AI出品的免费课程,版权属于DeepLearning.AI(https://www.deeplearning.ai/)。 本文是对该课程内容的翻译整理,只作为教育用途,不作为任何商业用途。 吴恩达『开发者的提示工程』课程完全笔记(PDF) :https://github.com/youcan

    2024年02月02日
    浏览(43)
  • AIGC | LLM 提示工程 -- 如何向ChatGPT提问

    当前生成式人工智能已经成为革命性的驱动源,正在迅速地重塑世界,将会改变我们生活方式和思考模式。LLM像一个学会了全部人类知识的通才,但这不意味每个人可以轻松驾驭这个通才。我们只有通过学习面向LLM的提示工程,才可以更好的让LLM成为您的顶级私人助理。 如何

    2024年02月07日
    浏览(57)
  • 给开发者的ChatGPT提示词工程指南

    【中文完整版全9集】第1集 引入-ChatGPT提示词工程师教程 吴恩达xOpenAI官方 【OpenAI官方 | 中文完整版】 吴恩达ChatGPT提示工程师初级到高级(AI大神吴恩达教你写提示词) ChatGPT Prompt Engineering for Development 基础大语言模型和指令精调大语言模型的区别: 指令精调大语言模型经过

    2024年02月09日
    浏览(43)
  • 吴恩达|chatgpt 提示词工程师学习笔记。

    目录 一、提示指南 写提示词的2大原则: 模型的限制 二、迭代 三、总结 四、推断 五、转换 六、扩展 七、对话机器人 吴恩达和openai团队共同开发了一款免费的课程,课程是教大家如何更有效地使用prompt来调用chatgpt,整个课程时长1个半小时,也提供了对应的环境和代码,大

    2024年02月08日
    浏览(55)
  • ChatGPT prompt engineering (中文版)笔记 |吴恩达ChatGPT 提示工程

    出处:https://download.csdn.net/download/weixin_45766780/87746321 感谢中文版翻译https://github.com/datawhalechina/prompt-engineering-for-developers/tree/main/content 国内 == 需要对openapi的endpoint做一个反向代理,并修改本地openai包的源代码== 如下图: completion 原则一:编写清晰、具体的指令 你应该通过提供

    2024年02月03日
    浏览(54)
  • ChatGPT提示词工程(七):Chatbot聊天机器人

    这是吴恩达 《ChatGPT Prompt Engineering for Developers》 的课程笔记系列。 本文是第八讲的内容:Chatbot 参考: ChatGPT提示词工程(一):Guidelines准则 的第二节 其中,辅助函数有变化: 这里,可以自定义消息, message 里面的 role ,可以是 system 、 user 、 assistant : system :系统辅助模

    2024年02月06日
    浏览(46)
  • 提示工程师:如何高效的向ChatGPT提问对话

            最近ChatGPT真的火出圈了,现在打开知乎、博客、抖音,B站都是这方面的信息,ChatGPT相关的信息铺天盖地的袭转而来。对于这种类似新一轮信息技术革命,作为普通人的我们,该如何做呢?这是我们该思考的。         英伟达创始人兼CEO黄仁勋:this is the iph

    2023年04月12日
    浏览(99)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包