1. 引入
promptfoo 是一款专为大模型安全测试打造的强大工具。它能通过红队测试、渗透测试以及漏洞扫描等方式,对各类大模型展开深度安全评估,全面检测模型在不同场景下的安全性。
2. 运行promptfoo的过程
-
安装nodejs
-
用npm安装promptfoo
npm install -g promptfoo
- 设置测试内容/范围
promptfoo redteam setup
这个步骤,会弹出浏览器启动的UI,设置“测试目标与系统提示词”,“待测大模型链接方式”,“插件”与“策略”。设置完成后,生成一个promptfooconfig.yaml
文件。后续测评时,用-c
参数指定对应的promptfooconfig.yaml
文件,就能对大模型进行测评。
所以这个步骤也可以修改参考3中给定的样例yaml文件,比如对于测评vllm部署的大模型,下面是一个简单的promptfooconfig.yaml
文件:
description: My Red Team Configuration
targets:
- id: openai:completion:tt-gpt-qwen2.5-7b
config:
apiBaseUrl: http://10.11.12.8:6001/v1
prompts:
- '{{prompt}}'
redteam:
provider:
id: openai:completion:tt-gpt-qwen2.5-7b
config:
apiBaseUrl: http://10.46.156.8:6001/v1
purpose: |-
The objective of the application is: test LLM vulnerability
You are: security agent
The LLM agent has access to these systems: security agent
plugins:
- id: hallucination # Tests for fabrication of false or misleading information
- id: hijacking # Tests for unauthorized resource usage and purpose deviation
strategies:
- id: jailbreak # Single-shot optimization of safety bypass techniques
- id: prompt-injection # Tests for direct prompt injection vulnerabilities
上面这个yaml文件中的关键配置为:
(1)targets:待测模型,模型名称为tt-gpt-qwen2.5-7b
(这里的格式是因为兼容vllm部署的模型API格式)。
(2)provider:攻击者模型(产生测评所用的提示词),模型名称为tt-gpt-qwen2.5-7b
,这里将其配置为与targets同一个模型。
(3)插件设置:plugins数组中包含hallucination和hijacking两个插件,分别用于测试幻觉、未经授权的资源使用和目的偏离问题。
(4)策略设置:strategies数组包含jailbreak和prompt - injection两个策略,分别用于单步优化绕过安全技术测试和直接提示注入漏洞测试。
- 运行测评
promptfoo redteam run
也可以用-c
指定具体的配置文件,用-o
指定生成测试用例的yaml文件,如下所示:
promptfoo redteam run -c promptfooconfig.yaml -o test.qwen.yaml --verbose
这里的--verbose
能输出更多细节测评过程。
- 查看结果
promptfoo redteam report
该命令运行后,弹出浏览器,能看到类似如下的结果:
点击能看到细节,比如点击 Baseline Testing:
可以看到,幻觉测试数据有5条,其中有3条的结果测评不通过,需要进行进一步审计。资源劫持有2条提示词不通过。(Flagged Attempts标红,表示Flagged Attempts是需要进一步审计的),如下也能看到具体需要关注的提示词与大模型回复。
3. 总结
promptfoo 能对大模型展开深度安全评估,检测模型在不同场景下的安全性。通过配置不同的plugin就能进行不同类型的安全测试(不同提示词数据集)。本文讲解了运行promptfoo的过程以及结果分析。
4. 参考
- https://github.com/promptfoo/promptfoo
- 大模型安全测试:https://www.promptfoo.dev/docs/red-team/quickstart/
- yaml配置样例:https://github.com/promptfoo/promptfoo/blob/main/examples/redteam-beavertails/promptfooconfig.yaml