-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance prompt template formatting: support nested placeholders and unpaired braces handling #1653
base: main
Are you sure you want to change the base?
Conversation
详细描述 新增功能: 支持嵌套结构占位符(如 {data[key]} 和 {obj.attr}),这些占位符将被保留原样,不被 str.format() 解析。 引入未成对花括号的处理逻辑: 未闭合的 { 转换为普通字符 {{。 单独的 } 转换为普通字符 }}。 确保未成对的花括号不会触发解析错误。 修复问题: 修复嵌套结构占位符在模板中被错误解析的问题。 修复未成对花括号导致 str.format() 抛出异常的问题。 具体修改: 新增 _replace_unpaired_braces 方法,使用正则表达式检测未成对的 { 或 },并将其替换为普通字符,确保模板完整性。 修改 SafeFormatter.format 方法,调用 _replace_unpaired_braces 预处理模板,避免未成对花括号干扰格式化流程。 优化嵌套结构的正则匹配逻辑,将匹配到的嵌套占位符(如 {data[key]})转义为普通文本 {{data[key]}},避免解析。 测试覆盖: 测试未成对的 { 和 }: {abc -> {abc(被保留为普通字符)。 abc} -> abc}(被保留为普通字符)。 测试嵌套占位符: {data[key]} -> 被保留原样。 测试合法占位符的正常解析: {name} -> 被替换为传入的数据值。 测试混合场景: {abc}def} -> {abc}def}。 Hello {name}, Welcome {place}! -> Hello Alice, Welcome Wonderland!(假设传入数据)。
@dcstrange please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
以下是针对你提出的所有代码修改的完整 Git 提交说明,包含中英文版本。
提交说明(中文)
标题
增强模板格式化功能,支持嵌套结构占位符与未成对花括号处理
详细描述
新增功能:
{data[key]}
和{obj.attr}
),这些占位符将被保留原样,不被str.format()
解析。{
转换为普通字符{{
。}
转换为普通字符}}
。修复问题:
str.format()
抛出异常的问题。具体修改:
_replace_unpaired_braces
方法,使用正则表达式检测未成对的{
或}
,并将其替换为普通字符,确保模板完整性。SafeFormatter.format
方法,调用_replace_unpaired_braces
预处理模板,避免未成对花括号干扰格式化流程。{data[key]}
)转义为普通文本{{data[key]}}
,避免解析。测试覆盖:
{
和}
:{abc
->{abc
(被保留为普通字符)。abc}
->abc}
(被保留为普通字符)。{data[key]}
-> 被保留原样。{name}
-> 被替换为传入的数据值。{abc}def}
->{abc}def}
。Hello {name}, Welcome {place}!
->Hello Alice, Welcome Wonderland!
(假设传入数据)。提交说明(英文)
Title
Enhance template formatting: support nested placeholders and unpaired braces handling
Description
New Features:
{data[key]}
and{obj.attr}
), which are preserved as plain text and will not be parsed bystr.format()
.{
is converted to a plain character{{
.}
is converted to a plain character}}
.Bug Fixes:
str.format()
exceptions.Changes:
_replace_unpaired_braces
method, which uses regular expressions to detect unpaired{
or}
and replaces them with plain characters, maintaining template integrity.SafeFormatter.format
to call_replace_unpaired_braces
for pre-processing templates, preventing unpaired braces from interfering with formatting.{{data[key]}}
, avoiding unnecessary parsing.Test Coverage:
{abc
->{abc
(retained as plain text).abc}
->abc}
(retained as plain text).{data[key]}
-> preserved as is.{name}
-> replaced with the value from input data.{abc}def}
->{abc}def}
.Hello {name}, Welcome {place}!
->Hello Alice, Welcome Wonderland!
(assuming input data provided).提交示例
标题:
详细描述:
英文:
如果你对提交说明有其他要求或需要进一步优化,请随时告诉我!