Despite their impressive performance, large language models (LMs) still struggle with reliably generating complex output structures when not finetuned to follow the required output format exactly. To address this issue, grammar-constrained decoding (GCD) can be used to control the generation of LMs, guaranteeing that the output follows a given structure. Most existing GCD methods are, however, limited to specific tasks, such as parsing or code generation. In this work, we demonstrate that formal grammars can describe the output space for a much wider range of tasks and argue that GCD can serve as a unified framework for structured NLP tasks in general. For increased flexibility, we introduce input-dependent grammars, which allow the grammar to depend on the input and thus enable the generation of different output structures for different inputs. We then empirically demonstrate the power and flexibility of GCD-enhanced LMs on (1) information extraction, (2) entity disambiguation, and (3) constituency parsing. Our results indicate that grammar-constrained LMs substantially outperform unconstrained LMs or even beat task-specific finetuned models. Grammar constraints thus hold great promise for harnessing off-the-shelf LMs for a wide range of structured NLP tasks, especially where training data is scarce or finetuning is expensive. Code and data: https://github.com/epfl-dlab/GCD.
翻译:尽管大型语言模型(LLMs)展现出卓越性能,但在未经过微调以精确遵循所需输出格式的情况下,它们仍难以可靠地生成复杂输出结构。为解决这一问题,语法约束解码(GCD)可用于控制语言模型的生成过程,确保输出符合给定结构。然而,现有大多数GCD方法局限于特定任务,例如句法解析或代码生成。本研究证明,形式语法能够描述更广泛任务的输出空间,并论证GCD可作为通用框架处理结构化NLP任务。为增强灵活性,我们引入输入相关语法,使语法规则可根据输入动态调整,从而针对不同输入生成不同输出结构。随后,我们在(1)信息抽取、(2)实体消歧和(3)成分句法分析任务上实证展示了GCD增强型语言模型的强大能力与灵活性。结果表明,受语法约束的语言模型显著优于无约束模型,甚至超越任务特定的微调模型。因此,语法约束在利用现成语言模型处理各类结构化NLP任务方面展现出巨大潜力,尤其适用于训练数据稀缺或微调成本高昂的场景。代码与数据集:https://github.com/epfl-dlab/GCD。