Large language models (LLMs) can learn to perform a wide range of natural language tasks from just a handful of in-context examples. However, for generating strings from highly structured languages (e.g., semantic parsing to complex domain-specific languages), it is challenging for the LLM to generalize from just a few exemplars. We explore $\textbf{grammar prompting}$ as a simple approach for enabling LLMs to use external knowledge and domain-specific constraints, expressed through a grammar expressed in Backus--Naur Form (BNF), during in-context learning. Grammar prompting augments each demonstration example with a specialized grammar that is minimally sufficient for generating the particular output example, where the specialized grammar is a subset of the full DSL grammar. For inference, the LLM first predicts a BNF grammar given a test input, and then generates the output according to the rules of the grammar. Experiments demonstrate that grammar prompting can enable LLMs to perform competitively on a diverse set of DSL generation tasks, including semantic parsing (SMCalFlow, Overnight, GeoQuery), PDDL planning, and even molecule generation (SMILES).
翻译:大语言模型(LLMs)能够仅通过少量上下文示例学习执行广泛的自然语言任务。然而,当涉及生成高度结构化语言字符串(如面向复杂领域特定语言的语义解析)时,LLMs难以从少量示例中进行泛化。我们探索了一种名为**语法提示**的简单方法,使LLMs在上下文学习过程中能够利用通过巴科斯-诺尔范式(BNF)语法表达的外部知识和领域特定约束。语法提示为每个示例增强一个专门语法,该语法是生成特定输出示例的最小充分子集(完整DSL语法的子集)。在推理阶段,LLM首先根据测试输入预测BNF语法,再依据该语法规则生成输出。实验表明,语法提示方法能使LLMs在多种DSL生成任务中取得竞争力,包括语义解析(SMCalFlow、Overnight、GeoQuery)、PDDL规划乃至分子生成(SMILES)。