提供上下文以提高准确性
通过向 AI 模型提供上下文,使模型能够更好地了解你的问题或应该知道的内容,从而提供最佳答案。 可以通过多种方式提供上下文。
请求输出组合
指定输出的结构可能会对结果产生很大影响。 这可能包括要求模型引用其来源、将响应编写为电子邮件、将响应格式设置为 SQL 查询、将情绪分类为特定结构等。 例如:
提示
Write a table in markdown with 6 animals in it, with their genus and species
响应
| Animal | Genus | Species |
| ---------------- |:--------------:|:--------------:|
| Red Panda | Ailurus | fulgens |
| African Elephant | Loxodonta | africana |
| Snow Leopard | Panthera | uncia |
| Arctic Fox | Vulpes | lagopus |
| Chimpanzee | Pan | troglodytes |
| Siberian Tiger | Panthera | tigris altaica |
此方法可用于自定义格式,例如 JSON 结构:
提示
Put two fictional characters into JSON of the following format
{
firstNameFictional:
jobFictional:
}
响应
Here's an example of how you can put two fictional characters into JSON format:
{
"firstNameFictional": "Henry",
"jobFictional": "Wizard"
},
{
"firstNameFictional": "Frank",
"jobFictional": "Hero"
}
系统消息
系统消息包含在提示的开头,旨在提供模型说明、回答的角度或其他有助于指导模型响应的信息。 此系统消息可能包括音调或个性、不应包含的主题或如何回答的详细信息(例如,格式设置)。
例如,可以向它提供以下一些系统消息:
- “我希望你像命令行终端一样操作。 在一个唯一代码块中完全像 cmd.exe 一样响应命令,没有其他要求。”
- “我希望你成为一名翻译,从英语翻译成西班牙语。 不要回复我说或问的任何内容,只翻译这两种语言,并用翻译的文本回复。”
- “作为励志演说家,自由地就目标和挑战给出令人鼓舞的建议。 你应该包括许多积极的肯定和建议的活动,以实现用户的最终目标。”
Azure AI Studio 中的聊天窗口顶部提供了其他示例系统消息,找到它们的方法是选择“提示示例”按钮。 尝试定义自己的系统提示以指定唯一响应,并与模型聊天以查看响应的不同之处。
ChatCompletion
终结点允许使用 System
聊天角色包括系统消息。
var chatCompletionsOptions = new ChatCompletionsOptions()
{
Messages =
{
new ChatRequestSystemMessage("You are a casual, helpful assistant. You will talk like an American old western film character."),
new ChatRequestUserMessage("Can you direct me to the library?")
}
};
响应
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Well howdy there, stranger! The library, huh?
Y'all just head down the main road till you hit the town
square. Once you're there, take a left and follow the street
for a spell. You'll see the library on your right, can’t
miss it. Happy trails!",
"role": "assistant"
}
}
],
...
}
response = openai.ChatCompletion.create(
model="gpt-35-turbo",
messages=[
{"role": "system", "content": "You are a casual, helpful assistant. You will talk like an American old western film character."},
{"role": "user", "content": "Can you direct me to the library?"}
]
)
响应
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Well howdy there, stranger! The library, huh?
Y'all just head down the main road till you hit the town
square. Once you're there, take a left and follow the street
for a spell. You'll see the library on your right, can’t
miss it. Happy trails!",
"role": "assistant"
}
}
],
...
}
系统消息在格式和内容方面都可能会显著更改响应。 尝试为模型定义一条明确的系统消息,以准确说明你期望的响应类型,以及你希望或不希望它包含的内容。
会话历史记录
除了系统消息外,还可以向模型提供其他消息以增强会话。 对话历史记录使模型能够继续以类似方式响应(例如,音调或格式设置),并允许用户在后续查询中引用以前的内容。 可以通过两种方式提供此历史记录:从实际聊天历史记录,或从用户定义的示例对话。
使用 OpenAI 模型的聊天接口(例如,ChatGPT 和 Azure AI Studio 中的聊天操场)自动包含对话历史记录,从而生成更丰富、更有意义的对话。 在聊天操场的“参数”部分中,可以指定要包含的过去消息数。 尝试将该值减少到 1 或增加到最大值,以查看不同历史记录量对对话的影响。
注意
提示中包含的会话历史记录越多意味着使用的输入令牌数就越多。 考虑到所使用的模型的令牌限制,必须确定用例的正确平衡。
聊天系统还可以利用模型的汇总功能来节省输入令牌。 应用可以选择汇总过去的消息,并将该摘要包含在对话历史记录中,然后仅向模型逐字提供过去几条消息。
少样本学习
使用用户定义的示例对话就是所谓的“少样本学习”,它提供应如何响应给定查询的模型示例。 这些示例用于训练模型如何响应。
例如,通过向模型提供几个提示和预期的响应,它以相同的模式继续运行,而不指示它该执行什么操作:
User: That was an awesome experience
Assistant: positive
User: I won't do that again
Assistant: negative
User: That was not worth my time
Assistant: negative
User: You can't miss this
Assistant:
如果仅使用 You can't miss this
(而没有来自少样本学习的其他上下文)提供模型,则响应不太可能有用。
实际上,对话历史记录和少样本学习以相同的方式发送到模型:每个用户消息和助手响应都是消息对象中的离散消息。 ChatCompletion
终结点已优化为包含消息历史记录,无论此消息历史记录是以少样本学习的形式提供,还是以实际对话历史记录的形式提供。
var chatCompletionsOptions = new ChatCompletionsOptions()
{
Messages =
{
new ChatRequestSystemMessage("You are a helpful assistant."),
new ChatRequestUserMessage("That was an awesome experience"),
new ChatRequestAssistantMessage("positive"),
new ChatRequestUserMessage("I won't do that again"),
new ChatRequestAssistantMessage("negative"),
new ChatRequestUserMessage("That was not worth my time"),
new ChatRequestAssistantMessage("negative"),
new ChatRequestUserMessage("You can't miss this")
}
};
response = openai.ChatCompletion.create(
model="gpt-35-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "That was an awesome experience"},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "I won't do that again"},
{"role": "assistant", "content": "negative"},
{"role": "user", "content": "That was not worth my time"},
{"role": "assistant", "content": "negative"},
{"role": "user", "content": "You can't miss this"}
]
)
拆分复杂任务
改进交互的另一种方法是将复杂的提示划分为多个查询。 这使模型能够更好地了解每个单独的部分,并且可以提高整体准确性。 通过划分提示,还可以在将来的提示中包含来自上一个提示的响应,以及使用该信息以及模型功能生成相关响应。
例如,可以询问模型 Doug can ride down the zip line in 30 seconds, and takes 5 minutes to climb back up to the top. How many times can Doug ride the zip line in 17 minutes?
。 结果可能是 3
,如果 Doug 从 zip 线的顶部开始,则结果不正确。
通过询问多个问题可能会得到信息更丰富的答案,这些答案关于返回到 zip 线顶部的往返时间,以及如何说明 Doug 从顶部开始的事实。 拆分此问题表明,Doug 事实上可以穿越 zip 线四次。
思维链
帮助你有效地拆分任务的一个有用方法是让模型解释其思维链。
要求模型使用确定响应的分步过程做出响应是了解模型如何解释提示的有用方法。 通过执行此操作,你可以看到模型在何处进行了错误的逻辑轮次,并更好地了解如何更改提示以避免错误。 此方法可能包括要求它引用其来源,就像必应聊天(使用 GPT-4 代模型),并给出确定答案的原因。
思维链提示技术最适合用于帮助你循环访问和改进提示,以便从模型获得最高质量的答案。
例如,询问模型 What sport is easiest to learn but hardest to master?
会得到一个答案作为响应,并简要说明原因。 但是,当出现 What sport is easiest to learn but hardest to master? Give a step by step approach of your thoughts, ending in your answer
提示时,响应是它如何得出答案的完整说明。
Sure, here's a step-by-step approach to answering the question:
1. Identify sports that are popular and have a large following.
2. Consider the basic mechanics of each sport and how easy they are to learn.
3. Think about the various techniques and skills required to play each sport at a high level, and how difficult they are to master.
4. Compare the relative ease of learning each sport to the difficulty of mastering it.
5. Based on the above analysis, determine which sport is easiest to learn but hardest to master.
Using this approach, I would say that the sport of golf is easiest to learn but hardest to master. Here's why:
1. Golf is a popular sport with a large following.
2. The basic mechanics of hitting a golf ball are relatively simple and can be learned quickly with some practice.
3. However, achieving consistency and mastering the various shots and techniques required to play at a high level can take years of practice and dedication.
4. Compared to other sports, the difference between a beginner and an intermediate golfer is relatively small, while the difference between an intermediate and an advanced golfer is much larger.
5. Therefore, I would say that golf is the sport that is easiest to learn but hardest to master.
了解模型用于形成其响应的原因步骤后,我们可以确定需要拆分提示的位置、提供更多信息或阐明说明。