文章目录
- 一. chat_completion函数
- 二. 设计有效的提示词
- 1.上下文
- 1.1. 更多细节的上下文
- 1.2. 让GPT改进上下文
- 2.任务
- 2.1. 提供足够的任务信息
- 2.2. OpenAI的任务示例
- 语法纠正
- 总结
- TL;DR概要
- Python转自然语言
- 计算时间复杂度
- 修复Python bug
- 产生python函数
- 3.角色
了解LLM和OpenAI API的基础知识,之后我们就可以了解一些更高阶的知识,比如提示工程、零样本学习、少样本学习到为特定任务微调模型,接下来我们将要了解提供开发LLM驱动型应用程序所需的一切知识。
一. chat_completion函数
先来回顾一下chat_completion函数
def chat_completion(prompt, model="gpt-4", temperature=0):
res = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
)
print(res["choices"][0]["message"]["content"])
model和temperature是两个可选特征,分别被默认设置为gpt-4和0。
为了说明提示工程的原理,我们使用示例文本“As Descartes said, I think therefore”(正如笛卡儿所说,我思故)。如果将此文本输入GPT-4,那么模型自然会通过迭代式地添加最可能出现的标记来补全句子:
chat_completion("As Descartes said, I think therefore")
模型的输出消息如下所示:
I am. This famous philosophical statement, also known as "Cogito, ergo
sum," emphasizes the existence of the self through the act of thinking
or doubting. Descartes used this statement as a foundational principle
in his philosophy, arguing that one's own existence is the most certain
and indubitable fact that can be known.
需要注意的是:
- 提示工程可能会影响OpenAI API的使用成本。
该成本与你发送给OpenAI并从其接收的标记数成正比
。如所述,我们强烈建议使用max_tokens参数,以避免费用超出预期。- 另外在openai库的方法中使用不同的参数,因为如果使用temperature、top_p和max_tokens等参数,那么即使使用相同的提示词,你也可能得到截然不同的结果。
二. 设计有效的提示词
很多任务可以通过提示词来完成,包括摘要、文本分类、情感分析和问题回答
。在所有这些任务中,我们通常需要在提示词中定义三大要素:角色、上下文和任务
,如下图。
这三大要素并非都是必需的,它们的顺序也不是固定的。不过,如果能够构造好提示词的结构并明确定义这三大要素,你就能够得到不错的结果。
请注意,即使使用了这三大要素,对于复杂的任务,你也可能需要采用更高级的技巧,如零样本学习、少样本学习和微调。
1.上下文
提示词的第一大要素是上下文。在为LLM编写输入文本时,必须尽可能详细地描述上下文。为了说明这一点,假设我们想使用GPT-4创建一个应用程序,用于推荐午餐的主菜。
1.1. 更多细节的上下文
接下来,我们将比较在两个上下文中获得的结果。第一个上下文提供较少的细节,第二个上下文则提供较多的细节。假设使用以下较短的提示词:
chat_completion("Give me a suggestion for the main course for
today's lunch.")
我们得到以下输出消息:
Grilled chicken with roasted vegetables and quinoa.
现在,在提示词中加入更多上下文信息:
prompt = """
Context: I do 2 hours of sport a day. I am vegetarian, and I don't
like green vegetables. I am conscientious about eating healthily.
Task: Give me a suggestion for a main course for today's lunch."""
chat_completion(prompt)
我们得到以下输出消息:
For today's lunch, you can try a Chickpea and Sweet Potato Curry
served with Quinoa. This dish is packed with protein, fiber, and
essential nutrients, while also being vegetarian and not relying
on green vegetables. The curry can be made with chickpeas, sweet
potatoes, tomatoes, onions, garlic, and a blend of spices like
turmeric, cumin, and coriander. Cook the quinoa separately and
serve it alongside the curry for a delicious and healthy meal.
在第二个例子中,由于提示词含有更多上下文,因此模型给出的句子更详细:它推荐了一道富含蛋白质的健康素食菜肴。
1.2. 让GPT改进上下文
为了找到可改进之处,你可以请GPT-4提些意见。
在下面的示例中,我们修改提示词,不再要求模型回答问题,而是指示它以增强补全效果为目标来向我们提出关于上下文的问题。使用以下提示词:
prompt = """
Context: I do 2 hours of sport a day. I am vegetarian and I don't
like green vegetables. I am very careful to eat healthily.
Task: Give me a suggestion for a main course for today's lunch? Do
not perform the requested task! Instead, can you ask me questions
about the context so that when I answer, you can perform the
requested task more efficiently?
"""
chat_completion(prompt)
GPT-4向我们提出以下问题:
Sure, I have a few questions to help me suggest a suitable main
course for you:
1. What type of sport do you do, and do you have any specific nutritional requirements related to it?
2. Are there any other vegetables or ingredients that you particularly
enjoy or dislike?
3. Do you prefer a hot or cold meal for lunch?
4. Are you open to trying new recipes or do you prefer sticking to
familiar dishes?
模型提出的问题通常与提示词相关。根据这些问题,我们可以在提示词中更新上下文。
总之,我们建议你在构建提示词时经常使用这个技巧。
2.任务
任务定义了你对GPT-4模型的用法,并且应该明确且具体。你应该提供足够的任务信息,并在提示词中使用合适的短语来引导模型给出你所期望的结果。
2.1. 提供足够的任务信息
在前面的例子中,任务是让模型为午餐推荐一道主菜。我们可以为该任务添加细节,以说明我们想要的内容。在本例中,我们要求模型添加一张表格,列出每种食材所需的克数
prompt = """
Context: I do 2 hours of sport a day. I am vegetarian, and I don't
like green vegetables. I am conscientious about eating healthily.
Task: Give me a suggestion for a main course for today's lunch. With
this suggestion, I also want a table with two columns where each row
contains an ingredient from the main course. The first column of the
table is the name of the ingredient. The second column of the table
is the number of grams of that ingredient needed for one person. Do
not give the recipe for preparing the main course.
"""
chat_completion(prompt)
模型给出以下输出结果:
Suggestion for today's lunch: Stuffed Bell Peppers with Quinoa and
Black Beans
| Ingredient | Grams per person |
|---------------------|------------------|
| Bell peppers | 200g |
| Quinoa | 60g |
| Black beans | 75g |
| Onion | 50g |
| Garlic | 5g |
| Olive oil | 10g |
| Tomato sauce | 50g |
| Cumin | 1g |
| Chili powder | 1g |
| Salt | 1g |
| Pepper | 1g |
| Shredded cheese | 30g |
2.2. OpenAI的任务示例
OpenAI API示例页面列出了48个任务示例。这些示例展示了GPT模型可以执行的任务,其中每个示例都配有相关的提示词和演示。见官网: Prompt examples
虽然这些示例使用了GPT-3模型和Completion端点,但对于ChatCompletion端点来说,原理是相同的,并且这些示例很好地说明了如何给OpenAI模型指派任务。
语法纠正
纠正句子中的语病,并将其修改为标准的英语句子。
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You will be provided with statements, and your task is to convert them to standard English."
},
{
"role": "user",
"content": "She no went to the market."
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
总结
给二年级学生概括一下
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "Summarize content you are provided with for a second-grade student."
},
{
"role": "user",
"content": "Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[20] and is on average the third-brightest natural object in the night sky after the Moon and Venus."
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
TL;DR概要
TL;DR是“too long; didn’t read”的首字母缩写,意为“太长了,没读”。有人发现,只需在文本末尾添加Tl;dr,即可请求模型对文本进行总结。
提示词示例如下。
A neutron star [...] atomic nuclei. Tl;dr
Python转自然语言
用自然语言解释一段Python代码。
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "system",
"content": "You will be provided with a piece of code, and your task is to explain it in a concise way."
},
{
"role": "user",
"content": "class Log:\n def __init__(self, path):\n dirname = os.path.dirname(path)\n os.makedirs(dirname, exist_ok=True)\n f = open(path, \"a+\")\n \n # Check that the file is newline-terminated\n size = os.path.getsize(path)\n if size > 0:\n f.seek(size - 1)\n end = f.read(1)\n if end != \"\\n\":\n f.write(\"\\n\")\n self.f = f\n self.path = path\n \n def log(self, event):\n event[\"_event_id\"] = str(uuid.uuid4())\n json.dump(event, self.f)\n self.f.write(\"\\n\")\n \n def state(self):\n state = {\"complete\": set(), \"last\": None}\n for line in open(self.path):\n event = json.loads(line)\n if event[\"type\"] == \"submit\" and event[\"success\"]:\n state[\"complete\"].add(event[\"id\"])\n state[\"last\"] = event\n return state"
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
计算时间复杂度
计算一个函数的时间复杂度。
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You will be provided with Python code, and your task is to calculate its time complexity."
},
{
"role": "user",
"content": "def foo(n, k):\n accum = 0\n for i in range(n):\n for l in range(k):\n accum += i\n return accum"
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
修复Python bug
修复含有bug的Python代码。
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "system",
"content": "You will be provided with a piece of Python code, and your task is to find and fix bugs in it."
},
{
"role": "user",
"content": "import Random\n a = random.randint(1,12)\n b = random.randint(1,12)\n for i in range(10):\n question = \"What is \"+a+\" x \"+b+\"? \"\n answer = input(question)\n if answer = a*b\n print (Well done!)\n else:\n print(\"No.\")"
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
SQL请求
构建简单的SQL查询。提示词示例如下。
Create a SQL request to find all users who live in California and
have over 1000 credits.
产生python函数
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Write a Python function that takes as input a file path to an image, loads the image into memory as a numpy array, then crops the rows and columns around the perimeter if they are darker than a threshold value. Use the mean value of rows and columns to decide if they should be marked for deletion."
}
],
temperature=0.7,
max_tokens=64,
top_p=1
)
3.角色
在撰写提示词时,影响模型的最后一种方式是赋予其一个角色。当然,角色和上下文可以独立使用,但同时使用可以加强对模型输出的控制
。
在以下示例中,我们赋予模型运动营养专家的角色:
prompt = """
# Role: You are a nutritionist designing healthy diets for high-performance athletes. You take into account the nutrition needed for a good recovery.
# Context: I do 2 hours of sport a day. I am vegetarian, and I don't like green vegetables. I am conscientious about eating healthily.
# Task: Based on your expertise defined in your role, give me a suggestion for a main course for today's lunch. With this suggestion, I also want a table with two columns where each row in the table contains an ingredient from the main course. The first column of the table is the name of the ingredient. The second column of the table is the number of grams of that ingredient needed for one person. Do not give the recipe for preparing the main course.
"""
chat_completion(prompt)
我们得到以下结果:
Main Course Suggestion: Quinoa and Chickpea Stuffed Bell Peppers
Ingredient Table:
| Ingredient | Grams per Person |
|---------------------|------------------|
| Quinoa | 60 |
| Chickpeas | 100 |
| Bell Peppers | 200 |
| Olive Oil | 10 |
| Onion | 50 |
| Garlic | 5 |
| Tomato | 100 |
| Feta Cheese | 30 |
| Kalamata Olives | 20 |
| Fresh Basil | 5 |
| Salt | 2 |
| Black Pepper | 1 |
提示词可用于调整像GPT模型这样的LLM的概率分布集
。它们可以被视为模型指南,引导模型生成特定类型的结果。
注意,上述提到的上下文、任务、角色
的提示词格式,只是一种方法,你完全可以创建不明确定义这些要素的提示词。不要受限于“上下文−任务−角色”框架,而应将其视为帮助你有效设计提示词的工具。