namespace Service.AI;
///
/// 提示词模板引擎,处理占位符替换
///
public class PromptTemplateEngine
{
///
/// 替换模板中的占位符
///
/// 模板字符串,支持 {{key}} 格式的占位符
/// 占位符字典,key 为占位符名称(不含 {{ }}),value 为替换值
/// 替换后的字符串
public static string ReplacePlaceholders(string template, Dictionary placeholders)
{
if (string.IsNullOrEmpty(template) || placeholders == null || placeholders.Count == 0)
{
return template;
}
var result = template;
foreach (var placeholder in placeholders)
{
var key = placeholder.Key;
var value = placeholder.Value ?? string.Empty;
result = result.Replace($"{{{{{key}}}}}", value);
}
return result;
}
///
/// 替换模板中的占位符(简化版本)
///
/// 模板字符串
/// 分类名称
/// 分类类型
/// 颜色方案
/// 风格强度(0.0-1.0)
/// 替换后的字符串
public static string ReplaceForIconGeneration(
string template,
string categoryName,
string categoryType,
string colorScheme,
double styleStrength)
{
var strengthDescription = styleStrength switch
{
>= 0.9 => "极度简约(仅保留最核心元素)",
>= 0.7 => "高度简约(去除所有装饰)",
>= 0.5 => "简约(保留必要细节)",
_ => "适中"
};
var placeholders = new Dictionary
{
["category_name"] = categoryName,
["category_type"] = categoryType,
["color_scheme"] = colorScheme,
["style_strength"] = $"{styleStrength:F1} - {strengthDescription}"
};
return ReplacePlaceholders(template, placeholders);
}
}