用故事化叙述重构代码题,让大模型更准地思考和写代码。
StoryCoder: Narrative Reformulation for Structured Reasoning in LLM Code Generation

- 把代码问题改写成包含任务、约束和示例的故事式描述
- 零样本下平均提升18.7%的通过率,错误减少且代码更模块化
- 适合需要精准推理的编程任务,尤其对小模型效果更明显
高效代码生成不仅依赖模型能力,还需精心设计的问题表征以引导模型推理与规划。现有方法多通过增加推理步骤或注入特定思维结构来改进,但未改变问题中分散的条件信息。受人类将零散信息整合为连贯解释方式的启发,我们提出StoryCoder——一种叙事重构框架,将代码生成问题转化为连贯的自然语言叙事,提供比简单改写更丰富的上下文结构。每个叙事包含任务概述、约束条件和示例测试用例,由选定算法类型与问题类别指导。在HumanEval、LiveCodeBench和CodeForces上对11个模型的实验表明,该方法带来持续提升,零样本下平均通过率(pass@10)提高18.7%。分析还显示,叙事重构能引导模型采用正确算法策略,减少实现错误,并促进更模块化的代码结构。这些收益依赖于叙事连贯性与问题类别的匹配,说明无论模型规模或架构如何,结构化问题表征对代码生成至关重要。代码已开源:https://github.com/gu-ni/StoryCoder。
原文摘要 · Abstract (English)
Effective code generation requires both model capability and a problem representation that carefully structures how models reason and plan. Existing approaches augment reasoning steps or inject specific structure into how models think, but leave scattered problem conditions unchanged. Inspired by the way humans organize fragmented information into coherent explanations, we propose StoryCoder, a narrative reformulation framework that transforms code generation questions into coherent natural language narratives, providing richer contextual structure than simple rephrasings. Each narrative consists of three components: a task overview, constraints, and example test cases, guided by the selected algorithm and genre. Experiments across 11 models on HumanEval, LiveCodeBench, and CodeForces demonstrate consistent improvements, with an average gain of 18.7% in zero-shot pass@10. Beyond accuracy, our analyses reveal that narrative reformulation guides models toward correct algorithmic strategies, reduces implementation errors, and induces a more modular code structure. The analyses further show that these benefits depend on narrative coherence and genre alignment, suggesting that structured problem representation is important for code generation regardless of model scale or architecture. Our code is available at https://github.com/gu-ni/StoryCoder.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。