AuthorBuddy/Services/OllamaSetupService.cs

124 lines
4.7 KiB
C#
Raw Normal View History

2026-05-31 19:31:27 +02:00
using System.Runtime.InteropServices;
using System.Text.Json;
using AuthorBuddy.Web.Models;
namespace AuthorBuddy.Web.Services;
public class OllamaSetupService : IOllamaSetupService
{
private readonly IOllamaService _ollamaService;
private readonly OperationState _opState;
private readonly OllamaSettings _currentSettings;
public OllamaSetupService(IOllamaService ollamaService, OperationState opState, OllamaSettings currentSettings)
{
_ollamaService = ollamaService;
_opState = opState;
_currentSettings = currentSettings;
}
public async Task<SetupRecommendation> AnalyzeSettingsAsync()
{
var recommendation = new SetupRecommendation();
var recommended = new OllamaSettings();
_opState.SetStatus("Überprüfe Ollama-Einstellungen...", true);
_opState.SetThought("");
// OS Detection
string os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows" :
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "Linux" : "Unbekannt";
_opState.AppendThought($"System erkannt: {os}");
recommendation.Findings.Add($"Betriebssystem: {os}");
// Connection Check
_opState.AppendThought("Prüfe Verbindung zu Ollama...");
var connection = await _ollamaService.TestConnectionAsync();
if (connection.Success)
{
_opState.AppendThought("Verbindung erfolgreich.");
recommendation.Findings.Add("Ollama ist erreichbar.");
recommended.OllamaUrl = _currentSettings.OllamaUrl;
}
else
{
_opState.AppendThought("Verbindung fehlgeschlagen!");
recommendation.Findings.Add("Ollama ist NICHT erreichbar. Bitte prüfen Sie, ob der Server läuft.");
recommended.OllamaUrl = "http://localhost:11434";
recommendation.IsCorrect = false;
}
// Model Recommendations
_opState.AppendThought("Analysiere Modell-Empfehlungen...");
// Recommendation logic
recommended.EmbedModel = "nomic-embed-text";
recommended.StyleModel = "gemma4:e4b";
recommended.SpellingModel = "phi3:mini";
if (_currentSettings.EmbedModel != recommended.EmbedModel)
{
recommendation.Findings.Add($"Empfehlung: Nutze {recommended.EmbedModel} für Embeddings (aktuell: {_currentSettings.EmbedModel}).");
recommendation.IsCorrect = false;
}
if (_currentSettings.StyleModel != recommended.StyleModel)
{
recommendation.Findings.Add($"Empfehlung: Nutze {recommended.StyleModel} für Stil-Optimierung (aktuell: {_currentSettings.StyleModel}).");
recommendation.IsCorrect = false;
}
if (_currentSettings.SpellingModel != recommended.SpellingModel)
{
recommendation.Findings.Add($"Empfehlung: Nutze {recommended.SpellingModel} für Rechtschreibung (aktuell: {_currentSettings.SpellingModel}).");
recommendation.IsCorrect = false;
}
if (recommendation.IsCorrect == false)
{
recommendation.Message = "Es wurden Optimierungspotenziale in Ihren Ollama-Einstellungen gefunden.";
}
else
{
recommendation.IsCorrect = true;
recommendation.Message = "Alle Ollama-Einstellungen sind optimal konfiguriert.";
}
recommended.LektorSystemPrompt = _currentSettings.LektorSystemPrompt;
recommended.FactCheckTemplate = _currentSettings.FactCheckTemplate;
recommended.SpellingPrompt = _currentSettings.SpellingPrompt;
recommended.Language = _currentSettings.Language;
_opState.SetStatus("Überprüfung abgeschlossen", false);
recommendation.RecommendedSettings = recommended;
return recommendation;
}
public async Task<bool> CreateConfigFileAsync(OllamaSettings settings)
{
try
{
_opState.SetStatus("Erstelle Konfigurationsdatei...", true);
_opState.SetThought("Schreibe ollama_config.json in das App-Verzeichnis...");
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(settings, options);
// Writing to app root
await File.WriteAllTextAsync("ollama_config.json", json);
_opState.SetStatus("Konfigurationsdatei erstellt", false);
_opState.SetThought("Datei 'ollama_config.json' wurde erfolgreich gespeichert.");
return true;
}
catch (Exception ex)
{
_opState.SetStatus("Fehler beim Erstellen der Datei", false);
_opState.SetThought($"Error: {ex.Message}");
return false;
}
}
}