Guidance: a programming paradigm for steering language models

Guidance is a programming paradigm for steering language models, and it allows users to constrain generation, e.g. with regex and CFGs.

Verified

On this page

What it is

Guidance is a programming paradigm for steering language models. Guidance is available through PyPI and supports a variety of backends (Transformers, llama.cpp, OpenAI, etc.). When using Guidance, you can work with large language models using common Python idioms.

Core facts

Read by a script from the official API, not rewritten by a model.

License
MIT
Languages
Jupyter Notebook
Maintainer
guidance-ai
Verified

Main features

  1. Guidance allows users to constrain generation, for example with regex and CFGs.

  2. Guidance also allows users to interleave control (conditionals, loops, tool use) with generation.

  3. The project says Guidance reduces latency and cost versus conventional prompting or fine-tuning.

  4. In a Jupyter notebook, Guidance provides a widget that the project says gives a richer user experience.

  5. When the output has to be an item from a list known in advance, Guidance provides a select function.

  6. A JSON schema is a context free grammar, so it can be used to constrain an LLM using Guidance.

  7. The JSON generation sample is based on a Pydantic model.

  8. The blood pressure generated in that sample is not one the model will have seen for a human.

  9. Users can create their own Guidance functions that interact with language models, and these functions are marked using the @guidance decorator.

  10. A decorated function can be used like gen or select.

  11. When such a function is used, the language_model argument is filled in automatically.

  12. Guidance functions can be composed to construct a full context free grammar.

  13. In the HTML example, a function is marked as stateless to indicate that it is intended for composing a grammar.

  14. The HTML example adds the HTML start tag, then generates the header, then the body, and then appends the ending HTML tag.

  15. The HTML example also provides a wrapper that allows setting the temperature of the generation and capturing the generated page from the Model object.

  16. The constraints imposed by a grammar often mean that some tokens are known in advance.

  17. Guidance does not need the model to generate these tokens. Instead, it can insert them into the generation.

  18. According to the project, this saves forward passes through the model, which reduces GPU usage.

  19. In the HTML generation example, if the last opened tag was h1, then as soon as the model generates the characters that start a closing tag, Guidance can fill in the rest without needing the model to perform a forward pass.

  20. The project says that when generating JSON, a substantial number of tokens can often be fast-forwarded because of the structural constraints the schema imposes.

Getting started

  1. If you already have the backend required for your model, you can run the install command.

  2. A gen call can be constrained to match a regular expression.

  3. Generated text can be captured with Guidance.

  4. When iterating on constraints, you can validate candidate strings locally and test a full run with the Mock model.

From the README
Shell
pip install guidance
From the README
python
from guidance import system, user, assistant, gen
from guidance.models import Transformers

# Could also do LlamaCpp or many other models
phi_lm = Transformers("microsoft/Phi-4-mini-instruct")

# Model objects are immutable, so this is a copy
lm = phi_lm

with system():
    lm += "You are a helpful assistant"

with user():
    lm += "Hello. What is your name?"

with assistant():
    lm += gen(max_tokens=20)

print(lm)
From the README
python
# Get a new copy of the Model
lm = phi_lm

with system():
    lm += "You are a helpful assistant"

with user():
    lm += "Hello. What is your name?"

with assistant():
    lm += gen(name="lm_response", max_tokens=20)

print(f"{lm['lm_response']=}")
From the README
Plain text
lm['lm_response']='I am Phi, an AI developed by Microsoft. How can I help you today?'
From the README
python
lm = phi_lm

with system():
    lm += "You are a teenager"

with user():
    lm += "How old are you?"

with assistant():
    lm += gen("lm_age", regex=r"\d+", temperature=0.8)

print(f"The language model is {lm['lm_age']} years old")
From the README
Plain text
The language model is 13 years old

Who it is for

Guidance can ensure that output conforms to a context free grammar, so long as the backend LLM has full support for Guidance. The Guidance function example that builds a simple HTML webpage is not a full implementation of HTML.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.