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任务。为提升灵活性,我们引入输入依赖型语法,使语法能根据输入变化,从而针对不同输入生成不同输出结构。随后,我们在(1)信息抽取、(2)实体消歧和(3)成分句法分析任务上实证展示增强GCD的LM的强大能力与灵活性。结果表明,经语法约束的LM显著优于未约束的LM,甚至超越特定任务微调模型。因此,语法约束在利用现成LM处理广泛结构化NLP任务方面前景广阔,尤其在训练数据稀缺或微调成本高昂的场景下。代码与数据:https://github.com/epfl-dlab/GCD。