如何在Promptfoo评估配置中转义双大括号?
问题原因与解决方案
你遇到的问题是Promptfoo的Handlebars模板引擎将{{some_string}}识别为变量并解析为空,核心是转义方式不正确。以下是两种可行的修正方案:
方案1:使用Handlebars原生字面量转义(推荐)
Promptfoo基于Handlebars,要输出原始的{{和}},需要用连续四个大括号包裹内容(每两个大括号会被解析为一个字面量大括号)。
修改后的配置文件:
description: "test_2025-04-30_003" prompts: - "Output this exact string:{{{{some_string}}}}" providers: - "openai:gpt-4o-mini" tests: assert: - type: contains-all value: "{{{{some_string}}}}"
方案2:YAML字符串转义配合Handlebars字面量
在YAML双引号字符串中,用转义的双引号包裹Handlebars内容,确保模板引擎将其识别为字符串字面量而非变量。
修改后的配置文件:
description: "test_2025-04-30_003" prompts: - "Output this exact string:{{ \"{{some_string}}\" }}" providers: - "openai:gpt-4o-mini" tests: assert: - type: contains-all value: "{{ \"{{some_string}}\" }}"
执行promptfoo eval --verbose后,请求内容会正确包含Output this exact string:{{some_string}}。
内容的提问来源于stack exchange,提问作者Tobias Hermann
相关产品推荐
相关产品推荐

