计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
outlines【效率与协作】Generates guaranteed-valid structured text from LLMs with Outlines, the Python library that constrains token sampling to a JSON schema, regex, choice list or grammar. Use when a user asks for reliable JSON from a local or hosted model, classification into fixed labels, regex-shaped output, grammar-constrained generation, or structured output with vLL…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Outlines forces a model's output to match a declared type. For local backends it compiles the type (Pydantic model, regex, choice list, context-free grammar) into a constraint on token sampling, so the first generation is already valid and no retry loop is needed. For hosted backends (OpenAI, Anthropic, Gemini, Ollama, vLLM server) it passes the type to the provider's own structured-output feature through one common interface.
Checked against Outlines 1.3 (October 2026). The 1.x API is model(prompt, output_type). The 0.x calls (outlines.generate.json(...), outlines.models.transformers(...), outlines.generate.regex(...)) were removed; code using them fails on current versions.
pip install "outlines[transformers]" # local Hugging Face models (also pulls torch)
pip install "outlines[ollama]" # or: openai, anthropic, gemini, llamacpp, vllm, mlxlm
import outlines
from transformers import AutoModelForCausalLM, AutoTokenizer
name = "microsoft/Phi-3-mini-4k-instruct"
model = outlines.from_transformers(
AutoModelForCausalLM.from_pretrained(name),
AutoTokenizer.from_pretrained(name),
)
Other loaders: outlines.from_ollama(client, "llama3.2"), outlines.from_openai(client, model_name), outlines.from_vllm(openai_client, model_name) for a running vLLM server (OpenAI client pointed at http://127.0.0.1:8000/v1), outlines.from_vllm_offline(llm), outlines.from_llamacpp(llm), plus from_anthropic, from_gemini, from_mistral, from_sglang, from_tgi, from_mlxlm.
The second argument of the call is the constraint. Always pass max_new_tokens on the Transformers backend: the default of 20 truncates JSON.
from enum import Enum
from typing import Literal
from pydantic import BaseModel, Field
from outlines.types import Regex, Choice, CFG
class ReviewAnalysis(BaseModel):
sentiment: Literal["positive", "negative", "neutral"]
score: float = Field(ge=0, le=1)
topics: list[str]
raw = model("Analyze: 'Great product, slow shipping'", ReviewAnalysis, max_new_tokens=200)
review = ReviewAnalysis.model_validate_json(raw) # the call returns a JSON string
phone = model("Support phone number:", Regex(r"\(\d{3}\) \d{3}-\d{4}"), max_new_tokens=20)
label = model("Is this spam? 'You won $1000000!!!'", Choice(["spam", "ham", "uncertain"]))
count = model("How many minutes in an hour?", int, max_new_tokens=5)
Also accepted: Literal[...], Enum classes, plain types (int, float, bool, datetime.date), JsonSchema(schema_string), and CFG(lark_grammar_string) for a Lark grammar (not every backend supports CFG).
answers = model.batch(["Capital of Lithuania?", "Capital of Latvia?"], max_new_tokens=20)
generator = outlines.Generator(model, ReviewAnalysis) # compile the constraint once
raw = generator("Analyze: 'Arrived broken'", max_new_tokens=200)
Request: "Sort these tickets into billing, bug or feature, nothing else."
tickets = ["I was charged twice in September", "Export button does nothing on Safari"]
labels = Choice(["billing", "bug", "feature"])
print(model.batch([f"Ticket: {t}\nCategory:" for t in tickets], labels, max_new_tokens=8))
Result: ['billing', 'bug']. The output can only be one of the three strings, so no post-processing is needed.
Request: "Get name, city and plan from this signup email using our vLLM server."
import openai, outlines
from pydantic import BaseModel
class Signup(BaseModel):
name: str
city: str
plan: str
client = openai.OpenAI(base_url="http://127.0.0.1:8000/v1", api_key="local-vllm-key")
model = outlines.from_vllm(client, "microsoft/Phi-3-mini-4k-instruct")
raw = model("Priya Nair from Pune signed up for the Growth plan.", Signup)
print(Signup.model_validate_json(raw))
Result: Signup(name='Priya Nair', city='Pune', plan='Growth'), validated against the schema.
Generator rather than recompiling.