Files
EmailBill/Service/AI/PromptTemplateEngine.cs
SunCheng a88556c784 fix
2026-02-15 10:10:28 +08:00

67 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Service.AI;
/// <summary>
/// 提示词模板引擎,处理占位符替换
/// </summary>
public class PromptTemplateEngine
{
/// <summary>
/// 替换模板中的占位符
/// </summary>
/// <param name="template">模板字符串,支持 {{key}} 格式的占位符</param>
/// <param name="placeholders">占位符字典key 为占位符名称(不含 {{ }}value 为替换值</param>
/// <returns>替换后的字符串</returns>
public static string ReplacePlaceholders(string template, Dictionary<string, string> 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;
}
/// <summary>
/// 替换模板中的占位符(简化版本)
/// </summary>
/// <param name="template">模板字符串</param>
/// <param name="categoryName">分类名称</param>
/// <param name="categoryType">分类类型</param>
/// <param name="colorScheme">颜色方案</param>
/// <param name="styleStrength">风格强度0.0-1.0</param>
/// <returns>替换后的字符串</returns>
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<string, string>
{
["category_name"] = categoryName,
["category_type"] = categoryType,
["color_scheme"] = colorScheme,
["style_strength"] = $"{styleStrength:F1} - {strengthDescription}"
};
return ReplacePlaceholders(template, placeholders);
}
}