课程:https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/

课程代码:https://github.com/ArslanKAS/Prompt-Engineering-by-OpenAI

文档:https://platform.openai.com/docs/api-reference/introduction

Overview

  1. clear not equal short。有时必要的增加prompt的长度能下达更精准的命令。
  2. 使用区分符号如三引号,tag符号等。避免对模型产生误导。
  3. 解决复杂问题时要引导模型step by step的解决问题。模型有跳过说明性文字而直接看结果的倾向,可能导致错误答案。
  4. 要给模型思考时间。如提示他直到计算出答案前不要决定结果。
  5. 避免让模型直接生成名字等。模型有生成不存在的答案的倾向。必要时可以向模型提供相关的情报信息引导它生成真实存在的名字。
  6. 提示工程和深度学习的过程一样,是递归的。我们应该根据具体问题具体分析,在一次次的实验中找到最优prompt。
  7. 巧妙利用数组存放prompts,以让模型进行流水线工作。
  8. 对结果的格式进行要求以获得适合编程用的内容:转化JSON,作成html形式,数值化等。

基本函数

openai 的api导入:

import os
import openai
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 = """..."""

prompt_2 = f"""
你的任务如下:
1. 总结<>内包含的内容为一句话
2. 将总结的内容转换为法语
3. 将法语总结里的名字列成表
4. 生成json对象,里面应包含以下的键:法语总结,名字

答案应使用如下的格式:
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)

方法二:引导step by step的生成答案 | 给模型思考时间

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)

方法三:给模型足够的情报

这个prompt非常长,但是给了模型非常多的需要的信息以生成想要的答案:模型担任的职能,需要关注的细节,面向的客户群体,必须输出的内容。

prompt = f"""
Your task is to help a marketing team create a
description for a retail website of a product based
on a technical fact sheet.

Write a product description based on the information
provided in the technical specifications delimited by
triple backticks.

The description is intended for furniture retailers,
so should be technical in nature and focus on the
materials the product is constructed from.

At the end of the description, include every 7-character
Product ID in the technical specification.

Use at most 50 words.

Technical specifications: ```{fact_sheet_chair}```
"""
response = get_completion(prompt)
print(response)

方法四:处理流水线

for i in range(len(reviews)):
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site.

Summarize the review below, delimited by triple \
backticks in at most 20 words.

Review: ```{reviews[i]}```
"""

response = get_completion(prompt)
print(i, response, "\n")

场景一:对客户评价进行处理并提供可分析的数据

利用模型自动对客户的满意度进行评价。利用此我们还可以想到更多的可能性:

  1. 预警系统。对不满意的客户及时预警和对应。
  2. 分析系统。每个产品的满意程度的分析。
  3. 处理系统。对不满意的客户我们可以利用提取到的关键信息,让模型先自动进行消息回复的对应。并且可以通过更改temp参数增加其多样性。
  4. 流水线处理。利用数组就可以批量处理。
prompt = f"""
Identify the following items from the review text:
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

场景二:对话机器人

可对话型和上述的单纯处理型的区别在于:需要记忆能力。

因此首先我们使用新的函数。其中messages里记录了前面的对话。

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]

message的格式如下:

messages =  [  
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},
{'role':'user', 'content':'tell me a joke'},
{'role':'assistant', 'content':'Why did the chicken cross the road'},
{'role':'user', 'content':'I don\'t know'} ]

system为最高层的要求,相当于下达prompt的系统。user为客户,assistant为模型。

接下来利用GUI包生成披萨订购助手:

## 储存之前的对话
def collect_messages(_):
prompt = inp.value_input
inp.value = ''
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))

return pn.Column(*panels)

##MAIN
import panel as pn # GUI
pn.extension()

panels = [] # collect display

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
The menu includes \
pepperoni pizza 12.95, 10.00, 7.00 \
cheese pizza 10.95, 9.25, 6.50 \
eggplant pizza 11.95, 9.75, 6.75 \

"""} ] # accumulate messages

inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

dashboard