自动从文档和执行数据中学习语法,提升语言模型生成有效程序的能力
Learning Context-Free Grammars for Grammar-Constrained Decoding via Declarative Agentic Programming with Guarantees
- 用语言模型驱动的代理自动学习目标语言的上下文无关语法
- 生成的语法在未见数据上精度接近完美,执行效率提升3.8倍
- 适合需要可靠程序生成的开发与运维场景,尤其对小众领域工具有效
语言模型(LMs)越来越多地通过领域特定语言(DSL)调用外部服务。然而,由于这些DSL资源少且晦涩,语言模型常生成语法错误的程序。语法约束解码可避免此类问题,但需依赖语法规范。通常以目标语言的上下文无关文法形式存在,而第三方DSL往往缺乏此类规范。本文提出一个名为Autogrammar的智能体,能从文档和执行数据中自动学习上下文无关文法。该智能体被形式化为一个克里普克结构,其非确定性选择由语言模型决定,实现通过线性时序逻辑约束进行声明式行为控制。我们在三个DSL(Amazon CloudWatch Logs Insights、Dynatrace Query Language、Datadog Search Syntax)上评估了四种Autogrammar变体,结果表明:生成的语法在未见数据上达到近似完美的精确率;时间约束使执行时间减少3.8倍,且精度无显著下降;执行数据至关重要,文档则可忽略;使用Autogrammar生成的语法进行语法约束解码,在十项真实任务中有八项显著提升端到端语言模型性能,达到甚至超过专业维护语法的水平。相比之下,现有基于语言模型的基线及先进形式化方法生成的文法表现明显更差。
原文摘要 · Abstract (English)
Language models (LMs) are increasingly used to interact with external services via programs written in domain-specific languages (DSLs). Unfortunately, since DSLs are often low-resource and esoteric, LMs frequently produce syntactically invalid programs in these languages. Grammar-constrained decoding can eliminate such failures, but requires syntactic constraints. These are usually in the form of a context-free grammar for the target language, an artifact that is hard to come by for third-party DSLs. In this work, we define an agent, called Autogrammar, that automatically learns context-free grammars from documentation and execution data. Autogrammar is formalized as a Kripke structure whose nondeterministic choices are resolved by a language model, enabling declarative control of agent behavior via linear temporal logic constraints. We evaluate four versions of Autogrammar on three DSLs (i.e., Amazon CloudWatch Logs Insights, Dynatrace Query Language, and Datadog Search Syntax) and find that it generates grammars that achieve near perfect precision on unseen data; that temporal restrictions reduce execution time by 3.8x without incurring statistically-significant loss in precision; that execution data is crucial while documentation is dispensable; and that grammar-constrained decoding using Autogrammar-generated grammars significantly improves end-to-end LM performance on eight out of ten real tasks, matching or exceeding the performance of a professionally-maintained grammar. In comparison, the context-free grammars generated by existing LM baselines and a state-of-the-art formal technique perform significantly worse over the same evaluation.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。