25
2023-05
学习笔记|ChatGPT Prompt Engineering for Developers 2
吴恩达chatGPT课程学习笔记&简单翻译,有兴趣的同学还是去看原版的课程比较好~~
LEARN MORE
图片由Stable Diffusion绘制
首先
狗头保命,不是翻译,是学习笔记,也就是中间删了部分我觉得不重要的内容,部分我觉得有意思的内容稍稍扩展了一点。
感兴趣的同学请去看原版的课程,原本的课程是免费的:
https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/
想要逐句翻译版本的同学可以看这里:
https://blog.csdn.net/youcans/article/details/130489953
第二讲:指导原则
课程以网页版的jupyter notebook作为辅助,可以中途暂停视频自己运行代码查看效果,非常棒,省去了很多时间成本。
首先是两个prompt的指导原则:
课程中给到了非常好的一个工具,就是可以现场用jupyter notebook调试代码,并且学习站内置了chatgpt的api,可以一边看视频一边调试,真的是完全不用操心:
首先,使用chatgpt api的框架是这样的,可以无脑复制直接用:
import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
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 = f"""
内容
""
prompt = f"""
提示词
```{text}```
"""
response = get_completion(prompt)
print(response)
对于“具体且清晰的指示”,有三个小技巧:
第一是用分隔符(比如 """,---,< >)避免提示注入。简单说就是可以参考上面的框架,把text和prompt拆开写。
第二要求AI用结构化的输出,例如 HTML 或 JSON 格式。可以参考下面这个表述:Provide them in JSON format with the following keys: XX,XX,XX
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)
第三个小技巧是检查模型是否有if…… end 的情况,即在什么样的情况下需要停止模型。有点类似于case when 语句一定要有else,在搭建模型的时候必须要检查else的情况,否则模型可能会输出一些奇怪的东西。
第四个技巧是给一个回答的参考样例,比如,“请用XX的口吻来回答我”。
第二个原则,给模型思考的时间。
如果你给模型一个太复杂的任务,模型很有可能开始胡说八道。可以考虑把任务进行拆解,告诉模型第一步做什么,第二步做什么,就能提高模型的准确度。演示给了一个把一段英文的故事翻译成法语,并输入输出一个 JSON 对象,包括以下字段:法语摘要和名字的数量。这就是一个比较复杂的任务了,直接的文本描述AI难以理解,所以在提示词中告诉AI,第一步,总结文本,第二部翻译成法语,第三步列出名字,第四步输出json对象。
这个案例的代码也是可以参考的,use the following 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)
第二个技巧是,教导模型在快速得出结论之前,先自己想办法解决问题。
chatgpt早期的时候,如果你给AI一个错误的命题,AI就会顺着你的错误给你一个错误的答案,比如“为什么莫扎特不出新歌了”,chatgpt会一本正经地回答为什么。如果想要AI给你一个正确的结论,你首先需要教导AI什么是正确的。
课程中给了一个案例,给了AI一个学生的解题思路让AI判断是否正确,AI果断跟着顺拐了。解决的思路也很简单,让AI自己去解决这个问题,然后比对一下AI自己的解决思路和学生是否一致,再输出学生的解答是否正确。代码可以参考:
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)
最后说一下模型的弱点,由于AI在训练过程中采用了非常庞大的知识量,导致AI对自己的知识边界没有很清晰的认知,于是AI会去试图回答一些超出自己能力范围的问题——编造出一堆看起来很正确的胡话。
减少胡说八道的一个很好的策略是,如果你想让模型根据文本生成答案,可以要求模型先从文本中找到任何相关引用,然后让它使用这些引用来回答问题,并且把答案追溯到源文件。比如要求AI从pandas官方文档中找答案,而不是瞎编一个函数给我(是的我遇到过AI瞎编得函数,而且看起来好像没毛病)。
THANKS