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.
翻译:大型语言模型(LM)虽表现卓越,但若未针对特定输出格式进行微调,其生成复杂输出结构的可靠性仍显不足。为解决此问题,语法约束解码(GCD)可控制LM的生成过程,确保输出遵循既定结构。然而,现有GCD方法多局限于特定任务(如解析或代码生成)。本研究证明,形式化语法可描述更广泛任务的输出空间,并论证GCD可成为通用结构化NLP任务的统一框架。为增强灵活性,我们引入输入依赖型语法,允许语法结构随输入动态调整,从而为不同输入生成不同输出结构。实验在三个任务上验证了GCD增强型LM的效能与灵活性:(1)信息抽取、(2)实体消歧、(3)成分句法分析。结果表明,语法约束的LM显著优于无约束LM,甚至超越专用微调模型。语法约束为利用现成LM处理广泛结构化NLP任务(尤其训练数据稀缺或微调成本高昂的场景)提供了巨大潜力。代码与数据:https://github.com/epfl-dlab/GCD。