140 lines
5.2 KiB
C#
140 lines
5.2 KiB
C#
|
|
using AuthorBuddy.Web.Data;
|
|||
|
|
using AuthorBuddy.Web.Models;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
|
|||
|
|
namespace AuthorBuddy.Web.Services;
|
|||
|
|
|
|||
|
|
public class SettingsService : ISettingsService
|
|||
|
|
{
|
|||
|
|
private const string KeyOllamaUrl = "ollama_url";
|
|||
|
|
private const string KeyRagModel = "ollama_rag_model";
|
|||
|
|
private const string KeyStyleModel = "ollama_style_model";
|
|||
|
|
private const string KeySpellingModel = "ollama_spelling_model";
|
|||
|
|
private const string KeyTheme = "theme";
|
|||
|
|
private const string KeyLektorPrompt = "ollama_lektor_prompt";
|
|||
|
|
private const string KeyFactCheckTemplate = "ollama_fact_check";
|
|||
|
|
private const string KeySpellingPrompt = "ollama_spelling_prompt";
|
|||
|
|
|
|||
|
|
private readonly IDbContextFactory<AppDbContext> _contextFactory;
|
|||
|
|
private readonly OllamaSettings _ollamaConfig;
|
|||
|
|
|
|||
|
|
public SettingsService(IDbContextFactory<AppDbContext> contextFactory, OllamaSettings ollamaConfig)
|
|||
|
|
{
|
|||
|
|
_contextFactory = contextFactory;
|
|||
|
|
_ollamaConfig = ollamaConfig;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<string?> GetAsync(string key)
|
|||
|
|
{
|
|||
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|||
|
|
var setting = await context.Settings.FindAsync(key);
|
|||
|
|
return setting?.Value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public Task<string?> GetOllamaUrlAsync() => GetAsync(KeyOllamaUrl);
|
|||
|
|
public Task<string?> GetRagModelAsync() => GetAsync(KeyRagModel);
|
|||
|
|
public Task<string?> GetStyleModelAsync() => GetAsync(KeyStyleModel);
|
|||
|
|
|
|||
|
|
public Task<string?> GetLektorSystemPromptAsync() => GetAsync(KeyLektorPrompt);
|
|||
|
|
public Task<string?> GetFactCheckTemplateAsync() => GetAsync(KeyFactCheckTemplate);
|
|||
|
|
public Task<string?> GetSpellingPromptAsync() => GetAsync(KeySpellingPrompt);
|
|||
|
|
|
|||
|
|
public Task<string?> GetSpellingModelAsync() => GetAsync(KeySpellingModel);
|
|||
|
|
public Task<string?> GetThemeAsync() => GetAsync(KeyTheme);
|
|||
|
|
|
|||
|
|
private async Task SetAsync(string key, string value)
|
|||
|
|
{
|
|||
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|||
|
|
var existing = await context.Settings.FindAsync(key);
|
|||
|
|
if (existing != null)
|
|||
|
|
existing.Value = value;
|
|||
|
|
else
|
|||
|
|
context.Settings.Add(new SettingEntity { Key = key, Value = value });
|
|||
|
|
await context.SaveChangesAsync();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SaveOllamaUrlAsync(string url)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeyOllamaUrl, url);
|
|||
|
|
_ollamaConfig.OllamaUrl = url;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SaveRagModelAsync(string model)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeyRagModel, model);
|
|||
|
|
_ollamaConfig.StyleModel = model;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SaveStyleModelAsync(string model)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeyStyleModel, model);
|
|||
|
|
_ollamaConfig.StyleModel = model;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SaveLektorSystemPromptAsync(string prompt)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeyLektorPrompt, prompt);
|
|||
|
|
_ollamaConfig.LektorSystemPrompt = prompt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SaveFactCheckTemplateAsync(string prompt)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeyFactCheckTemplate, prompt);
|
|||
|
|
_ollamaConfig.FactCheckTemplate = prompt;
|
|||
|
|
}
|
|||
|
|
public async Task SaveSpellingPromptAsync(string prompt)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeySpellingPrompt, prompt);
|
|||
|
|
_ollamaConfig.SpellingPrompt = prompt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task SaveSpellingModelAsync(string model)
|
|||
|
|
{
|
|||
|
|
await SetAsync(KeySpellingModel, model);
|
|||
|
|
_ollamaConfig.SpellingModel = model;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public Task SaveThemeAsync(string theme)
|
|||
|
|
=> SetAsync(KeyTheme, theme);
|
|||
|
|
|
|||
|
|
public async Task InitializeAsync()
|
|||
|
|
{
|
|||
|
|
await using var context = await _contextFactory.CreateDbContextAsync();
|
|||
|
|
|
|||
|
|
var entries = new (string Key, string Default)[]
|
|||
|
|
{
|
|||
|
|
(KeyOllamaUrl, _ollamaConfig.OllamaUrl),
|
|||
|
|
(KeyRagModel, _ollamaConfig.StyleModel),
|
|||
|
|
(KeyStyleModel, _ollamaConfig.StyleModel),
|
|||
|
|
(KeySpellingModel, _ollamaConfig.SpellingModel),
|
|||
|
|
(KeyTheme, _ollamaConfig.Theme),
|
|||
|
|
(KeyLektorPrompt, _ollamaConfig.LektorSystemPrompt),
|
|||
|
|
(KeyFactCheckTemplate, _ollamaConfig.FactCheckTemplate),
|
|||
|
|
(KeySpellingPrompt, _ollamaConfig.SpellingPrompt),
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
foreach (var entry in entries)
|
|||
|
|
{
|
|||
|
|
var existing = await context.Settings.FindAsync(entry.Key);
|
|||
|
|
if (existing != null)
|
|||
|
|
{
|
|||
|
|
var val = existing.Value;
|
|||
|
|
if (entry.Key == KeyOllamaUrl) _ollamaConfig.OllamaUrl = val;
|
|||
|
|
else if (entry.Key == KeyRagModel) _ollamaConfig.StyleModel = val;
|
|||
|
|
else if (entry.Key == KeyStyleModel) _ollamaConfig.StyleModel = val;
|
|||
|
|
else if (entry.Key == KeySpellingModel) _ollamaConfig.SpellingModel = val;
|
|||
|
|
else if (entry.Key == KeyTheme) _ollamaConfig.Theme = val;
|
|||
|
|
else if (entry.Key == KeyLektorPrompt) _ollamaConfig.LektorSystemPrompt = val;
|
|||
|
|
else if (entry.Key == KeyFactCheckTemplate) _ollamaConfig.FactCheckTemplate = val;
|
|||
|
|
else if (entry.Key == KeySpellingPrompt) _ollamaConfig.SpellingPrompt = val;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
context.Settings.Add(new SettingEntity { Key = entry.Key, Value = entry.Default });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
await context.SaveChangesAsync();
|
|||
|
|
}
|
|||
|
|
}
|