HackerPapers

arXiv:2403.01632cs.LG

SynCode: LLM Generation with Grammar Augmentation

Shubham Ugare, Tarun Suresh, Hangoo Kang, Sasa Misailovic, Gagandeep Singh

Early discovery. Linked by one identifiable commenter. This paper is outside the main ranking; a mention is not an endorsement.

Abstract

LLMs are widely used in complex AI applications. These applications underscore the need for LLM outputs to adhere to a specific format, for their integration with other components in the systems. Typically the format rules e.g., for data serialization formats such as JSON, YAML, or Code in Programming Language are expressed as context-free grammar (CFG). Due to the hallucinations and unreliability of LLMs, instructing LLMs to adhere to specified syntax becomes an increasingly important challenge. We present SynCode, a novel framework for efficient and general syntactical decoding with LLMs, to address this challenge. SynCode ensures soundness and completeness with respect to the CFG of a formal language, effectively retaining valid tokens while filtering out invalid ones. SynCode uses an offline-constructed, efficient lookup table, the DFA mask store, derived from the DFA of the language's grammar for efficient generation. SynCode seamlessly integrates with any language defined by CFG, as evidenced by experiments focusing on generating JSON, Python, and Go outputs. Our experiments evaluating the effectiveness of SynCode for JSON generation demonstrate that SynCode eliminates all syntax errors and significantly outperforms state-of-the-art baselines. Furthermore, our results underscore how SynCode significantly reduces 96.07% of syntax errors in generated Python and Go code, showcasing its substantial impact on enhancing syntactical precision in LLM generation. Our code is available at https://github.com/uiuc-focal-lab/syncode

arXiv comments
Journal reference
DOI
Subjects
cs.LGMachine Learning cs.FLFormal Languages and Automata Theory cs.PLProgramming Languages cs.SESoftware Engineering

On Hacker News

Linked by 1 identifiable commenter in 1 comment across 1 thread, from May 2024 to May 2024.

Comments linking this paper, per year.

Discussed in comments1 thread

    • SynCode is efficient (<10% overhead) and provably sound in generating syntactical LLM output for any LR(1) grammar. Currently supports built-in grammars for Python, Go, Java, JSON, SQL, first-order logic, and support for many grammars is WIP. We have added some interesting…

      sugareView on HNParent context

Submitted to Hacker News

Connected through shared HN threads, not inferred similarity or a recommendation.