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.
翻译:尽管大型语言模型(LMs)性能卓越,但在未经过微调以严格遵循特定输出格式的情况下,它们仍难以可靠地生成复杂输出结构。为解决此问题,可借助语法约束解码(GCD)控制语言模型的生成过程,确保输出符合给定结构。然而,现有大多数GCD方法局限于特定任务(如句法解析或代码生成)。本研究证明,形式语法能描述更广泛任务的输出空间,并论证GCD可作为结构化NLP任务统一框架的潜力。为提升灵活性,我们引入输入依赖型语法,使语法规则可根据输入动态调整,从而针对不同输入生成差异化输出结构。随后,我们通过(1)信息抽取、(2)实体消歧和(3)成分句法分析三项任务,实证展示了增强型GCD语言模型的效能与灵活性。结果表明,受语法约束的语言模型显著优于无约束语言模型,甚至能超越任务特定微调模型。因此,语法约束在利用现成语言模型处理各类结构化NLP任务方面具有巨大潜力——尤其在训练数据稀缺或微调成本高昂的场景下。代码与数据:https://github.com/epfl-dlab/GCD。