---
title: "Stop sequences (language models) — 停止序列"
source: "https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97"
wiki: "systems-analysis.info/int"
article: "Stop_sequences_(language_models)_—_停止序列"
language: "zh"
categories:
  - "Category:Chinese"
  - "Category:Large language models"
  - "Category:Machine learning"
  - "Category:Systems analysis terminology"
revision_id: 6954
wiki_created_at: 2026-09-07T00:13:44Z
wiki_modified_at: 2026-09-07T00:13:44Z
downloaded_at: 2026-09-07T23:16:36Z
---

# Stop sequences (language models) — 停止序列

**Stop Sequence** (停止序列) 在大型语言模型 (LLM)的语境中，是一种特殊的字符或词元序列，它向模型发出信号，要求停止生成文本<sup>[\[1\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-promptitude_def-1)</sup>。该机制是自回归语言模型的重要组成部分，可确保以可控和可预测的方式完成响应。

使用停止序列时，模型在生成的每个步骤都会检查已生成的文本是否以指定的序列之一结尾。如果找到匹配项，生成过程将立即停止，而停止序列本身**不会**包含在最终输出中<sup>[\[2\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-openai_api-2)</sup>。这使得开发者能够精确控制响应的边界，而无需更改请求本身。

## 基本工作原理

在自回归语言模型中，文本是逐个词元顺序生成的。在每个步骤中，模型会根据之前的所有序列（输入提示和已生成的文本）预测下一个词元。在数学上，这表示为条件概率：

$$
P(y_{t} \mid y_{< t},x)
$$

其中 $y_{t}$ 是当前生成的词元，$y_{< t}$ 是先前生成的词元序列，而 $x$ 是输入序列<sup>[\[3\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-vellum_params-3)</sup>。

**停止序列**机制是中断此迭代过程的外部标准。

## 停止序列的类型

存在几种主要的停止机制类型，它们可以单独使用，也可以组合使用。

### 1. 序列结束 (EOS) 词元

**End-of-Sequence (EOS)** 是一些特殊的词元（例如 \`\<\|endoftext\|\>\`），它们被内置在模型的词汇表中，用于表示文本逻辑片段的结束。模型被训练在认为响应完成时生成 EOS 词元，因为训练数据集中的所有文本都以该词元结尾<sup>[\[4\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-gpt3_survey-4)</sup>。当检测到 EOS 词元时，生成会自动停止。

研究表明，EOS 词元的存在会影响注意力架构：模型会发展出内部的位置计数机制，但这可能会限制其在处理远超训练样本长度的序列时的外推能力<sup>[\[5\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-no_eos_extrapolation-5)</sup>。

### 2. 自定义序列

这些是开发者为特定任务设置的任意字符串。它们不是模型词汇表的一部分，而是在字符级别进行跟踪。示例包括：

- **换行符**：\`\n\` 或 \`\n\n\`，用于在段落后停止。
- **上下文标记**：\`Human:\`、\`User:\` 或 \`Q:\`，用于在对话中分隔话语。
- **特殊标记**：\`###\`、\`\</output\>\` 或 \`END\`。

### 3. 结构化序列

这些是用于完成特定结构化元素的专用标记，在生成格式化内容时至关重要<sup>[\[1\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-promptitude_def-1)</sup>：

- **代码**：三个反引号 (\`\`\`) 用于结束代码块。
- **JSON/XML**：闭合括号 (\`}\`) 或标签 (\`\</element\>\`)。

## 技术实现与问题

有效检测停止序列是一项复杂的任务，伴随着一系列难题。

### 检测算法与优化

实际系统中的检测过程包括：

1.  **每步检查**：在生成每个新词元后，系统会检查当前输出是否以指定的停止序列之一结尾。
2.  **处理部分匹配**：系统必须跟踪序列的一部分已经生成但尚未完全匹配的情况。
3.  **多标准检查**：大多数系统（例如 OpenAI API）允许同时跟踪多个（最多四个）停止序列<sup>[\[2\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-openai_api-2)</sup>。

在 Hugging Face Transformers 框架中，为此实现了抽象类 \`StoppingCriteria\`，它允许创建自定义的停止标准，例如 \`MaxLengthCriteria\`（按长度）或 \`EosTokenCriteria\`（按 EOS 词元）<sup>[\[4\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-gpt3_survey-4)</sup>。

### 问题与限制

- **分词问题**：这是主要的技术难题。同一字符序列（例如 \`\nUser:\`）可能会根据上下文以不同方式被分词。这使得可靠检测变得复杂，因为停止序列可能会被分割到多个词元中<sup>[\[5\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-no_eos_extrapolation-5)</sup>。
- **性能**：在每个步骤检查多个长停止序列可能会减慢生成速度，尤其是在实时处理长序列时。
- **误报**：指定的序列可能会在期望的响应中间偶然出现，导致过早终止。因此，选择足够独特和特定的标记非常重要（例如 \`\n###\n\`）<sup>[\[6\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-finetune_gpt3-6)</sup>。

## 应用与使用场景

停止序列是控制 LLM 行为的强大工具。

- **控制长度和成本**：允许限制响应的最大大小，从而减少词元消耗，这在使用付费 API 时非常重要。
- **对话系统**：用于清晰地分隔对话者的话语，以防止模型助手为用户生成回复。
- **生成结构化内容**：在以 JSON、XML 格式获取正确输出或编写代码时不可或缺，可防止在结构完成后添加多余信息<sup>[\[7\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-cursor_backticks-7)</sup>。
- **防止不当行为**：有助于在出现重复或不正确内容（幻觉）时中断生成。
- **训练与微调 (fine-tuning)**：在训练数据集中，通常使用独特的标记（例如 \`###\`）作为停止序列，以教会模型在正确的位置结束响应<sup>[\[6\]](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_note-finetune_gpt3-6)</sup>。

## 当前研究方向

- **自适应停止标准**：开发根据上下文和生成文本的质量动态确定终止点的方法。
- **基于熵的方法**：使用词元分布的熵作为标准。高熵可能表示模型的不确定性，并可作为停止生成的信号。

## 外部链接

- <a href="https://help.openai.com/en/articles/5072263-how-do-i-use-stop-sequences-in-the-openai-api" class="external text" rel="nofollow">OpenAI 关于使用停止序列的文档</a>
- <a href="https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.StoppingCriteria" class="external text" rel="nofollow">Hugging Face 关于 StoppingCriteria 的文档</a>

## 参考文献

- Sutskever, I.; Vinyals, O.; Le, Q. V. (2014). *Sequence to Sequence Learning with Neural Networks*. <a href="https://arxiv.org/abs/1409.3215" class="external text" rel="nofollow">arXiv:1409.3215</a>.
- Vaswani, A. et al. (2017). *Attention Is All You Need*. <a href="https://arxiv.org/abs/1706.03762" class="external text" rel="nofollow">arXiv:1706.03762</a>.
- Keskar, N. S. et al. (2019). *CTRL: A Conditional Transformer Language Model for Controllable Generation*. <a href="https://arxiv.org/abs/1909.05858" class="external text" rel="nofollow">arXiv:1909.05858</a>.
- Holtzman, A. et al. (2020). *The Curious Case of Neural Text Degeneration*. <a href="https://arxiv.org/abs/1904.09751" class="external text" rel="nofollow">arXiv:1904.09751</a>.
- Brown, T. et al. (2020). *Language Models are Few-Shot Learners*. <a href="https://arxiv.org/abs/2005.14165" class="external text" rel="nofollow">arXiv:2005.14165</a>.
- Zong, M.; Krishnamachari, B. (2022). *A Survey on GPT-3*. <a href="https://arxiv.org/abs/2212.00857" class="external text" rel="nofollow">arXiv:2212.00857</a>.
- Zhao, Y. et al. (2022). *Calibrating Sequence Likelihood Improves Conditional Language Generation*. <a href="https://arxiv.org/abs/2210.00045" class="external text" rel="nofollow">arXiv:2210.00045</a>.
- Hu, J. C.; Cavicchioli, R.; Capotondi, A. (2023). *A Request for Clarity over the End-of-Sequence Token in the Self-Critical Sequence Training*. <a href="https://arxiv.org/abs/2305.12254" class="external text" rel="nofollow">arXiv:2305.12254</a>.
- Zhu, W. et al. (2024). *Improving Open-Ended Text Generation via Adaptive Decoding*. <a href="https://arxiv.org/abs/2402.18223" class="external text" rel="nofollow">arXiv:2402.18223</a>.
- Zhang, H. et al. (2024). *Adaptable Logical Control for Large Language Models*. <a href="https://arxiv.org/abs/2406.13892" class="external text" rel="nofollow">arXiv:2406.13892</a>.
- Suh, Y. J. et al. (2025). *The Curious Case of Sequentially Mis-calibrated Language Models*. <a href="https://arxiv.org/abs/2205.11916" class="external text" rel="nofollow">arXiv:2205.11916</a>.

## 注释

1.  <span id="cite_note-promptitude_def-1">↑ <sup>[1.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-promptitude_def_1-0)</sup> <sup>[1.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-promptitude_def_1-1)</sup> “Stop Sequence: Understanding & Setting It Correctly”. *Promptitude.io Help Center*. <a href="https://help.promptitude.io/en/articles/8897040-stop-sequence-understanding-setting-it-correctly" class="external autonumber" rel="nofollow">[1]</a></span>
2.  <span id="cite_note-openai_api-2">↑ <sup>[2.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-openai_api_2-0)</sup> <sup>[2.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-openai_api_2-1)</sup> “How do I use stop sequences in the OpenAI API?”. *OpenAI Help Center*. <a href="https://help.openai.com/en/articles/5072263-how-do-i-use-stop-sequences-in-the-openai-api" class="external autonumber" rel="nofollow">[2]</a></span>
3.  <span id="cite_note-vellum_params-3">[↑](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-vellum_params_3-0) “How to use stop sequences?”. *Vellum*. <a href="https://www.vellum.ai/llm-parameters/stop-sequence" class="external autonumber" rel="nofollow">[3]</a></span>
4.  <span id="cite_note-gpt3_survey-4">↑ <sup>[4.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-gpt3_survey_4-0)</sup> <sup>[4.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-gpt3_survey_4-1)</sup> Brown, Tom, et al. “A Survey on GPT-3”. *arXiv:2212.00857* \[cs.CL\], 1 Dec. 2022. <a href="https://arxiv.org/pdf/2212.00857" class="external autonumber" rel="nofollow">[4]</a></span>
5.  <span id="cite_note-no_eos_extrapolation-5">↑ <sup>[5.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-no_eos_extrapolation_5-0)</sup> <sup>[5.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-no_eos_extrapolation_5-1)</sup> Suh, Y. J., et al. “The Curious Case of Sequentially Mis-calibrated Language Models”. *arXiv:2205.11916* \[cs.CL\], 24 May 2022. <a href="https://arxiv.org/pdf/2205.11916" class="external autonumber" rel="nofollow">[5]</a></span>
6.  <span id="cite_note-finetune_gpt3-6">↑ <sup>[6.0](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-finetune_gpt3_6-0)</sup> <sup>[6.1](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-finetune_gpt3_6-1)</sup> Eric, Mihail. “How to Finetune GPT3”. *mihaileric.com*. <a href="https://www.mihaileric.com/posts/how-to-finetune-gpt3/" class="external autonumber" rel="nofollow">[6]</a></span>
7.  <span id="cite_note-cursor_backticks-7">[↑](https://systems-analysis.info/int/Stop_sequences_(language_models)_%E2%80%94_%E5%81%9C%E6%AD%A2%E5%BA%8F%E5%88%97#cite_ref-cursor_backticks_7-0) Corin, Daniel. “Way Enough - Cursor Triple Backticks Stop Sequence”. *danielcorin.com*. <a href="https://www.danielcorin.com/til/cursor/triple-backticks-stop-sequence/" class="external autonumber" rel="nofollow">[7]</a></span>
