2026-11-21 19:47:10 +01:00
using AIStudio.AIOperations ;
using AIStudio.AIOperations.DataSourceChoice ;
2025-02-17 16:51:26 +01:00
using AIStudio.Chat ;
using AIStudio.Components ;
using AIStudio.Provider ;
using AIStudio.Settings ;
2025-05-04 14:59:30 +02:00
using AIStudio.Tools.PluginSystem ;
2025-02-17 16:51:26 +01:00
namespace AIStudio.Tools.RAG.DataSourceSelectionProcesses ;
public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
{
2025-09-25 19:47:18 +02:00
private static readonly ILogger < AgenticSrcSelWithDynHeur > LOGGER = Program . LOGGER_FACTORY . CreateLogger < AgenticSrcSelWithDynHeur >();
2025-05-04 14:59:30 +02:00
private static string TB ( string fallbackEN ) => I18N . I . T ( fallbackEN , typeof ( AgenticSrcSelWithDynHeur ). Namespace , nameof ( AgenticSrcSelWithDynHeur ));
2025-02-17 16:51:26 +01:00
#region Implementation of IDataSourceSelectionProcess
/// <inheritdoc />
public string TechnicalName => "AgenticSrcSelWithDynHeur" ;
/// <inheritdoc />
2025-05-04 14:59:30 +02:00
public string UIName => TB ( "Automatic AI data source selection with heuristik source reduction" );
2025-02-17 16:51:26 +01:00
/// <inheritdoc />
2025-05-04 14:59:30 +02:00
public string Description => TB ( "Automatically selects the appropriate data sources based on the last prompt. Applies a heuristic reduction at the end to reduce the number of data sources." );
2025-02-17 16:51:26 +01:00
/// <inheritdoc />
2025-09-25 19:47:18 +02:00
public async Task < DataSelectionResult > SelectDataSourcesAsync ( IProvider provider , IContent lastUserPrompt , ChatThread chatThread , AllowedSelectedDataSources dataSources , CancellationToken token = default )
2025-02-17 16:51:26 +01:00
{
var proceedWithRAG = true ;
IReadOnlyList < IDataSource > selectedDataSources = [];
IReadOnlyList < DataSourceAgentSelected > finalAISelection = [];
2026-11-21 19:47:10 +01:00
var choiceOperation = Program . SERVICE_PROVIDER . GetRequiredService < DataSourceChoiceOperation >();
var providerResolver = Program . SERVICE_PROVIDER . GetRequiredService < AIOperationProviderResolver >();
var settingsManager = Program . SERVICE_PROVIDER . GetRequiredService < SettingsManager >();
2026-09-09 18:43:37 +02:00
var allowedDataSources = dataSources . AllowedDataSources . ToDictionary ( ds => ds . Id , StringComparer . Ordinal );
2025-02-17 16:51:26 +01:00
try
{
2026-11-21 19:47:10 +01:00
//
// 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 ();
2025-02-17 16:51:26 +01:00
// Check if the AI selected any data sources:
if ( aiSelectedDataSources . Count is 0 )
{
2025-09-25 19:47:18 +02:00
LOGGER . LogWarning ( "The AI did not select any data sources. The RAG process is skipped." );
2025-02-17 16:51:26 +01:00
proceedWithRAG = false ;
return new ( proceedWithRAG , selectedDataSources );
}
// Log the selected data sources:
2026-09-14 11:20:57 +02:00
var selectedDataSourceInfo = string . Join ( ", " , aiSelectedDataSources . Select ( ds => $"'[Id={ds.Id}, reason={ds.Reason}, confidence={ds.Confidence}]'" ));
2025-09-25 19:47:18 +02:00
LOGGER . LogInformation ( $"The AI selected the data sources automatically. {aiSelectedDataSources.Count} data source(s) are selected: {selectedDataSourceInfo}." );
2025-02-17 16:51:26 +01:00
//
// Check how many data sources were hallucinated by the AI:
//
var totalAISelectedDataSources = aiSelectedDataSources . Count ;
// Filter out the data sources that are not available:
2026-09-09 18:43:37 +02:00
aiSelectedDataSources = aiSelectedDataSources . Where ( x => allowedDataSources . ContainsKey ( x . Id )). ToList ();
2025-02-17 16:51:26 +01:00
// Store the real AI-selected data sources:
2026-09-09 18:43:37 +02:00
finalAISelection = aiSelectedDataSources . Select ( x => new DataSourceAgentSelected { DataSource = allowedDataSources [ x . Id ], AIDecision = x , Selected = false }). ToList ();
2025-02-17 16:51:26 +01:00
var numHallucinatedSources = totalAISelectedDataSources - aiSelectedDataSources . Count ;
if ( numHallucinatedSources > 0 )
2026-09-09 18:43:37 +02:00
LOGGER . LogWarning ( $"The AI selected {numHallucinatedSources} unavailable data source(s). We ignore them." );
2025-02-17 16:51:26 +01:00
if ( aiSelectedDataSources . Count > 3 )
{
2025-02-22 20:51:06 +01:00
// We have more than 3 data sources. Let's filter by confidence:
var targetWindow = aiSelectedDataSources . DetermineTargetWindow ( TargetWindowStrategy . A_FEW_GOOD_ONES );
var threshold = aiSelectedDataSources . GetConfidenceThreshold ( targetWindow );
2025-02-17 16:51:26 +01:00
//
// Filter the data sources by the threshold:
//
aiSelectedDataSources = aiSelectedDataSources . Where ( x => x . Confidence >= threshold ). ToList ();
foreach ( var dataSource in finalAISelection )
if ( aiSelectedDataSources . Any ( x => x . Id == dataSource . DataSource . Id ))
dataSource . Selected = true ;
2025-09-25 19:47:18 +02:00
LOGGER . LogInformation ( $"The AI selected {aiSelectedDataSources.Count} data source(s) with a confidence of at least {threshold}." );
2025-02-17 16:51:26 +01:00
// Transform the final data sources to the actual data sources:
2026-09-09 18:43:37 +02:00
selectedDataSources = aiSelectedDataSources . Select ( x => allowedDataSources [ x . Id ]). ToList ();
2025-02-17 16:51:26 +01:00
return new ( proceedWithRAG , selectedDataSources );
}
//
// Case: we have max. 3 data sources. We take all of them:
//
// Transform the selected data sources to the actual data sources:
2026-09-09 18:43:37 +02:00
selectedDataSources = aiSelectedDataSources . Select ( x => allowedDataSources [ x . Id ]). ToList ();
2025-02-17 16:51:26 +01:00
// Mark the data sources as selected:
foreach ( var dataSource in finalAISelection )
dataSource . Selected = true ;
return new ( proceedWithRAG , selectedDataSources );
}
finally
{
// Send the selected data sources to the data source selection component.
// Then, the user can see which data sources were selected by the AI.
await MessageBus . INSTANCE . SendMessage ( null , Event . RAG_AUTO_DATA_SOURCES_SELECTED , finalAISelection );
chatThread . AISelectedDataSources = finalAISelection ;
}
}
#endregion
2026-11-21 19:47:10 +01:00
/// <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 ());
2025-02-17 16:51:26 +01:00
}