Outlines 模型体系完全指南:统一接口下的结构化文本生成与后端矩阵
【免费下载链接】outlinesStructured Outputs项目地址: https://gitcode.com/GitHub_Trending/ou/outlines
Outlines 将各种推理客户端与引擎统一封装为"模型"(Model)对象,向开发者提供一套标准化的接口来生成结构化文本。本文以docs/features/models/index.md为核心,结合 src/outlines/models/init.py 与 src/outlines/models/base.py 等源码,系统讲解模型加载、直接调用、Generator 复用、流式与批量生成、功能特性矩阵、本地/服务端模型分类以及异步模型用法,帮助你为任意推理后端快速接入 Outlines 的结构化生成能力。
模型是什么:对推理客户端与引擎的统一封装
在 Outlines 中,模型是一层薄封装:它包裹一个推理客户端(client)或推理引擎(engine),对外提供标准化的生成接口。无论底层是transformers本地模型、OpenAI 远程 API,还是 TGI 服务端,上层代码面对的都是一致的调用方式。
从源码看,所有同步模型继承自 Model 基类,异步模型继承自AsyncModel基类。基类定义了统一的__call__、batch、stream三个公开方法,以及由子类实现的generate、generate_batch、generate_stream三个内部方法。每个模型实例还必须携带一个type_adapter属性(ModelTypeAdapter实例),它负责把用户输入的 prompt 与output_type转换成对应模型期望的参数格式——例如把 OpenAI 的输入转成messages、把输出类型转成response_format(见 openai.py 中的 OpenAITypeAdapter),对本地模型则生成对应的 logits processor。
所有模型类都配有对应的加载函数,命名规则为from_加上模型名的小写形式:
Transformers模型 →from_transformersOpenAI模型 →from_openaiTGI模型 →from_tgi- 其余类推:
from_anthropic、from_dottxt、from_gemini、from_llamacpp、from_lmstudio、from_mistral、from_mlxlm、from_ollama、from_sglang、from_vllm、from_vllm_offline
这些函数统一从 src/outlines/models/init.py 导出,并同时暴露到outlines顶层命名空间(见 src/outlines/init.py)。加载参数因提供商而异,具体参数请查阅对应模型文档。
创建模型实例后,你有两种使用方式:
- 直接调用模型:把 prompt 和可选的
output_type直接传给模型; - 先创建可复用的 Generator:把模型与
output_type绑定为一个Generator对象,之后反复调用。
两者在功能上是等价的。从 base.py 的__call__实现 可以看出,直接调用模型时,Outlines 内部其实就是用Generator(model, output_type, backend)创建生成器后再调用它,batch与stream方法同理(见 base.py L124-L216)。
模型的输入与输出约束
模型的输入可以是:
- 纯文本 prompt(所有模型均支持);
- 视觉或多模态输入(多模态模型支持);
- 聊天输入(
Chat实例,会话模型支持)。
关于输入格式的完整说明,请参阅 模型输入文档。
在生成结果时,你可以通过output_type约束输出格式,支持的类别包括 Python 基础类型、Literal/Enum多选、JSON Schema、正则与上下文无关文法(CFG)。完整说明请参阅 输出类型文档。
以下是一个完整的端到端示例(同步调用、带约束调用、Generator 复用三种方式):
from outlines import from_transformers, Generator import transformers # 创建模型 model = from_transformers( transformers.AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"), transformers.AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct"), ) # 方式一:直接调用 response = model("How many countries are there in the world", max_new_tokens=20) print(response) # 'There are 200 countries in the world.' # 方式二:直接调用并指定 output_type(约束为整数输出) response = model("How many countries are there in the world", int, max_new_tokens=20) print(response) # '200' # 方式三:先创建 Generator 再调用 generator = Generator(model, int) response = generator("How many countries are there in the world") print(response) # '200'注意:虽然传入了int作为输出类型,但 Outlines 的生成器始终返回字符串,需要自行将结果强转为目标类型,例如int(response)或对 Pydantic 模型使用Character.model_validate_json(result)。
流式生成:stream方法
部分模型支持流式生成,通过stream方法实现。它接收的参数与__call__相同,但返回的是一个迭代器而非单个字符串,适合逐块消费长输出。以 OpenAI 模型为例:
from outlines import from_openai import openai # 创建模型 model = from_openai( openai.OpenAI(), "gpt-4o" ) # 流式响应 for chunk in model.stream("Tell a short story about a cat.", max_tokens=50): print(chunk) # 'This...'从源码实现看,stream同样走Generator(model, output_type, backend)路线,然后调用生成器的stream方法(base.py L170-L216)。对于黑盒模型,流式响应直接来自底层客户端的stream=True调用,例如 OpenAI 的chat.completions.create(stream=True, ...)(见 openai.py L330-L339)。
批量生成:batch方法
部分模型支持批量处理,通过batch方法实现。它与__call__类似,但接收一个prompt 列表并返回一个字符串列表。以Transformers模型为例:
from outlines import from_transformers import transformers # 创建模型 model = from_transformers( transformers.AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"), transformers.AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct"), ) # 批量调用 response = model.batch(["What's the capital of Latvia?", "What's the capital of Estonia?"], max_new_tokens=20) print(response) # ['Riga', 'Tallinn']注意batch并非所有模型都支持(见下文特性矩阵)。以 OpenAI 为例,其generate_batch方法直接抛出NotImplementedError,因为openai库本身不支持批量推理(openai.py L289-L297)。
功能特性矩阵:15 个后端的能力全景
下表按字母序汇总了 Outlines 集成各模型在输出类型与生成特性上的支持情况(来源:docs/features/models/index.md):
| 能力 | Anthropic | Dottxt | Gemini | LlamaCpp | LM Studio | MLXLM | Mistral | Ollama | OpenAI | SGLang | TGI | Transformers | Transformers MultiModal | VLLM | VLLMOffline |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 输出类型 | |||||||||||||||
| 简单类型 | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| JSON Schema | ❌ | ✅ | 🟠 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 多选 | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 正则 | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| 文法(CFG) | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | 🟠 | ❌ | ✅ | ✅ | ✅ | ✅ |
| 生成特性 | |||||||||||||||
| 异步 | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ |
| 流式 | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ |
| 视觉 | ✅ | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ |
| 批量 | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ✅ |
读表要点:
- 🟠 表示部分支持:Gemini 对 JSON Schema、SGLang 对文法(CFG)属于部分能力;
- 简单类型、多选、正则、文法这类需要精细控制 token 序列的输出类型,往往只有本地模型(或支持原生约束的服务端)才完整支持;
- 视觉输入(Vision)集中在对话类 API 模型(Anthropic、Gemini、LM Studio、Mistral、Ollama、OpenAI)以及 TransformersMultiModal 上;
- 批量能力最完整的是 MLXLM、Transformers、TransformersMultiModal 与 VLLMOffline 等本地/离线模型。
模型分类:本地模型与服务端模型
根据文本生成发生的位置,模型分为两类。
本地模型(Local Models)
本地模型在实例化所使用的推理库对象内部完成生成。由于 Outlines 可以直接通过 logits processor 介入生成过程,因此所有结构化输出类型均可用。源码中本地模型被归类为SteerableModel(可操纵模型),定义于 src/outlines/models/init.py L32:LlamaCpp、MLXLM、Transformers(注意VLLMOffline在文档分类上属于本地模型,但类型归属上被列为黑盒)。
可用的本地模型:
- LlamaCpp
- MLXLM
- Transformers
- TransformersMultiModal
- VLLMOffline
以Transformers为例,加载后可直接用全部输出类型做约束生成:
from typing import Literal import outlines from transformers import AutoModelForCausalLM, AutoTokenizer output_type = Literal["Paris", "London", "Rome", "Berlin"] model = outlines.from_transformers( AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"), AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct") ) result = model("What is the capital of France?", output_type, max_new_tokens=10, temperature=0) print(result) # 'Paris'从 generator.py 的 SteerableGenerator 可以看清其原理:构造生成器时,output_type先被python_types_to_terms转成中间表示,再根据类型走get_cfg_logits_processor、get_json_schema_logits_processor或get_regex_logits_processor三条路径编译成 logits processor 并缓存复用;调用时则先reset()再传给模型的generate(见 generator.py L280-L300)。测试用例见 tests/test_generator.py。
服务端模型(Server-Based Models)
服务端模型用一个客户端初始化,客户端向实际负责文本生成的服务器发送请求。由于生成发生在远端,Outlines 对生成过程的控制有限,部分输出类型不受支持。生成服务器可以是远程的(如 OpenAI、Anthropic),也可以是本地的(如 SGLang)。
可用的服务端模型:
- Anthropic
- Dottxt
- Gemini
- LM Studio
- Mistral
- Ollama
- OpenAI
- SgLang
- TGI
- VLLM
这类模型在源码中归类为BlackBoxModel(黑盒模型),见 src/outlines/models/init.py L33-L45。对于黑盒模型,output_type不会被编译成 logits processor,而是直接透传给底层模型由其原生机制处理(见 generator.py BlackBoxGenerator L28-L35)。例如 OpenAI 的OpenAITypeAdapter.format_output_type会把 JSON 类型的output_type转成response_format={"type": "json_schema", ...},把空字典转成 JSON 模式(json_object),并对Regex、CFG直接抛出TypeError明确拒绝(openai.py L141-L209),这与特性矩阵中 OpenAI 不支持正则、文法的结论一致。
以TGI为例,它依赖 HuggingFace 的 Text Generation Inference 服务器与huggingface_hub客户端,安装可选依赖pip install "outlines[tgi]",用from_tgi传入InferenceClient或AsyncInferenceClient即可获得TGI或AsyncTGI实例:
import outlines import huggingface_hub # 创建推理客户端(指向本地或远程 TGI 服务器) client = huggingface_hub.InferenceClient("http://localhost:11434") async_client = huggingface_hub.AsyncInferenceClient("http://localhost:11434") # 创建同步模型与异步模型 sync_model = outlines.from_tgi(client) async_model = outlines.from_tgi(async_client)异步模型:Async<ModelName>实例
部分模型提供异步版本。使用方式很简单:向加载函数传入提供商对象的异步版本,即可得到Async<ModelName>实例,其方法与功能与同步实例保持一致(调用时使用await)。
例如:
from outlines import from_tgi from huggingface_hub import AsyncInferenceClient model = from_tgi( AsyncInferenceClient("http://localhost:8000/v1") ) print(type(model)) # outlines.models.tgi.AsyncTGI拥有异步版本的模型如下:
- LM Studio
- Mistral
- Ollama
- OpenAI
- SgLang
- TGI
- VLLM
异步模型的实现同样可见于 base.py 的 AsyncModel 基类:__call__、batch、stream均为协程或异步迭代器,内部同样委托给Generator完成;对应实例在 models/init.py 的 AsyncBlackBoxModel 联合类型 中统一导出(AsyncDottxt、AsyncLMStudio、AsyncMistral、AsyncOllama、AsyncOpenAI、AsyncTGI、AsyncSGLang、AsyncVLLM)。
选择模型的实践建议
结合特性矩阵与模型分类,可以按以下思路选择:
- 需要最完整的结构化输出能力(简单类型、JSON、多选、正则、文法全覆盖):优先选择本地模型,如
Transformers、MLXLM;从源码结构看,LlamaCpp、TransformersMultiModal、VLLMOffline也被列为本地模型,结构化能力同样完整; - 主要使用 OpenAI 兼容 API 或云端服务:选择
OpenAI、Anthropic、Gemini等,但要确认所需输出类型(如正则、文法)是否被服务端原生支持; - 需要异步或流式体验:参考矩阵中 Async 与 Streaming 两行,
Ollama、OpenAI、TGI、SGLang、VLLM是同时具备两者的主要选项; - 需要批量吞吐:本地模型
Transformers、MLXLM、VLLMOffline支持batch,服务端模型则普遍不支持。
最后提醒:output_type的具体支持范围因模型而异,使用前请查阅目标模型的独立文档页,并结合上文特性矩阵确认能力边界,避免在不受支持的模型上使用正则或文法约束导致运行时错误。
【免费下载链接】outlinesStructured Outputs项目地址: https://gitcode.com/GitHub_Trending/ou/outlines
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考