Openai+Coursera: ChatGPT Prompt Engineering(一)

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

今天我学习了DeepLearning.AI的 Prompt Engineering 的在线课程,我想和大家一起分享一下该门课程的一些主要内容。

下面是我们访问大型语言模(LLM)的主要代码:

import openai

openai.api_key  ='XXXXXXXXX'

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, 
    )
    return response.choices[0].message["content"]

提示词原则(Prompting Principles)

  • 原则1:写出清晰和具体的说明(Write clear and specific instructions)

  • 原则2:给模型时间“思考”(Give the model time to “think”)

策略

策略 1:使用分隔符清楚地指示输入的不同部分
分隔符可以是:```, """, < >, <tag> </tag>, :

对策略1的说明:

策略1的意思是:如果我们需要chatgpt完成某个特定任务,那么我们要在prompt里面明确的告诉ChatGPT需要做的事情是什么以及所需要的素材的位置。比如我们要chatGPT对一段文本内容做一个总结或摘要,那么我们就需要在prompt中指明:1.需要gpt完成的任务是什么, 2. 任务所需的素材在哪里。

下面的代码演示的是让gpt对一段特定文本内容做摘要, 在定义prompt时我们需要用特定的分隔符来分隔需要做摘要的文本内容,这样做的目的是让gpt知道文本内容所在位置。

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)

Openai+Coursera: ChatGPT Prompt Engineering(一)

 策略 2:可以要求GPT给出一个结果化的结果

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

Openai+Coursera: ChatGPT Prompt Engineering(一)

 策略3:要求模型检查条件是否满足

这里我们要求gpt去检查一段文本的内容是否满足特定的条件(步骤),如果满足则按条件输出符合条件的内容,如果不满足条件则输出:不符合条件。

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)

Openai+Coursera: ChatGPT Prompt Engineering(一)

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)

Openai+Coursera: ChatGPT Prompt Engineering(一)

 上述两个prompt中第一个prompt的内容符合特定的条件,所以结果按条件输出结果,第二个prompt不符合条件,所以输出: No steps provided.

策略 4:“Few-shot”提示词

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要求ChatGPT以一致的风格来回答问题,在prompt中给出来child和grandparent的一轮对话内容作为一个例子,这样做的目的是让ChatGPT明白所谓的“consistent style”是怎么一回事,prompt的最后是child的一个问题,但是没有grandparent的回答。这是让ChatGPT明白要做的事情是:充当grandparent角色来回答child的最后一个问题,并且风格要和前面的grandparent的风格保存一致。

下面是ChatGPT返回的结果:

Openai+Coursera: ChatGPT Prompt Engineering(一)

 原则 2:给模型时间“思考”

策略 1:指定完成任务所需的步骤

这里我们会给模型一段文本,然后要求模型按指定的步骤来完成一个任务:

执行以下操作:
1 - 用 1 个句子总结以下用三重反引号分隔的文本。
2 - 将摘要翻译成法语。
3 - 在法语摘要中列出每个名字。
4 - 输出包含以下键的 json 对象:french_summary、num_names。

用换行符分隔你的答案。

ext = 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)

Openai+Coursera: ChatGPT Prompt Engineering(一)

 要求指定格式的输出

这里我们要求ChatGPT按指定的格式来输出内容,在prompt中的内容分成了2部分,上半部分告诉ChatGPT需要做哪几件事情,下半部分输出的格式:

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)

Openai+Coursera: ChatGPT Prompt Engineering(一)

 策略 2:在匆忙下结论之前指示模型自己制定解决方案

这里在prompt中给ChatGPT出了一道学生做的简单的数学应用题,希望ChatGPT来判断学生的答案是否正确。对于ChatGPT给出的答案我们发现,它并没有仔细的去亲自去做这道题目,结果给出了一个错误的答案。从中我们发现,当prompt中的问题的逻辑比较复杂的时候,我们直接了当的要求ChatGPT给出一个答案时候,ChatGPT也会像人类一样经常犯错误。下面我们先将prompt翻译一下:

Openai+Coursera: ChatGPT Prompt Engineering(一)

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)

 Openai+Coursera: ChatGPT Prompt Engineering(一)

请注意,该学生的答案实际上是不正确的。

我们可以通过指示ChatGPT首先计算出自己的答案再来判断学生的答案是否正确。因此需要改写一下prompt,我们需要把求解这道应用题的详细步骤告诉ChatGPT:

Openai+Coursera: ChatGPT Prompt Engineering(一)

接下来给出实现这些步骤的具体格式:

Openai+Coursera: ChatGPT Prompt Engineering(一)

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)

Openai+Coursera: ChatGPT Prompt Engineering(一)

 这里我们可以发现ChatGPT的实际输出是从“Use the following format:” 的“Actual solution:”开始的,并没有将“Question:”和“Student's solution:”这两部分内容输出。这表明ChatGPT能够理解人类的意图,因为“Question:”和“Student's solution:”部分在题目中已经出现,无需在重复输出,只需输出“Actual solution:”部分以后的内容即可。最后ChatGPT通过比对自己的答案和学生的答案后给出了正确的答案。

模型局限性:幻觉(Hallucinations)

当ChatGPT不知道问题答案时候,ChatGPT往往不会承认自己不知道,因此GPT往往会在不知道正确答案的时候会给出一个貌似正确的错误答案。

下面我们在prompt中要求ChatGPT给出Boie这家公司的电动牙刷的相关信息,这里的 Boie 是一家真实的公司,产品名称不是真实的。

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

Openai+Coursera: ChatGPT Prompt Engineering(一)

 这里我们发现ChatGPT对一个Boie公司不存在的产品说了一大堆的内容,好像这个产品是真实存在似的。文章来源地址https://www.toymoban.com/news/detail-471147.html

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

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

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

相关文章

  • 使用 ChatGPT 的 7 个技巧 | Prompt Engineering 学习笔记

    前段时间在 DeepLearning 学了一门大火的 Prompt 的课程,吴恩达本人授课,讲的通俗易懂,感觉受益匪浅,因此在这里总结分享一下我的学习笔记。 为什么要学习 Prompt ? 因为在未来的 AIGC 年代,学习有效的 Promot 提示词有效的利用 AI 来完成一些重复性的工作。这也我认为未来

    2024年02月07日
    浏览(38)
  • prompt-engineering-note(面向开发者的ChatGPT提问工程学习笔记)

    ChatGPT Prompt Engineering Learning Notesfor Developers (面向开发者的ChatGPT提问工程学习笔记) 课程简单介绍了语言模型的工作原理,提供了最佳的提示工程实践,并展示了如何将语言模型 API 应用于各种任务的应用程序中。 此外,课程里面提供了 Jupyter Notebook 代码实例,可以直接使用

    2024年02月12日
    浏览(29)
  • 关于Prompt Engineering你该了解啥?OpenAI应用研究负责人帮你梳理了

    ‍来源 | 机器之心 微信号:almosthuman2014 随着 ChatGPT、GPT-4 等模型的兴起,人们对如何创建提示以获得想要的输出越来越感兴趣。研究者对特定提示的响应可能很难预测,并且会因模型的不同而不同。本文来自 OpenAI 的翁丽莲(Lilian Weng) 撰文介绍了关于提示的一些内容,包括

    2024年02月02日
    浏览(23)
  • Prompt工程师指南[应用篇]:Prompt应用、ChatGPT|Midjouney Prompt Engineering

    主题: 与 ChatGPT 对话 Python 笔记本 Topics: ChatGPT介绍 审查对话任务 与ChatGPT对话 Python笔记本 ChatGPT介绍 ChatGPT是OpenAI训练的一种新型模型,可以进行对话交互。该模型经过训练,可以按照提示中的指令,在对话上下文中提供适当的回应。ChatGPT 可以帮助回答问题、建议菜谱、按

    2024年02月04日
    浏览(42)
  • 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日
    浏览(40)
  • 提升ChatGPT性能的实用指南:Prompt Engineering的艺术

    提示工程是一门新兴学科,就像是为大语言模型(LLM)设计的\\\"语言游戏\\\"。通过这个\\\"游戏\\\",我们可以更有效地引导 LLM 来处理问题。只有熟悉了这个游戏的规则,我们才能更清楚地认识到 LLM 的能力和局限。 这个\\\"游戏\\\"不仅帮助我们理解 LLM,它也是提升 LLM 能力的途径。有效

    2024年02月13日
    浏览(23)
  • 《ChatGPT Prompt Engineering for Developers》课程-提示词原则

    本章的主要内容为编写 Prompt 的原则,在本章中,我们将给出两个编写 Prompt 的原则与一些相关的策略,你将练习基于这两个原则来编写有效的 Prompt,从而便捷而有效地使用 LLM。 本教程使用 OpenAI 所开放的 ChatGPT API,因此你需要首先拥有一个 ChatGPT 的 API_KEY(也可以直接访问

    2024年02月03日
    浏览(27)
  • 【简单入门】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月05日
    浏览(36)
  • 提升ChatGPT答案质量和准确性的方法Prompt engineering

    影响模型回答精确度的因素 我们应该知道一个好的提示词,要具备一下要点: 清晰简洁,不要有歧义; 有明确的任务/问题,任务如果太复杂,需要拆分成子任务分步完成; 确保prompt中包含解答问题的必要说明/信息; 指定输出长度,避免浪费token/生成内容过长; 使用分隔

    2024年02月05日
    浏览(25)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包