From 4494a2a2021ec85854ff7cb1dfa3f9164c0c6d45 Mon Sep 17 00:00:00 2001 From: Nickk888 Date: Mon, 12 Oct 2026 17:02:40 +0200 Subject: [PATCH] Try catches, ensuring SuccessStatusCode --- Models/Configuration.cs | 1 + Services/ApiService.cs | 3 -- Services/DocumentProcessingService.cs | 57 +++++++++++++++++++-------- Services/LLMAgentService.cs | 2 + 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Models/Configuration.cs b/Models/Configuration.cs index fcd7b8a..9cc6bef 100644 --- a/Models/Configuration.cs +++ b/Models/Configuration.cs @@ -16,6 +16,7 @@ public class Configuration(IConfiguration config) public int CheckDocumentsToProcessJobPeriodSeconds { get; set; } = int.Parse(config["Settings:CheckDocumentsToProcessJobPeriodSeconds"] ?? throw new InvalidOperationException("Configuration not found \"Settings:CheckDocumentsToProcessJobPeriodSeconds\"")); + public string ApiMasterToken { get; set; } = config["TokenGenerator:MasterTokenHash"] ?? throw new InvalidOperationException("Configuration not found \"TokenGenerator:MasterTokenHash\""); public string SystemPrompt { get; set; } = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Prompts", "llm-system-prompt.txt")) ?? throw new InvalidOperationException("File \"SystemPrompt\" not found."); public string Prompt { get; set; } = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Prompts", "llm-prompt.txt")) ?? throw new InvalidOperationException("File \"Prompt\" not found."); diff --git a/Services/ApiService.cs b/Services/ApiService.cs index c9797da..eb0257b 100644 --- a/Services/ApiService.cs +++ b/Services/ApiService.cs @@ -1,6 +1,3 @@ -using System.Net.Http.Headers; -using PaperlessAI.Models.LMStudio; - namespace PaperlessAI.Services; public class ApiService(HttpClient client) diff --git a/Services/DocumentProcessingService.cs b/Services/DocumentProcessingService.cs index be8a9aa..c08b3cd 100644 --- a/Services/DocumentProcessingService.cs +++ b/Services/DocumentProcessingService.cs @@ -84,31 +84,54 @@ public class DocumentProcessingService(IServiceScopeFactory scopeFactory, ILogge private async Task UnloadModel() { - await using var scope = scopeFactory.CreateAsyncScope(); - var agentService = scope.ServiceProvider.GetRequiredService(); + try + { + isWorking = true; + await using var scope = scopeFactory.CreateAsyncScope(); + var agentService = scope.ServiceProvider.GetRequiredService(); - logger.LogInformation($"Unloading Model"); - isWorking = true; - await agentService.UnloadModel(); - isWorking = false; + logger.LogInformation($"Unloading Model"); + await agentService.UnloadModel(); + } + catch (Exception e) + { + logger.LogError(e.ToString()); + throw; + } + finally + { + isWorking = false; + } + } private async Task ProcessDocument(PaperlessDocument docToProcess) { - await using var scope = scopeFactory.CreateAsyncScope(); - var agentService = scope.ServiceProvider.GetRequiredService(); - var repository = scope.ServiceProvider.GetRequiredService(); + var llmResponse = new LmStudioLlmResponse(); + try + { + isWorking = true; - isWorking = true; + await using var scope = scopeFactory.CreateAsyncScope(); + var agentService = scope.ServiceProvider.GetRequiredService(); + var repository = scope.ServiceProvider.GetRequiredService(); - logger.LogInformation($"Processing: {docToProcess}"); + logger.LogInformation($"Processing: {docToProcess}"); - docToProcess.DocumentState = EDocumentState.Started; - await repository.Update(docToProcess); - - var llmResponse = await agentService.ProcessDocumentWithAI(docToProcess); - - isWorking = false; + docToProcess.DocumentState = EDocumentState.Started; + await repository.Update(docToProcess); + + llmResponse = await agentService.ProcessDocumentWithAI(docToProcess); + } + catch (Exception e) + { + logger.LogError(e.ToString()); + throw; + } + finally + { + isWorking = false; + } return llmResponse ?? new LmStudioLlmResponse(); } } diff --git a/Services/LLMAgentService.cs b/Services/LLMAgentService.cs index 4e66a72..74414ee 100644 --- a/Services/LLMAgentService.cs +++ b/Services/LLMAgentService.cs @@ -24,6 +24,7 @@ public class LLMAgentService(ApiService apiService, Configuration config, ILogge Reasoning = config.Reasoning }; var response = await apiService.Post("chat", content); + response.EnsureSuccessStatusCode(); // Response var contentMessage = await response.Content.ReadAsStringAsync(); @@ -55,6 +56,7 @@ public class LLMAgentService(ApiService apiService, Configuration config, ILogge }; var response = await apiService.Post("models/unload", content); + response.EnsureSuccessStatusCode(); var message = await response.Content.ReadAsStringAsync(); logger.LogInformation(message); }