many fixes

This commit is contained in:
Leonid Pershin
2025-10-16 07:11:30 +03:00
parent 0007a0ffc4
commit 7a3a0172cf
56 changed files with 2202 additions and 1258 deletions

View File

@@ -0,0 +1,58 @@
using System.Text;
using ChatBot.Models.Configuration;
using ChatBot.Services.Interfaces;
using Microsoft.Extensions.Options;
namespace ChatBot.Services
{
/// <summary>
/// System prompt provider that loads prompt from file
/// </summary>
public class FileSystemPromptProvider : ISystemPromptProvider
{
private readonly string _filePath;
private readonly ILogger<FileSystemPromptProvider> _logger;
private readonly Lazy<string> _cachedPrompt;
public FileSystemPromptProvider(
IOptions<OllamaSettings> settings,
ILogger<FileSystemPromptProvider> logger
)
{
_filePath = settings.Value.SystemPromptFilePath;
_logger = logger;
_cachedPrompt = new Lazy<string>(LoadPrompt);
}
public string GetSystemPrompt() => _cachedPrompt.Value;
private string LoadPrompt()
{
if (!File.Exists(_filePath))
{
var error = $"System prompt file not found: {_filePath}";
_logger.LogError(error);
throw new FileNotFoundException(error);
}
try
{
var prompt = File.ReadAllText(_filePath, Encoding.UTF8);
_logger.LogInformation(
"System prompt loaded from {FilePath} ({Length} characters)",
_filePath,
prompt.Length
);
return prompt;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to read system prompt file: {FilePath}", _filePath);
throw new InvalidOperationException(
$"Failed to read system prompt file '{_filePath}': {ex.Message}",
ex
);
}
}
}
}