many fixes
This commit is contained in:
58
ChatBot/Services/FileSystemPromptProvider.cs
Normal file
58
ChatBot/Services/FileSystemPromptProvider.cs
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user