PromptPex: Automatic Test Generation for Language Model Prompts

📄 arXiv: 2503.05070v1 📥 PDF

作者: Reshabh K Sharma, Jonathan De Halleux, Shraddha Barke, Benjamin Zorn

分类: cs.SE, cs.AI

发布日期: 2025-03-07

🔗 代码/项目: GITHUB


💡 一句话要点

PromptPex:一种用于语言模型提示的自动测试生成工具

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 大型语言模型 提示工程 自动测试生成 单元测试 软件质量

📋 核心要点

  1. 大型语言模型提示工程面临鲁棒性挑战,现有方法难以有效测试和调试提示。
  2. PromptPex通过提取提示的输入输出规范,自动生成多样化且有针对性的单元测试。
  3. 实验表明,PromptPex生成的测试能有效发现不同模型中的无效输出,优于基线方法。

📝 摘要(中文)

大型语言模型(LLMs)被广泛应用于各种应用中,并且针对这些模型的提示被作为类似代码的工件集成到软件应用中。这些提示的行为很像传统的软件,它们接收输入、生成输出并执行某些特定功能。然而,提示在许多方面与传统代码不同,需要新的方法来确保其鲁棒性。例如,与传统软件不同,提示的输出取决于解释它的AI模型。此外,虽然自然语言提示很容易修改,但更新的影响更难预测。因此,需要新的方法来测试、调试和修改提示。为了解决这些问题,我们开发了PromptPex,这是一种基于LLM的工具,可以自动生成和评估给定提示的单元测试。PromptPex从提示中提取输入和输出规范,并使用它们来生成多样化、有针对性和有效的单元测试。这些测试有助于识别提示更改时的回归,并且还可以作为一种工具来理解不同模型如何解释提示。我们使用PromptPex为八个基准提示生成测试,并通过观察这些测试是否能导致四个不同的模型产生无效输出来评估生成测试的质量。PromptPex始终如一地创建测试,与精心构建的基于LLM的基线测试生成器相比,这些测试会导致更多的无效模型输出。此外,通过从输入提示中提取具体的规范,PromptPex使提示编写者能够清楚地理解和测试提示的特定方面。PromptPex的源代码可在https://github.com/microsoft/promptpex获取。

🔬 方法详解

问题定义:论文旨在解决大型语言模型(LLM)提示的测试问题。现有方法在测试LLM提示时存在不足,例如难以预测提示修改的影响,以及缺乏有效的工具来识别不同模型对提示的不同解释。传统的软件测试方法并不完全适用于LLM提示,因为提示的行为受到底层AI模型的影响,并且自然语言的灵活性使得测试更加复杂。

核心思路:PromptPex的核心思路是从给定的LLM提示中自动提取输入和输出规范,然后利用这些规范生成多样化、有针对性的单元测试。通过这些单元测试,可以评估提示在不同模型下的行为,并检测潜在的回归和错误。这种方法的核心在于将自然语言提示转化为可测试的规范,从而实现自动化测试。

技术框架:PromptPex的整体框架包括以下几个主要模块:1) 提示解析模块:负责解析给定的LLM提示,提取输入和输出的描述信息。2) 测试生成模块:根据提取的输入输出规范,生成多样化的单元测试用例。3) 测试执行模块:将生成的测试用例提交给不同的LLM模型执行,并记录模型的输出结果。4) 结果评估模块:比较模型的输出结果与预期的输出规范,判断测试是否通过,并生成测试报告。

关键创新:PromptPex最重要的技术创新点在于其自动提取提示规范并生成单元测试的能力。与手动编写测试用例相比,PromptPex可以更高效地生成大量多样化的测试用例,从而更全面地覆盖提示的各种可能行为。此外,PromptPex还可以帮助提示编写者更好地理解提示的规范,并发现潜在的问题。

关键设计:PromptPex的关键设计包括:1) 使用LLM进行提示解析,自动提取输入输出规范。2) 设计多样化的测试用例生成策略,包括边界值测试、等价类划分等。3) 采用灵活的测试执行框架,支持不同的LLM模型和测试环境。4) 使用清晰的测试报告格式,方便用户理解测试结果。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

PromptPex在八个基准提示上进行了评估,并与一个精心构建的基于LLM的基线测试生成器进行了比较。实验结果表明,PromptPex生成的测试用例能够导致更多的无效模型输出,表明其在发现提示错误方面具有更高的有效性。具体来说,PromptPex在四个不同的模型上都表现出优于基线方法的性能。

🎯 应用场景

PromptPex可应用于各种使用大型语言模型的软件应用中,例如智能客服、文本生成、代码生成等。通过自动生成和评估单元测试,PromptPex可以提高LLM提示的鲁棒性和可靠性,减少因提示错误导致的潜在风险。此外,PromptPex还可以帮助开发者更好地理解和调试LLM提示,提高开发效率。

📄 摘要(原文)

Large language models (LLMs) are being used in many applications and prompts for these models are integrated into software applications as code-like artifacts. These prompts behave much like traditional software in that they take inputs, generate outputs, and perform some specific function. However, prompts differ from traditional code in many ways and require new approaches to ensure that they are robust. For example, unlike traditional software the output of a prompt depends on the AI model that interprets it. Also, while natural language prompts are easy to modify, the impact of updates is harder to predict. New approaches to testing, debugging, and modifying prompts with respect to the model running them are required. To address some of these issues, we developed PromptPex, an LLM-based tool to automatically generate and evaluate unit tests for a given prompt. PromptPex extracts input and output specifications from a prompt and uses them to generate diverse, targeted, and valid unit tests. These tests are instrumental in identifying regressions when a prompt is changed and also serve as a tool to understand how prompts are interpreted by different models. We use PromptPex to generate tests for eight benchmark prompts and evaluate the quality of the generated tests by seeing if they can cause each of four diverse models to produce invalid output. PromptPex consistently creates tests that result in more invalid model outputs than a carefully constructed baseline LLM-based test generator. Furthermore, by extracting concrete specifications from the input prompt, PromptPex allows prompt writers to clearly understand and test specific aspects of their prompts. The source code of PromptPex is available at https://github.com/microsoft/promptpex.