Chaining language model (LM) calls as composable modules is fueling a new way of programming, but ensuring LMs adhere to important constraints requires heuristic "prompt engineering". We introduce LM Assertions, a programming construct for expressing computational constraints that LMs should satisfy. We integrate our constructs into the recent DSPy programming model for LMs, and present new strategies that allow DSPy to compile programs with LM Assertions into more reliable and accurate systems. We also propose strategies to use assertions at inference time for automatic self-refinement with LMs. We report on four diverse case studies for text generation and find that LM Assertions improve not only compliance with imposed rules but also downstream task performance, passing constraints up to 164% more often and generating up to 37% more higher-quality responses. Our reference implementation of LM Assertions is integrated into DSPy at https://github.com/stanfordnlp/dspy
翻译:将语言模型(LM)调用链接为可组合模块正催生一种新的编程范式,但确保LM遵循关键约束仍需依赖启发式的“提示工程”。我们提出LM断言这一编程构造,用于表达LM应满足的计算约束。我们将该构造集成到最新的DSPy LM编程模型中,并提出新策略使DSPy能够将包含LM断言的程序编译成更可靠、更准确的系统。同时,我们提出在推理阶段利用断言实现LM自动自优化的策略。我们在四个不同的文本生成案例研究中发现,LM断言不仅提升了对既定规则的遵守程度,还改善了下游任务性能——约束满足率最高提升164%,高质量响应生成量最高提升37%。我们的LM断言参考实现已集成至DSPy,代码地址:https://github.com/stanfordnlp/dspy