如何使用大型语言模型
date
Apr 16, 2023
slug
openai_how_to_use_llm
status
Published
tags
openai
summary
大型语言模型是将文本映射到文本的函数。给定一个输入文本字符串,大型语言模型会预测接下来应该出现的文本。在大型语言模型的所有输入中,迄今为止影响最大的是文本提示。
type
Post
How large language models work 大型语言模型的工作原理
Large language models are functions that map text to text. Given an input string of text, a large language model predicts the text that should come next.大型语言模型是将文本映射到文本的函数。给定一个输入文本字符串,大型语言模型会预测接下来应该出现的文本。
The magic of large language models is that by being trained to minimize this prediction error over vast quantities of text, the models end up learning concepts useful for these predictions. For example, they learn:大型语言模型的神奇之处在于,通过训练以最大限度地减少对大量文本的预测误差,模型最终学习到对这些预测有用的概念。例如,他们学习:
- how to spell 怎么拼
- how grammar works 语法是如何运作的
- how to paraphrase 如何释义
- how to answer questions 如何回答问题
- how to hold a conversation 如何进行对话
- how to write in many languages如何用多种语言写作
- how to code 如何编码
- etc.
None of these capabilities are explicitly programmed in—they all emerge as a result of training.这些能力都没有明确编入程序——它们都是训练的结果。
GPT-3 powers hundreds of software products, including productivity apps, education apps, games, and more.GPT-3 为数百种软件产品提供支持,包括生产力应用程序、教育应用程序、游戏等。
How to control a large language model如何控制大型语言模型
Of all the inputs to a large language model, by far the most influential is the text prompt.在大型语言模型的所有输入中,迄今为止影响最大的是文本提示。
Large language models can be prompted to produce output in a few ways:可以通过以下几种方式提示大型语言模型生成输出:
- Instruction
: Tell the model what you want
说明:告诉模型你想要什么
- Completion
: Induce the model to complete the beginning of what you want
完成:诱导模型完成你想要的开始
- Demonstration
- A few examples in the prompt提示中的几个例子
- Many hundreds or thousands of examples in a fine-tuning training dataset微调训练数据集中的数百或数千个示例
: Show the model what you want, with either:
演示:向模型展示您想要的内容,可以使用:
An example of each is shown below.下面显示了每个示例。
Instruction prompts 指令提示
Instruction-following models (e.g.,
text-davinci-003 or any model beginning with text-) are specially designed to follow instructions. Write your instruction at the top of the prompt (or at the bottom, or both), and the model will do its best to follow the instruction and then stop. Instructions can be detailed, so don't be afraid to write a paragraph explicitly detailing the output you want.指令跟随模型(例如, text-davinci-003 或任何以 text- 开头的模型)专门设计用于遵循指令。在提示的顶部(或底部,或两者)写下您的指令,模型将尽最大努力遵循指令,然后停止。说明可以很详细,所以不要害怕写一段明确详细说明您想要的输出。Example instruction prompt: 示例指令提示:
Extract the name of the author from the quotation below.
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
― Ted Chiang, ExhalationOutput:
Ted ChiangCompletion prompt example 完成提示示例
Completion-style prompts take advantage of how large language models try to write text they think is mostly likely to come next. To steer the model, try beginning a pattern or sentence that will be completed by the output you want to see. Relative to direct instructions, this mode of steering large language models can take more care and experimentation. In addition, the models won't necessarily know where to stop, so you will often need stop sequences or post-processing to cut off text generated beyond the desired output.完成式提示利用大型语言模型如何尝试编写他们认为最有可能出现的文本。要引导模型,请尝试从一个模式或句子开始,该模式或句子将由您想要查看的输出完成。相对于直接指令,这种转向大型语言模型的方式可以更加小心和实验。此外,模型不一定知道在哪里停止,因此您通常需要停止序列或后处理来切断超出所需输出的生成文本。
Example completion prompt: 示例完成提示:
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
― Ted Chiang, Exhalation
The author of this quote isOutput:
Ted ChiangDemonstration prompt example (few-shot learning)演示提示示例(few-shot learning)
Similar to completion-style prompts, demonstrations can show the model what you want it to do. This approach is sometimes called few-shot learning, as the model learns from a few examples provided in the prompt.与完成式提示类似,演示可以向模型展示您希望它做什么。这种方法有时称为小样本学习,因为模型从提示中提供的几个示例中学习。
Example demonstration prompt: 示例演示提示:
Quote:
“When the reasoning mind is forced to confront the impossible again and again, it has no choice but to adapt.”
― N.K. Jemisin, The Fifth Season
Author: N.K. Jemisin
Quote:
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
― Ted Chiang, Exhalation
Author:Output:
Ted ChiangFine-tuned prompt example 微调提示示例
With enough training examples, you can fine-tune a custom model. In this case, instructions become unnecessary, as the model can learn the task from the training data provided. However, it can be helpful to include separator sequences (e.g.,
-> or ### or any string that doesn't commonly appear in your inputs) to tell the model when the prompt has ended and the output should begin. Without separator sequences, there is a risk that the model continues elaborating on the input text rather than starting on the answer you want to see.通过足够的训练示例,您可以微调自定义模型。在这种情况下,指令变得不必要,因为模型可以从提供的训练数据中学习任务。但是,包含分隔符序列(例如, -> 或 ### 或输入中不常出现的任何字符串)以告知模型提示何时结束以及输出何时应该开始可能会有所帮助。如果没有分隔符序列,则存在模型继续详细说明输入文本而不是从您想要看到的答案开始的风险。Example fine-tuned prompt (for a model that has been custom trained on similar prompt-completion pairs):示例微调提示(对于已在类似提示完成对上进行自定义训练的模型):
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
― Ted Chiang, Exhalation
###Output:
Ted ChiangCode Capabilities
Large language models aren't only great at text - they can be great at code too. OpenAI's specialized code model is called Codex.大型语言模型不仅擅长文本——它们也擅长代码。 OpenAI 的专用代码模型称为 Codex。
Codex powers more than 70 products, including:Codex 支持 70 多种产品,包括:
- GitHub Copilot
(autocompletes code in VS Code and other IDEs)
GitHub Copilot(在 VS Code 和其他 IDE 中自动完成代码)
- Pygma
(turns Figma designs into code)
Pygma(将 Figma 设计转化为代码)
- Replit
(has an 'Explain code' button and other features)
Replit(有一个“解释代码”按钮和其他功能)
- Warp
(a smart terminal with AI command search)
Warp(带AI命令搜索的智能终端)
- Machinet
(writes Java unit test templates)
Machinet(编写 Java 单元测试模板)
Note that unlike instruction-following text models (e.g.,
text-davinci-002), Codex is not trained to follow instructions. As a result, designing good prompts can take more care.请注意,与指令跟随文本模型(例如, text-davinci-002 )不同,Codex 未经过训练以遵循指令。因此,设计好的提示可以更加小心。More prompt advice 更及时的建议
For more prompt examples, visit OpenAI Examples.有关更多提示示例,请访问 OpenAI 示例。
In general, the input prompt is the best lever for improving model outputs. You can try tricks like:通常,输入提示是改进模型输出的最佳杠杆。您可以尝试以下技巧:
- Give more explicit instructions.
E.g., if you want the output to be a comma separated list, ask it to return a comma separated list. If you want it to say "I don't know" when it doesn't know the answer, tell it 'Say "I don't know" if you do not know the answer.'
给予更明确的指示。例如,如果您希望输出是一个逗号分隔的列表,请让它返回一个逗号分隔的列表。如果您希望它在不知道答案时说“我不知道”,请告诉它“如果您不知道答案,请说“我不知道”。”
- Supply better examples.
If you're demonstrating examples in your prompt, make sure that your examples are diverse and high quality.
提供更好的例子。如果您在提示中演示示例,请确保您的示例多样化且质量高。
- Ask the model to answer as if it was an expert.
Explicitly asking the model to produce high quality output or output as if it was written by an expert can induce the model to give higher quality answers that it thinks an expert would write. E.g., "The following answer is correct, high-quality, and written by an expert."
让模型像专家一样回答。明确要求模型产生高质量的输出或输出,就好像它是由专家写的一样,可以诱导模型给出它认为专家会写的更高质量的答案。例如,“以下答案是正确的、高质量的,并且由专家撰写。”
- Prompt the model to write down the series of steps explaining its reasoning.Let's think step by step
E.g., prepend your answer with something like "
." Prompting the model to give an explanation of its reasoning before its final answer can increase the likelihood that its final answer is consistent and correct.
提示模型写下解释其推理的一系列步骤。例如,在您的答案前加上“让我们逐步思考”之类的内容。提示模型在其最终答案之前给出其推理的解释可以增加其最终答案一致和正确的可能性。