about
Grammar Prompting for Domain-Specific Language Generation with LLMs (arxiv.org)
1 point by yeesian on Oct 31, 2023 | hide | past | pdf | 1 comment on HN

In plain words: Instead of showing only sample answers, it adds the few grammar rules each answer follows, so the model learns the output's strict shape. For a new question it first picks the needed rules, then writes an answer obeying them, matching the best existing approaches.

Abstract · Grammar Prompting for Domain-Specific Language Generation with Large Language Models

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 propose \emph{grammar prompting}, a simple approach to enable LLMs to use external knowledge and domain-specific constraints, expressed through a grammar 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 SMILES-based molecule generation.

Bailin Wang, Zi Wang, Xuezhi Wang, Yuan Cao, Rif A. Saurous, Yoon Kim
arXiv:2305.19234 · cs.CL, cs.AI · submitted May 30, 2023 · updated Nov 3, 2023
abstract · pdf · html · camera-ready version

add comment on HN

Relatedly:

Llama: Add grammar-based sampling - https://news.ycombinator.com/item?id=36819906 (105 comments)

ReLLM: Exact Structure for Large Language Model Completions - https://news.ycombinator.com/item?id=35829399 (13 comments)

Show HN: Structured output from LLMs without reprompting - https://news.ycombinator.com/item?id=36750083 (54 comments)

Show HN: LLMs can generate valid JSON 100% of the time - https://news.ycombinator.com/item?id=37125118 (303 comments)

A guidance language for controlling LLMs - https://news.ycombinator.com/item?id=35963936 (190 comments)

LMQL: A query language for programming (large) language models - https://news.ycombinator.com/item?id=35956484 (12 comments)