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.
翻译:我们提出了一种方法,用于引导大语言模型(LLM)在无需微调的情况下生成遵循特定约定的结构化内容。通过利用基于协程的内容生成约束(借助预协商的上下文无关文法,CFG)在解码过程中引导LLM,使其输出符合形式语言规范。这增强了目标数据结构、类型或指令生成的稳定性和一致性,降低了应用开发复杂性。实验显示,对于长度超过36个和282个标记的领域特定语言(DSL),GPT-2和Gemma的错误率分别超过95%。我们引入了基于协程的DSL生成框架YieldLang,并在包括JSON和Mermaid流程图生成在内的多种任务上结合LLM对其进行评估。与基准相比,我们的方法将准确率提升了1.09至11.6倍,且LLM仅需约16.5%的样本即可有效生成JSON。这提升了LLM生成内容对计算机程序的可可用性。