Large Language Models (LLMs) are increasingly used in production systems, powering applications such as chatbots, summarization, and question answering. Despite their success, controlling the length of their response remains a significant challenge, particularly for tasks requiring structured outputs or specific levels of detail. In this work, we propose a method to adapt pre-trained decoder-only LLMs for precise control of response length. Our approach incorporates a secondary length-difference positional encoding (LDPE) into the input embeddings, which counts down to a user-set response termination length. Fine-tuning with LDPE allows the model to learn to terminate responses coherently at the desired length, achieving mean token errors of less than 3 tokens. We also introduce Max New Tokens++, an extension that enables flexible upper-bound length control, rather than an exact target. Experimental results on tasks such as question answering and document summarization demonstrate that our method enables precise length control without compromising response quality.
翻译:大型语言模型(LLMs)正日益广泛应用于生产系统,为聊天机器人、文本摘要和问答等应用提供支持。尽管取得了显著成功,控制其响应长度仍然是一个重大挑战,特别是在需要结构化输出或特定细节层次的任务中。本研究提出一种方法,用于适配预训练的仅解码器LLMs以实现响应长度的精确控制。我们的方法在输入嵌入中引入了一种辅助的长度差分位置编码(LDPE),该编码以用户设定的响应终止长度为终点进行倒计时。通过LDPE进行微调,使模型能够学会在期望长度处连贯地终止响应,实现平均词元误差小于3个词元。我们还提出了Max New Tokens++扩展机制,该机制支持灵活的上限长度控制而非精确目标长度。在问答和文档摘要等任务上的实验结果表明,我们的方法能够在保持响应质量的同时实现精确的长度控制。