Try catches, ensuring SuccessStatusCode
This commit is contained in:
1 parent
8b476e6939
commit
4494a2a202
4 files changed
+42
-19
No files matched your search
@@ -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 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 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.");
|
public string Prompt { get; set; } = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Prompts", "llm-prompt.txt")) ?? throw new InvalidOperationException("File \"Prompt\" not found.");
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
using System.Net.Http.Headers;
|
|
||||||
using PaperlessAI.Models.LMStudio;
|
|
||||||
|
|
||||||
namespace PaperlessAI.Services;
|
namespace PaperlessAI.Services;
|
||||||
|
|
||||||
public class ApiService(HttpClient client)
|
public class ApiService(HttpClient client)
|
||||||
|
|||||||
@@ -84,31 +84,54 @@ public class DocumentProcessingService(IServiceScopeFactory scopeFactory, ILogge
|
|||||||
|
|
||||||
private async Task UnloadModel()
|
private async Task UnloadModel()
|
||||||
{
|
{
|
||||||
await using var scope = scopeFactory.CreateAsyncScope();
|
try
|
||||||
var agentService = scope.ServiceProvider.GetRequiredService<LLMAgentService>();
|
{
|
||||||
|
isWorking = true;
|
||||||
|
await using var scope = scopeFactory.CreateAsyncScope();
|
||||||
|
var agentService = scope.ServiceProvider.GetRequiredService<LLMAgentService>();
|
||||||
|
|
||||||
|
logger.LogInformation($"Unloading Model");
|
||||||
|
await agentService.UnloadModel();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.LogError(e.ToString());
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isWorking = false;
|
||||||
|
}
|
||||||
|
|
||||||
logger.LogInformation($"Unloading Model");
|
|
||||||
isWorking = true;
|
|
||||||
await agentService.UnloadModel();
|
|
||||||
isWorking = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<LmStudioLlmResponse> ProcessDocument(PaperlessDocument docToProcess)
|
private async Task<LmStudioLlmResponse> ProcessDocument(PaperlessDocument docToProcess)
|
||||||
{
|
{
|
||||||
await using var scope = scopeFactory.CreateAsyncScope();
|
var llmResponse = new LmStudioLlmResponse();
|
||||||
var agentService = scope.ServiceProvider.GetRequiredService<LLMAgentService>();
|
try
|
||||||
var repository = scope.ServiceProvider.GetRequiredService<PaperlessDocumentRepository>();
|
{
|
||||||
|
isWorking = true;
|
||||||
|
|
||||||
isWorking = true;
|
await using var scope = scopeFactory.CreateAsyncScope();
|
||||||
|
var agentService = scope.ServiceProvider.GetRequiredService<LLMAgentService>();
|
||||||
|
var repository = scope.ServiceProvider.GetRequiredService<PaperlessDocumentRepository>();
|
||||||
|
|
||||||
logger.LogInformation($"Processing: {docToProcess}");
|
logger.LogInformation($"Processing: {docToProcess}");
|
||||||
|
|
||||||
docToProcess.DocumentState = EDocumentState.Started;
|
docToProcess.DocumentState = EDocumentState.Started;
|
||||||
await repository.Update(docToProcess);
|
await repository.Update(docToProcess);
|
||||||
|
|
||||||
var llmResponse = await agentService.ProcessDocumentWithAI(docToProcess);
|
llmResponse = await agentService.ProcessDocumentWithAI(docToProcess);
|
||||||
|
}
|
||||||
isWorking = false;
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.LogError(e.ToString());
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isWorking = false;
|
||||||
|
}
|
||||||
return llmResponse ?? new LmStudioLlmResponse();
|
return llmResponse ?? new LmStudioLlmResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ public class LLMAgentService(ApiService apiService, Configuration config, ILogge
|
|||||||
Reasoning = config.Reasoning
|
Reasoning = config.Reasoning
|
||||||
};
|
};
|
||||||
var response = await apiService.Post("chat", content);
|
var response = await apiService.Post("chat", content);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
var contentMessage = await response.Content.ReadAsStringAsync();
|
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);
|
var response = await apiService.Post("models/unload", content);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
var message = await response.Content.ReadAsStringAsync();
|
var message = await response.Content.ReadAsStringAsync();
|
||||||
logger.LogInformation(message);
|
logger.LogInformation(message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user