We propose a method to guide Large Language Models (LLMs) in generating structured content adhering to specific conventions without fine-tuning. By utilizing coroutine-based content generation constraints through a pre-agreed context-free grammar (CFG), LLMs are directed during decoding to produce formal language compliant outputs. This enhances stability and consistency in generating target data structures, types, or instructions, reducing application development complexities. Experimentally, error rates of GPT-2 and Gemma exceed 95% for DSLs longer than 36 and 282 tokens, respectively. We introduce YieldLang, a coroutine-based DSL generation framework, and evaluate it with LLMs on various tasks including JSON and Mermaid flowchart generation. Compared to benchmarks, our approach improves accuracy by 1.09 to 11.6 times, with LLMs requiring only about 16.5% of the samples to generate JSON effectively. This enhances usability of LLM-generated content for computer programs.
翻译:本文提出一种方法,在不进行微调的情况下,引导大语言模型生成符合特定约定的结构化内容。通过利用基于协程的内容生成约束(通过预定义的上下文无关文法),在解码过程中引导大语言模型产生符合形式语言规范的输出。这增强了生成目标数据结构、类型或指令时的稳定性和一致性,降低了应用开发复杂度。实验表明,对于超过36个token和282个token的领域特定语言,GPT-2和Gemma的错误率分别超过95%。我们引入了基于协程的DSL生成框架YieldLang,并在包括JSON和Mermaid流程图生成在内的多种任务上对大语言模型进行了评估。与基准方法相比,我们的方法将准确率提升了1.09至11.6倍,且大语言模型仅需约16.5%的样本即可有效生成JSON。这提升了大语言模型生成内容在计算机程序中的可用性。