吴恩达联手OpenAI推出Prompt Engineering 课程 1. Guidelines

课程地址:ChatGPT Prompt Engineering for Developers。戳一下链接就可以进入课程页面了,进入后点击Learn for Free,如果有 Google 账号直接用账号登陆就可以了。该课程是免费的,不需要任何费用。这里直接从第二节 Guidelines 看看。

吴恩达联手 OpenAI 推出 Prompt Engineering 课程 1. Guidelines

2. Guidelines

主要介绍 prompts 的两个原则:

  1. Principle 1: Write clear and specific instructions
  2. Principle 2: Give the model time to“think”

进入 Guidelines 小节(点击上图中左边的 Guidelines 就可以进入了),你的界面应该是这样的:

吴恩达联手 OpenAI 推出 Prompt Engineering 课程 1. Guidelines

这里使用 openaipython 包,你可以通过pip install openai 本地安装。本地设置 api-key:openai.api_key = "sk-..."

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

Principle 1: Write clear and specific instructions

第一个策略是对一段文本进行总结, 这里就是用 Summarize the text delimited by triple backticks into a single sentence. 作为指令,让 model 对三个反引号包裹的 text 进行总结, 这就构成了一个 prompt。在创建 prompt 时,要用一些分隔符来隔开 instruction 和 text, 比如““`, <>”等

再调用 get_completion(prompt, model="gpt-3.5-turbo") 就可以了。这里对于的一些文档总结,报告总结都是可以用这个模板的。

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)

输出还不错:

To guide a model towards the desired output and reduce the chances of irrelevant or incorrect responses, it is important to provide clear and specific instructions, which may be longer prompts that provide more clarity and context for the model.

第二个策略是生成满足条件的文本, 并输出为指定格式如 json, html,这里任务是要生成 3 个有book_id, title, author, genre 4 个 keys 的 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)
================================
[
  {
    "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"
  }
]

接下来还有 Ask the model to check whether conditions are satisfied"Few-shot" prompting

prompt 越清晰就回答就越好,另外可以根据其格式来设计 prompt。比如 "Few-shot" prompting 中用 <child>: / <grandparent>: 来设置角色,进一步限定生成的范围。

Principle 2: Give the model time to“think”

先看第一个策略,Tactic 1: Specify the steps required to complete a task。直接看 prompt:

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
}

这里, 每个步骤是从逐步递进的,就是下一步是在上一步的基础上得出的。这样能给 GPT 时间思考生成正确回答。

  1. 第一步是一句话总结text.
  2. 翻译成法语
  3. 列出法语总结中的每个名字
  4. 按 json 格式输出包含 french_summary, num_names 的文档

Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion,这是一个财务成本计算中判断学生答案是否正确的实例。以后老师改作业直接扔 ChatGPT,哈哈!

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.

这里给的学生 solution 是错误的,那么接下来,你可以要求给出 GPT 自己的答案并做比较,然后来判断学生的是正确的还是不正确。

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 + 10x

Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000

Is the student's solution the same as actual solution just calculated:
No

Student grade:
Incorrect
模型限制——“胡说”

GPT 也会胡说,这里 prompt 让给出 Boie 产品 AeroGlide UltraSlim Smart Toothbrush 信息,公司是真的,产品是假的。它会胡说一堆。

prompt = f"""Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie"""

这就是第一部分内容了,接下来内容在 第二部分

 
正文完
 
admin
版权声明:本站原创文章,由 admin 2023-11-26发表,共计6145字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请联系tensortimes@gmail.com。
评论(没有评论)
验证码