|
| 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 | ✅ | ❌ | ❌ | |
0 commit comments