A Guide to Structured Outputs Using Constrained Decoding
We often want specific outputs when interacting with generative language models. This is especially true in programming domains, where a generated output may become a direct input to a function. But sometimes, no matter how explicit you are with your instructions, generative language models will get too creative, deviate off task, or simply succumb to their urge to yap. Fortunately, there are techniques that ensure language models only return outputs that conform to your requirements. This article serves as a practitioner's guide for perhaps the most powerful of these techniques: constrained decoding. We'll cover what structured generation and constrained decoding are, how they work, best practices, useful patterns, and pitfalls to avoid. This article is a living document being continuously updated as the field evolves. Terminology such as "structured generation" and "constrained decoding" do not have consensus definitions, and their scopes are evolving over time — the discussion here
Introduction We often want specific outputs when interacting with generative language models. This is especially true in programming domains, where a generated output may become a direct input to a function. But sometimes, no matter how explicit you are with your instructions, generative language models will get too creative, deviate off task, or simply succumb to their urge to yap . Fortunately, there are techniques that ensure language models only return outputs that conform to your requirements. This article serves as a practitioner's guide for perhaps the most powerful of these techniques:
related reading
- GitHub - guidance-ai/guidance: A guidance language for controlling large language models.github.com
- GitHub - brexhq/prompt-engineering: Tips and tricks for working with Large Language Models like OpenAI's GPT-4.github.com
- Efficient Guided Generation for Large Language Modelsarxiv.org
- GenAI Handbookgenai-handbook.github.io
- Patterns for Building LLM-based Systems & Productseugeneyan.com
- Prompt Engineering Guide | Prompt Engineering Guidepromptingguide.ai
- Structured Decoding in vLLM: A Gentle Introductionbentoml.com
- structured decoding, a guide for the impatientaarnphm.xyz
- How to generate text: using different decoding methods for language generation with Transformershuggingface.co
- Speculative Decoding - philkravphilkrav.com
- GPT in 60 Lines of NumPy | Jay Modyjaykmody.com
- Productizing Large Language Modelsblog.replit.com