# Guidance（微软）

**Guidance** 是一个开源 Python 库，源自[微软研究院](https://www.edgepedia.org/zh/microsoft-research)，用于在生成过程中施加程序化约束，从而控制大语言模型的输出。开发者通过一次 API 调用表达所需的结构，例如 JSON、Python、HTML 或 SQL，该库便在推理层逐 token 地引导模型，使输出符合指定格式；微软称这可实现有保证的输出结构，并且与传统提示方式相比，延迟和成本降低 30%–50%。<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 开源 Python 库，通过一次 API 调用对任意语言模型进行受约束生成<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup> |
| 首次发布 | 2023 年 5 月，由微软研究院发布<sup>[2](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/downloads/)</sup> |
| 约束类型 | 正则表达式、在固定选项集上的 `select()`，以及任意上下文无关文法（前提是后端完全支持 Guidance）<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup><sup> • </sup><sup>[3](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)</sup> |
| 宣称的收益 | 100% 有保证的输出结构，延迟和成本降低 30%–50%<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup> |
| 文法引擎 | 自 v0.2.0（2025 年 1 月）起，采用基于 Rust 的 llguidance 库，对 128k 词表的 tokenizer 而言，每 token 约消耗 50 微秒单核 CPU 时间<sup>[4](https://github.com/guidance-ai/llguidance)</sup> |
| 后端 | Transformers、llama.cpp、OpenAI 等；通过 `pip install guidance` 安装<sup>[3](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)</sup> |
| 采用情况 | 微软引用称 GitHub 星标超过 1.9 万；仓库页面显示 2.18 万星标，采用 MIT 许可证<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup><sup> • </sup><sup>[5](https://github.com/guidance-ai/guidance)</sup> |

## 工作原理

Guidance 与传统提示的区别在于约束施加的位置。它不是在纯文本中要求模型产出合法 JSON、失败后重试，而是由 Guidance 程序在 Python 中把固定文本、生成调用和控制流交织起来，库在每一步约束采样，使只有符合文法的 token 才能被生成。<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup> `gen()` 调用可被约束为匹配某个正则表达式，例如用 `regex=r"\d+"` 强制给出整数答案；`select()` 调用则把输出限制为已知选项列表中的一项。<sup>[3](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)</sup> 只要后端模型完全支持 Guidance，该约束系统就能施加任意上下文无关文法。<sup>[3](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)</sup>

Token 快进是第二项效率机制。当文法确定某些 token 是预先已知的——例如某个 [HTML 元素](https://www.edgepedia.org/zh/html-element)的闭合标签——Guidance 会直接插入它们，而不是让模型去生成，从而省去对模型的前向传播，减少 GPU 占用。<sup>[3](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)</sup> 与提示链相比，Guidance 程序只是一次 API 调用，用户添加的文本会被批量处理，而不是随着执行展开而逐段生成。<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup>

接口是 Python 风格的：`system()`、`user()`、`assistant()` 等角色块构建对话上下文，生成的文本按名称捕获到模型对象上，可复用的自定义函数用 `@guidance` 装饰器标记。`Mock` 模型让开发者可以离线用文法校验候选字符串，无需调用模型 API。<sup>[3](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)</sup>

## 历史

微软于 2023 年 5 月发布 Guidance，称其能在单一连续流程中把生成、提示和逻辑控制交织起来，与语言模型处理文本的方式相吻合。<sup>[2](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/downloads/)</sup> 最初的版本使用基于 Handlebars 的模板语法，程序写成包含占位符和控制标签的字符串。<sup>[6](https://github.com/CatalinCighi/microsoft-guidance)</sup>

2025 年 1 月发布的 0.2.0 版是一次重大重构：核心文法处理从基于 Python 的 Earley 解析器迁移到基于 Rust 的 llguidance 库，发布说明称其为跨框架的最先进水平，并修复了旧引擎中的细微缺陷。<sup>[4](https://github.com/guidance-ai/llguidance)</sup><sup> • </sup><sup>[7](https://github.com/guidance-ai/guidance/releases)</sup> 0.3.x 系列的后续版本通过 litellm 增加了 Groq、Mistral 等模型提供方，加入了实验性的 SGLang 模型、带 [JSON Schema](https://www.edgepedia.org/zh/json-schema) 参数的 OpenAI 风格函数工具，并支持 GPT-5。<sup>[7](https://github.com/guidance-ai/guidance/releases)</sup>

## llguidance

Guidance 底层的文法引擎 llguidance 为大语言模型实现受约束解码，也称受约束采样或结构化输出。它可以施加任意上下文无关文法，并对 12.8 万 token 的 tokenizer 在约 50 微秒单核 CPU 时间内计算出 token 掩码——即能够合法延续当前前缀的 token 集合——且没有明显的启动开销。<sup>[4](https://github.com/guidance-ai/llguidance)</sup> 它接受 JSON Schema 的一大子集、正则表达式，以及 Lark 格式变体形式的上下文无关文法。<sup>[4](https://github.com/guidance-ai/llguidance)</sup>

该引擎的采用范围已远超 Guidance 本身。根据项目时间线，它于 2025 年 2 月并入 llama.cpp，2025 年 2 月并入 SGLang，2025 年 3 月并入 vLLM，2025 年 4 月并入 Chromium，并于 2025 年 5 月在 OpenAI 上线，用于驱动 JSON Schema 结构化输出。<sup>[4](https://github.com/guidance-ai/llguidance)</sup>

## 评估

Guidance 是 JSONSchemaBench 的试验平台，该基准由 EPFL 的 Saibo Geng 以及微软的 Michał Moskal、Eric Horvitz 和 Harsha Nori 等研究者于 2025 年 1 月提出。该基准包含约 1 万个真实世界的 JSON Schema，从三个维度评估受约束解码框架：生成符合约束的输出的效率、对多种约束类型的覆盖程度，以及生成输出的质量。共评估了六个框架：Guidance、Outlines、Llamacpp、XGrammar、OpenAI 和 Gemini。<sup>[8](https://arxiv.org/abs/2501.10868)</sup>

在基准结果中，Guidance 在所有任务上始终表现最佳，每项任务相较仅用语言模型的方法约提升 3%；作者报告称，无论采用哪个框架，受约束解码在所测试的推理任务上都比无约束设置取得更高性能。<sup>[9](https://openreview.net/pdf/87f0994dff5f854cb02110866e3c61a8e14c80f2.pdf)</sup> 二次分析报告称，Guidance 在实测模式覆盖率上领先，在 GlaiveAI 模式上达到 96%；而商业 API 在其支持的模式上达到 100% 合规，但覆盖的模式类型较少。<sup>[10](https://awesomeagents.ai/leaderboards/structured-output-json-leaderboard/)</sup>

## 相关工具

Guidance 属于受约束生成与语言模型编程工具这一族。LMQL（语言模型查询语言）把提示推广为文本提示与带模型输出约束的脚本的组合，其作者报告在按使用付费的 API 上可节省 26%–85% 的成本。<sup>[11](https://arxiv.org/abs/2212.06094)</sup> 一项研究性比较将面向大语言模型的编程系统分为高层系统（如 [LangChain](https://www.edgepedia.org/zh/langchain) 和 DSPy）与低层系统（如 LMQL、Guidance 和 SGLang）。<sup>[12](https://openreview.net/pdf?id=VqkAKQibpq)</sup> 另一个受约束生成库 Outlines 最初专注于正则表达式引导的生成。<sup>[13](https://www.ideals.illinois.edu/items/139529/bitstreams/450569/data.pdf)</sup> 文献中指出的一个实际区别是：托管模型会让基于文法的控制变得复杂，因为约束必须在计算 logits 的地方施加；而 Guidance 可与大多数能在本地托管的开源模型配合使用。<sup>[1](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)</sup>

## References

1. [Guidance: control LM output, Microsoft Research](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/)
2. [Guidance tools page, Microsoft Research](https://www.microsoft.com/en-us/research/project/guidance-control-lm-output/downloads/)
3. [Guidance README, guidance-ai/guidance on GitHub](https://github.com/guidance-ai/guidance/blob/main/README.md?plain=1)
4. [llguidance: Super-fast Structured Outputs, guidance-ai on GitHub](https://github.com/guidance-ai/llguidance)
5. [guidance-ai/guidance repository, GitHub](https://github.com/guidance-ai/guidance)
6. [microsoft-guidance fork README (original Handlebars syntax), GitHub](https://github.com/CatalinCighi/microsoft-guidance)
7. [Releases, guidance-ai/guidance on GitHub](https://github.com/guidance-ai/guidance/releases)
8. [Geng, S., et al. (2025). JSONSchemaBench: A Rigorous Benchmark of Structured Outputs for Language Models. arXiv:2501.10868](https://arxiv.org/abs/2501.10868)
9. [JSONSchemaBench: Evaluating Constrained Decoding with LLMs on Efficiency, Coverage and Quality, OpenReview](https://openreview.net/pdf/87f0994dff5f854cb02110866e3c61a8e14c80f2.pdf)
10. [Structured Output JSON Schema Leaderboard, Awesome Agents](https://awesomeagents.ai/leaderboards/structured-output-json-leaderboard/)
11. [Beurer-Kellner, L., Fischer, M., Vechev, M. (2022). Prompting Is Programming: A Query Language for Large Language Models. arXiv:2212.06094](https://arxiv.org/abs/2212.06094)
12. [Comparison among LMQL, Guidance, and SGLang, OpenReview](https://openreview.net/pdf?id=VqkAKQibpq)
13. [Ugare, S. (2025). Generating Structured Outputs from Language Models: Benchmark and Studies, University of Illinois](https://www.ideals.illinois.edu/items/139529/bitstreams/450569/data.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › 人工智能公司、人物与产品 › 人工智能产品与助手 › 人工智能智能体与开发者工具

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/guidance-microsoft

English original: https://www.edgepedia.org/guidance-microsoft
