Improved logging

This commit is contained in:
Thorsten Sommer 2025-09-04 11:40:54 +02:00
parent ce243913ed
commit c4a3d893f6
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 23 additions and 22 deletions

View File

@ -11,6 +11,8 @@ namespace AIStudio.Chat;
/// </summary> /// </summary>
public sealed class ContentText : IContent public sealed class ContentText : IContent
{ {
private static readonly ILogger<ContentText> LOGGER = Program.LOGGER_FACTORY.CreateLogger<ContentText>();
/// <summary> /// <summary>
/// The minimum time between two streaming events, when the user /// The minimum time between two streaming events, when the user
/// enables the energy saving mode. /// enables the energy saving mode.
@ -46,8 +48,7 @@ public sealed class ContentText : IContent
if(!chatThread.IsLLMProviderAllowed(provider)) if(!chatThread.IsLLMProviderAllowed(provider))
{ {
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<ContentText>>()!; LOGGER.LogError("The provider is not allowed for this chat thread due to data security reasons. Skipping the AI process.");
logger.LogError("The provider is not allowed for this chat thread due to data security reasons. Skipping the AI process.");
return chatThread; return chatThread;
} }
@ -61,8 +62,7 @@ public sealed class ContentText : IContent
} }
catch (Exception e) catch (Exception e)
{ {
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<ContentText>>()!; LOGGER.LogError(e, "Skipping the RAG process due to an error.");
logger.LogError(e, "Skipping the RAG process due to an error.");
} }
} }

View File

@ -10,6 +10,8 @@ namespace AIStudio.Tools.RAG.AugmentationProcesses;
public sealed class AugmentationOne : IAugmentationProcess public sealed class AugmentationOne : IAugmentationProcess
{ {
private static readonly ILogger<AugmentationOne> LOGGER = Program.LOGGER_FACTORY.CreateLogger<AugmentationOne>();
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AugmentationOne).Namespace, nameof(AugmentationOne)); private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AugmentationOne).Namespace, nameof(AugmentationOne));
#region Implementation of IAugmentationProcess #region Implementation of IAugmentationProcess
@ -26,12 +28,11 @@ public sealed class AugmentationOne : IAugmentationProcess
/// <inheritdoc /> /// <inheritdoc />
public async Task<ChatThread> ProcessAsync(IProvider provider, IContent lastPrompt, ChatThread chatThread, IReadOnlyList<IRetrievalContext> retrievalContexts, CancellationToken token = default) public async Task<ChatThread> ProcessAsync(IProvider provider, IContent lastPrompt, ChatThread chatThread, IReadOnlyList<IRetrievalContext> retrievalContexts, CancellationToken token = default)
{ {
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<AugmentationOne>>()!;
var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!; var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!;
if(retrievalContexts.Count == 0) if(retrievalContexts.Count == 0)
{ {
logger.LogWarning("No retrieval contexts were issued. Skipping the augmentation process."); LOGGER.LogWarning("No retrieval contexts were issued. Skipping the augmentation process.");
return chatThread; return chatThread;
} }
@ -57,7 +58,7 @@ public sealed class AugmentationOne : IAugmentationProcess
retrievalContexts = validationResults.Where(x => x.RetrievalContext is not null && x.Confidence >= threshold).Select(x => x.RetrievalContext!).ToList(); retrievalContexts = validationResults.Where(x => x.RetrievalContext is not null && x.Confidence >= threshold).Select(x => x.RetrievalContext!).ToList();
} }
logger.LogInformation($"Starting the augmentation process over {numTotalRetrievalContexts:###,###,###,###} retrieval contexts."); LOGGER.LogInformation($"Starting the augmentation process over {numTotalRetrievalContexts:###,###,###,###} retrieval contexts.");
// //
// We build a huge prompt from all retrieval contexts: // We build a huge prompt from all retrieval contexts:

View File

@ -9,6 +9,8 @@ namespace AIStudio.Tools.RAG.DataSourceSelectionProcesses;
public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
{ {
private static readonly ILogger<AgenticSrcSelWithDynHeur> LOGGER = Program.LOGGER_FACTORY.CreateLogger<AgenticSrcSelWithDynHeur>();
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AgenticSrcSelWithDynHeur).Namespace, nameof(AgenticSrcSelWithDynHeur)); private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AgenticSrcSelWithDynHeur).Namespace, nameof(AgenticSrcSelWithDynHeur));
#region Implementation of IDataSourceSelectionProcess #region Implementation of IDataSourceSelectionProcess
@ -29,9 +31,6 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
IReadOnlyList<IDataSource> selectedDataSources = []; IReadOnlyList<IDataSource> selectedDataSources = [];
IReadOnlyList<DataSourceAgentSelected> finalAISelection = []; IReadOnlyList<DataSourceAgentSelected> finalAISelection = [];
// Get the logger:
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<AgenticSrcSelWithDynHeur>>()!;
// Get the settings manager: // Get the settings manager:
var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!; var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!;
@ -46,7 +45,7 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
// Check if the AI selected any data sources: // Check if the AI selected any data sources:
if (aiSelectedDataSources.Count is 0) if (aiSelectedDataSources.Count is 0)
{ {
logger.LogWarning("The AI did not select any data sources. The RAG process is skipped."); LOGGER.LogWarning("The AI did not select any data sources. The RAG process is skipped.");
proceedWithRAG = false; proceedWithRAG = false;
return new(proceedWithRAG, selectedDataSources); return new(proceedWithRAG, selectedDataSources);
@ -54,7 +53,7 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
// Log the selected data sources: // Log the selected data sources:
var selectedDataSourceInfo = aiSelectedDataSources.Select(ds => $"[Id={ds.Id}, reason={ds.Reason}, confidence={ds.Confidence}]").Aggregate((a, b) => $"'{a}', '{b}'"); var selectedDataSourceInfo = aiSelectedDataSources.Select(ds => $"[Id={ds.Id}, reason={ds.Reason}, confidence={ds.Confidence}]").Aggregate((a, b) => $"'{a}', '{b}'");
logger.LogInformation($"The AI selected the data sources automatically. {aiSelectedDataSources.Count} data source(s) are selected: {selectedDataSourceInfo}."); LOGGER.LogInformation($"The AI selected the data sources automatically. {aiSelectedDataSources.Count} data source(s) are selected: {selectedDataSourceInfo}.");
// //
// Check how many data sources were hallucinated by the AI: // Check how many data sources were hallucinated by the AI:
@ -69,7 +68,7 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
var numHallucinatedSources = totalAISelectedDataSources - aiSelectedDataSources.Count; var numHallucinatedSources = totalAISelectedDataSources - aiSelectedDataSources.Count;
if (numHallucinatedSources > 0) if (numHallucinatedSources > 0)
logger.LogWarning($"The AI hallucinated {numHallucinatedSources} data source(s). We ignore them."); LOGGER.LogWarning($"The AI hallucinated {numHallucinatedSources} data source(s). We ignore them.");
if (aiSelectedDataSources.Count > 3) if (aiSelectedDataSources.Count > 3)
{ {
@ -85,7 +84,7 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
if (aiSelectedDataSources.Any(x => x.Id == dataSource.DataSource.Id)) if (aiSelectedDataSources.Any(x => x.Id == dataSource.DataSource.Id))
dataSource.Selected = true; dataSource.Selected = true;
logger.LogInformation($"The AI selected {aiSelectedDataSources.Count} data source(s) with a confidence of at least {threshold}."); LOGGER.LogInformation($"The AI selected {aiSelectedDataSources.Count} data source(s) with a confidence of at least {threshold}.");
// Transform the final data sources to the actual data sources: // Transform the final data sources to the actual data sources:
selectedDataSources = aiSelectedDataSources.Select(x => settings.ConfigurationData.DataSources.FirstOrDefault(ds => ds.Id == x.Id)).Where(ds => ds is not null).ToList()!; selectedDataSources = aiSelectedDataSources.Select(x => settings.ConfigurationData.DataSources.FirstOrDefault(ds => ds.Id == x.Id)).Where(ds => ds is not null).ToList()!;

View File

@ -11,6 +11,8 @@ namespace AIStudio.Tools.RAG.RAGProcesses;
public sealed class AISrcSelWithRetCtxVal : IRagProcess public sealed class AISrcSelWithRetCtxVal : IRagProcess
{ {
private static readonly ILogger<AISrcSelWithRetCtxVal> LOGGER = Program.LOGGER_FACTORY.CreateLogger<AISrcSelWithRetCtxVal>();
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AISrcSelWithRetCtxVal).Namespace, nameof(AISrcSelWithRetCtxVal)); private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AISrcSelWithRetCtxVal).Namespace, nameof(AISrcSelWithRetCtxVal));
#region Implementation of IRagProcess #region Implementation of IRagProcess
@ -27,7 +29,6 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
/// <inheritdoc /> /// <inheritdoc />
public async Task<ChatThread> ProcessAsync(IProvider provider, IContent lastPrompt, ChatThread chatThread, CancellationToken token = default) public async Task<ChatThread> ProcessAsync(IProvider provider, IContent lastPrompt, ChatThread chatThread, CancellationToken token = default)
{ {
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<AISrcSelWithRetCtxVal>>()!;
var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!; var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!;
var dataSourceService = Program.SERVICE_PROVIDER.GetService<DataSourceService>()!; var dataSourceService = Program.SERVICE_PROVIDER.GetService<DataSourceService>()!;
@ -36,7 +37,7 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
// //
if (chatThread.DataSourceOptions.IsEnabled()) if (chatThread.DataSourceOptions.IsEnabled())
{ {
logger.LogInformation("Data sources are enabled for this chat."); LOGGER.LogInformation("Data sources are enabled for this chat.");
// Across the different code-branches, we keep track of whether it // Across the different code-branches, we keep track of whether it
// makes sense to proceed with the RAG process: // makes sense to proceed with the RAG process:
@ -49,13 +50,13 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
// //
if(chatThread.Blocks.Count == 0) if(chatThread.Blocks.Count == 0)
{ {
logger.LogError("The chat thread is empty. Skipping the RAG process."); LOGGER.LogError("The chat thread is empty. Skipping the RAG process.");
return chatThread; return chatThread;
} }
if (chatThread.Blocks.Last().Role != ChatRole.AI) if (chatThread.Blocks.Last().Role != ChatRole.AI)
{ {
logger.LogError("The last block in the chat thread is not the AI block. There is something wrong with the chat thread. Skipping the RAG process."); LOGGER.LogError("The last block in the chat thread is not the AI block. There is something wrong with the chat thread. Skipping the RAG process.");
return chatThread; return chatThread;
} }
@ -92,12 +93,12 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
// No, the user made the choice manually: // No, the user made the choice manually:
// //
var selectedDataSourceInfo = selectedDataSources.Select(ds => ds.Name).Aggregate((a, b) => $"'{a}', '{b}'"); var selectedDataSourceInfo = selectedDataSources.Select(ds => ds.Name).Aggregate((a, b) => $"'{a}', '{b}'");
logger.LogInformation($"The user selected the data sources manually. {selectedDataSources.Count} data source(s) are selected: {selectedDataSourceInfo}."); LOGGER.LogInformation($"The user selected the data sources manually. {selectedDataSources.Count} data source(s) are selected: {selectedDataSourceInfo}.");
} }
if(selectedDataSources.Count == 0) if(selectedDataSources.Count == 0)
{ {
logger.LogWarning("No data sources are selected. The RAG process is skipped."); LOGGER.LogWarning("No data sources are selected. The RAG process is skipped.");
proceedWithRAG = false; proceedWithRAG = false;
} }
else else
@ -148,7 +149,7 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
}; };
if (previousDataSecurity != chatThread.DataSecurity) if (previousDataSecurity != chatThread.DataSecurity)
logger.LogInformation($"The data security of the chat thread was updated from '{previousDataSecurity}' to '{chatThread.DataSecurity}'."); LOGGER.LogInformation($"The data security of the chat thread was updated from '{previousDataSecurity}' to '{chatThread.DataSecurity}'.");
} }
// //
@ -175,7 +176,7 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
} }
catch (Exception e) catch (Exception e)
{ {
logger.LogError(e, "An error occurred during the retrieval process."); LOGGER.LogError(e, "An error occurred during the retrieval process.");
} }
} }
} }