Skip to content

Commit dc1e98a

Browse files
committed
Document the use of the backends
1 parent 71b8037 commit dc1e98a

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

docs/features/advanced/backends.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Structured Generation Backends
3+
---
4+
5+
# Structured Generation Backends
6+
7+
Outlines relies on a structured generation backend to control text generation for steerable models such thah they conform to the output type provided. One of those backends is of course `outlines-core`, but you also have access to two other libraries that fulfill the same purpose: `llguidance` and `xgrammar`.
8+
9+
## Overview
10+
11+
To select the backend to use for your generation, provide a value for the `backend` argument when calling a model or a generator.
12+
13+
For instance:
14+
15+
```python
16+
from typing import Literal
17+
import outlines
18+
from transformers import AutoModelForCausalLM, AutoTokenizer
19+
20+
output_type = Literal["Paris", "London", "Rome", "Berlin"]
21+
22+
model = outlines.from_transformers(
23+
AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct"),
24+
AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
25+
)
26+
27+
result = model("What is the capital of France?", output_type, backend="llguidance")
28+
print(result) # 'Paris'
29+
30+
generator = outlines.Generaor(model, output_type)
31+
result = generator("What is the capital of France?", backend="xgrammar")
32+
print(result) # 'Paris'
33+
```
34+
35+
If you do not provide a value for the `backend` argument, the default value will be used. The default value depends on the type of output type:
36+
37+
- JSON schema: `outlines_core`
38+
- Regex: `outlines_core`
39+
- Context-free grammar: `llguidance`
40+
- Interegular FSM: `outlines_core`
41+
42+
## Features matrix
43+
44+
As mentioned previously, selecting the structured generation backend is only applicable to steerable models, so `Transformers`, `LlmaCpp` and `MLXLM`. Additionaly, some backends do not support some models within those or some output types.
45+
46+
| | outlines_core | llguidance | xgrammar |
47+
|---|---|---|---|
48+
| **Models** | | | |
49+
| Transformers ||||
50+
| LlamaCpp ||||
51+
| MLXLM ||||
52+
| **Output Types** | | | |
53+
| JSON Schema ||||
54+
| Regex ||||
55+
| Grammar ||||
56+
| FSM ||||

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ nav:
161161

162162
- Advanced:
163163
- Logits Processors: features/advanced/logits_processors.md
164+
- Structured Generation Backends: features/advanced/backends.md
164165

165166
- API Reference: api_reference/
166167

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ sglang = ["openai"]
5858
tgi = ["huggingface_hub"]
5959
transformers = ["accelerate", "datasets", "transformers"]
6060
vllm = ["openai"]
61+
xgrammar = ["xgrammar"]
62+
llguidance = ["llguidance"]
6163
test = [
6264
"pre-commit",
6365
"pytest",
@@ -89,6 +91,8 @@ test = [
8991
"dottxt",
9092
"sentencepiece",
9193
"mkdocs_gen_files",
94+
"llguidance",
95+
"xgrammar",
9296
]
9397
test-gpu=["outlines[test]", "vllm; sys_platform == 'linux'"]
9498

@@ -170,6 +174,8 @@ module = [
170174
"tf-keras.*",
171175
"mkdocs_gen_files.*",
172176
"jsonpath_ng.*",
177+
"llguidance.*",
178+
"xgrammar.*",
173179
]
174180
ignore_missing_imports = true
175181

0 commit comments

Comments
 (0)