114 lines
3.2 KiB
C#
114 lines
3.2 KiB
C#
namespace Service.AI;
|
|
|
|
/// <summary>
|
|
/// 分类图标生成提示词提供器实现
|
|
/// </summary>
|
|
public class ClassificationIconPromptProvider : IClassificationIconPromptProvider
|
|
{
|
|
private readonly ILogger<ClassificationIconPromptProvider> _logger;
|
|
private readonly IconPromptSettings _config;
|
|
private readonly Random _random = new();
|
|
|
|
public ClassificationIconPromptProvider(
|
|
ILogger<ClassificationIconPromptProvider> logger,
|
|
IOptions<IconPromptSettings> config)
|
|
{
|
|
_logger = logger;
|
|
_config = config.Value;
|
|
}
|
|
|
|
public string GetPrompt(string categoryName, TransactionType categoryType)
|
|
{
|
|
var typeText = GetCategoryTypeText(categoryType);
|
|
var useNewPrompt = ShouldUseNewPrompt();
|
|
|
|
var template = useNewPrompt
|
|
? _config.DefaultPromptTemplate
|
|
: _config.OldDefaultPromptTemplate;
|
|
|
|
string prompt;
|
|
if (useNewPrompt)
|
|
{
|
|
prompt = PromptTemplateEngine.ReplaceForIconGeneration(
|
|
template,
|
|
categoryName,
|
|
typeText,
|
|
_config.ColorScheme,
|
|
_config.StyleStrength);
|
|
}
|
|
else
|
|
{
|
|
prompt = PromptTemplateEngine.ReplaceForIconGeneration(
|
|
template,
|
|
categoryName,
|
|
typeText,
|
|
_config.ColorScheme,
|
|
0);
|
|
}
|
|
|
|
_logger.LogDebug("使用 {PromptType} 提示词生成图标,分类:{CategoryName}",
|
|
useNewPrompt ? "新版" : "旧版",
|
|
categoryName);
|
|
|
|
return prompt;
|
|
}
|
|
|
|
public string GetSingleIconPrompt(string categoryName, TransactionType categoryType)
|
|
{
|
|
var typeText = GetCategoryTypeText(categoryType);
|
|
var useNewPrompt = ShouldUseNewPrompt();
|
|
|
|
var template = useNewPrompt
|
|
? _config.SingleIconPromptTemplate
|
|
: _config.OldSingleIconPromptTemplate;
|
|
|
|
string prompt;
|
|
if (useNewPrompt)
|
|
{
|
|
prompt = PromptTemplateEngine.ReplaceForIconGeneration(
|
|
template,
|
|
categoryName,
|
|
typeText,
|
|
_config.ColorScheme,
|
|
_config.StyleStrength);
|
|
}
|
|
else
|
|
{
|
|
prompt = PromptTemplateEngine.ReplaceForIconGeneration(
|
|
template,
|
|
categoryName,
|
|
typeText,
|
|
_config.ColorScheme,
|
|
0);
|
|
}
|
|
|
|
_logger.LogDebug("使用 {PromptType} 提示词生成单个图标,分类:{CategoryName}",
|
|
useNewPrompt ? "新版" : "旧版",
|
|
categoryName);
|
|
|
|
return prompt;
|
|
}
|
|
|
|
private bool ShouldUseNewPrompt()
|
|
{
|
|
if (!_config.EnableNewPrompt)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var randomValue = _random.NextDouble();
|
|
return randomValue < _config.GrayScaleRatio;
|
|
}
|
|
|
|
private static string GetCategoryTypeText(TransactionType categoryType)
|
|
{
|
|
return categoryType switch
|
|
{
|
|
TransactionType.Expense => "支出",
|
|
TransactionType.Income => "收入",
|
|
TransactionType.None => "不计入收支",
|
|
_ => "未知"
|
|
};
|
|
}
|
|
}
|