Moved the background agents to AI operations (#1215)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-21 19:47:10 +01:00
1 parent 8a4a26503b
commit c50a93cd17
139 files changed
+4069 -2340

No files matched your search

@@ -1,6 +1,7 @@
using System.Text;
using AIStudio.Agents;
using AIStudio.AIOperations;
using AIStudio.AIOperations.RetrievalContextValidation;
using AIStudio.Chat;
using AIStudio.Provider;
using AIStudio.Settings;
@@ -54,14 +55,21 @@ public sealed class AugmentationOne : IAugmentationProcess
// Want the user to validate all retrieval contexts?
if (settings.ConfigurationData.AgentRetrievalContextValidation.EnableRetrievalContextValidation && chatThread.DataSourceOptions.AutomaticValidation)
{
// Let's get the validation agent & set up its provider:
var validationAgent = Program.SERVICE_PROVIDER.GetService<AgentRetrievalContextValidation>()!;
if (validationAgent.SetLLMProvider(provider, chatThread.DataSecurity, chatThread.RequiredProviderConfidence))
//
// Which provider validates? Only one which may see what the chat may see: the model reads
// the passages. One choice serves all passages of this answer.
//
var validation = Program.SERVICE_PROVIDER.GetRequiredService<RetrievalContextValidationOperation>();
var providerResolver = Program.SERVICE_PROVIDER.GetRequiredService<AIOperationProviderResolver>();
var validationRequest = new RetrievalContextValidationRequest(chatThread.SystemPrompt, lastUserPrompt, preparedContexts, chatThread.RequiredProviderConfidence);
var validationProviderOptions = ClassicRagProviderOptions.ForDataAccess(settings, provider.ConfiguredProviderId, chatThread.DataSecurity, chatThread.RequiredProviderConfidence);
var validationChoice = providerResolver.Resolve(validation, validationRequest, RetrievalContextValidationOperation.GetProviderSettings(settings), validationProviderOptions);
if (validationChoice.HasProvider)
{
try
{
// Let's validate all retrieval contexts:
var validationResults = await validationAgent.ValidateRetrievalContextsAsync(lastUserPrompt, chatThread, preparedContexts, token);
var validationResults = await validation.RunAsync(validationRequest, new(validationChoice.Provider), token);
if (validationResults.Count == 0)
LOGGER.LogWarning("Retrieval context validation returned no results. Continuing augmentation with all retrieved contexts.");
else
@@ -98,7 +106,7 @@ public sealed class AugmentationOne : IAugmentationProcess
// an event. It does not change between two answers, so a message would repeat itself
// with every single one until the setting changes.
//
LOGGER.LogWarning("Skipping retrieval context validation because no sufficiently trusted validation agent provider is available. Continuing augmentation with all retrieved contexts.");
LOGGER.LogWarning("Skipping retrieval context validation because no provider which is trusted enough is available for it. Continuing augmentation with all retrieved contexts.");
}
}
@@ -0,0 +1,51 @@
using AIStudio.AIOperations;
using AIStudio.Provider;
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
namespace AIStudio.Tools.RAG;
/// <summary>
/// What the classic RAG process adds to the choice of a provider for its AI operations.
/// </summary>
/// <remarks>
/// The data source selection and the validation of retrieval contexts run with the same fallbacks, and
/// DataSourceService asks with them in advance which providers would see the data. All three build their
/// options here, so that the providers a check counts are the ones which run.
/// </remarks>
public static class ClassicRagProviderOptions
{
/// <summary>
/// Builds the options for an AI operation of the classic RAG process.
/// </summary>
/// <remarks>
/// Without a provider of its own, an operation falls back on the provider of the chat, and then on the
/// app-wide default. Its minimum confidence applies to both of them.
/// </remarks>
/// <param name="settingsManager">The settings, for the app-wide default.</param>
/// <param name="chatProviderId">The ID of the provider of the chat.</param>
/// <param name="checkProvider">An optional check of the caller, e.g., whether a provider may see the data sources.</param>
/// <returns>The options for the provider resolver.</returns>
public static AIOperationProviderOptions For(SettingsManager settingsManager, string chatProviderId, Func<AIStudio.Settings.Provider, AIOperationProviderIssue>? checkProvider = null) => new()
{
FallbackProviderIds = [chatProviderId, settingsManager.AppDefaultProviderId],
OwnMinimumConfidenceAppliesToFallbacks = true,
CheckProvider = checkProvider,
};
/// <summary>
/// Builds the options for an AI operation of the classic RAG process which reads the data of data sources.
/// </summary>
/// <remarks>
/// Besides the fallbacks, every candidate has to be allowed to see the data, by the security and the
/// confidence it requires. A candidate which is not reports NOT_ALLOWED_FOR_CONTENT.
/// </remarks>
/// <param name="settingsManager">The settings, for the app-wide default.</param>
/// <param name="chatProviderId">The ID of the provider of the chat.</param>
/// <param name="requiredDataSecurity">The data security the data requires.</param>
/// <param name="requiredConfidenceLevel">The provider confidence the data requires.</param>
/// <returns>The options for the provider resolver.</returns>
public static AIOperationProviderOptions ForDataAccess(SettingsManager settingsManager, string chatProviderId, DataSourceSecurity requiredDataSecurity, ConfidenceLevel requiredConfidenceLevel) => For(settingsManager, chatProviderId, candidate => candidate.AllowsDataSourceAccess(settingsManager, requiredDataSecurity, requiredConfidenceLevel)
? AIOperationProviderIssue.NONE
: AIOperationProviderIssue.NOT_ALLOWED_FOR_CONTENT);
}
@@ -1,4 +1,5 @@
using AIStudio.Agents;
using AIStudio.AIOperations;
using AIStudio.AIOperations.DataSourceChoice;
using AIStudio.Chat;
using AIStudio.Components;
using AIStudio.Provider;
@@ -31,14 +32,38 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
IReadOnlyList<IDataSource> selectedDataSources = [];
IReadOnlyList<DataSourceAgentSelected> finalAISelection = [];
// Get the agent for the data source selection:
var selectionAgent = Program.SERVICE_PROVIDER.GetService<AgentDataSourceSelection>()!;
var choiceOperation = Program.SERVICE_PROVIDER.GetRequiredService<DataSourceChoiceOperation>();
var providerResolver = Program.SERVICE_PROVIDER.GetRequiredService<AIOperationProviderResolver>();
var settingsManager = Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
var allowedDataSources = dataSources.AllowedDataSources.ToDictionary(ds => ds.Id, StringComparer.Ordinal);
try
{
// Let the AI agent do its work:
var aiSelectedDataSources = await selectionAgent.PerformSelectionAsync(provider, lastUserPrompt, chatThread, dataSources, token);
//
// Which provider chooses? Only one which may see the data sources: the model reads their
// descriptions, and what it chooses decides what is searched.
//
var request = new DataSourceChoiceRequest(chatThread.SystemPrompt, lastUserPrompt, dataSources.AllowedDataSources);
var choice = providerResolver.Resolve(choiceOperation, request, DataSourceChoiceOperation.GetProviderSettings(settingsManager), GetChoiceProviderOptions(settingsManager, provider.ConfiguredProviderId, dataSources.AllowedDataSources));
if (!choice.HasProvider)
{
LOGGER.LogWarning($"No provider may select the data sources, which require data security '{dataSources.AllowedDataSources.GetRequiredSecurityPolicy()}' and provider confidence '{dataSources.AllowedDataSources.GetRequiredConfidenceLevel().Name}'. The RAG process is skipped.");
proceedWithRAG = false;
return new(proceedWithRAG, selectedDataSources);
}
// Let the AI do its work:
var choiceResult = await choiceOperation.RunAsync(request, new(choice.Provider), token);
if (!choiceResult.IsAnswered)
{
LOGGER.LogWarning("The AI gave no usable answer to the selection of data sources. The RAG process is skipped.");
proceedWithRAG = false;
return new(proceedWithRAG, selectedDataSources);
}
var aiSelectedDataSources = choiceResult.SelectedDataSources.ToList();
// Check if the AI selected any data sources:
if (aiSelectedDataSources.Count is 0)
@@ -112,4 +137,17 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
}
#endregion
/// <summary>
/// What the process adds to the choice of the provider which selects the data sources.
/// </summary>
/// <remarks>
/// Besides the fallbacks of the classic RAG process, the provider has to be allowed to see the data
/// sources, by their security and by the confidence they require.
/// </remarks>
/// <param name="settingsManager">The settings.</param>
/// <param name="chatProviderId">The ID of the provider of the chat.</param>
/// <param name="dataSources">The data sources to choose from.</param>
/// <returns>The options for the provider resolver.</returns>
internal static AIOperationProviderOptions GetChoiceProviderOptions(SettingsManager settingsManager, string chatProviderId, IReadOnlyList<IDataSource> dataSources) => ClassicRagProviderOptions.ForDataAccess(settingsManager, chatProviderId, dataSources.GetRequiredSecurityPolicy(), dataSources.GetRequiredConfidenceLevel());
}
@@ -23,7 +23,7 @@ public interface IDataSourceSelectionProcess
/// <summary>
/// Starts the data source selection process.
/// </summary>
/// <param name="provider">The LLM provider. Used as default for data selection agents.</param>
/// <param name="provider">The LLM provider. Used as the fallback of the AI operation which selects data sources.</param>
/// <param name="lastUserPrompt">The last prompt that was issued by the user.</param>
/// <param name="chatThread">The chat thread.</param>
/// <param name="dataSources">The allowed data sources yielded by the data source service.</param>
@@ -293,12 +293,12 @@ public sealed class AISrcSelWithRetCtxVal : IRagProcess
/// Semantic Search asks the same place, DataSourceRequirements, so that both ways of searching
/// demand the same of the same data sources. Only the data sources whose passages reached the
/// AI count: one which found nothing leaves the chat as it was, and so does one whose passages
/// the validation agent dropped, or which did not fit into what the model can read, see
/// the validation dropped, or which did not fit into what the model can read, see
/// PassageBudget. Neither the model nor any of its tools ever saw those passages; the
/// validation agent did, but it has no tools. Otherwise, a data source of public papers could
/// validation did, but it has no tools. Otherwise, a data source of public papers could
/// fill the budget while the passages of a confidential one stayed out, and the chat would
/// still lose the web search for good. The data security and the provider confidence were
/// raised before the retrieval already, for every selected data source, because the agents see
/// raised before the retrieval already, for every selected data source, because the AI operations see
/// the passages as well; the data sources with passages are among them, so asking again would
/// change nothing.<br/><br/>
/// The answer which follows does not offer the web search any more: the request reads the