Improved the augmentation and generation part of RAG by passing the augmented data into the system prompt

This commit is contained in:
Thorsten Sommer 2025-03-08 13:54:07 +01:00
parent 9a9a555e44
commit 8ba65988f9
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 30 additions and 21 deletions

View File

@ -40,6 +40,11 @@ public sealed record ChatThread
/// </summary> /// </summary>
public IReadOnlyList<DataSourceAgentSelected> AISelectedDataSources { get; set; } = []; public IReadOnlyList<DataSourceAgentSelected> AISelectedDataSources { get; set; } = [];
/// <summary>
/// The augmented data for this chat thread. Will be inserted into the system prompt.
/// </summary>
public string AugmentedData { get; set; } = string.Empty;
/// <summary> /// <summary>
/// The name of the chat thread. Usually generated by an AI model or manually edited by the user. /// The name of the chat thread. Usually generated by an AI model or manually edited by the user.
/// </summary> /// </summary>
@ -74,31 +79,48 @@ public sealed record ChatThread
/// <returns>The prepared system prompt.</returns> /// <returns>The prepared system prompt.</returns>
public string PrepareSystemPrompt(SettingsManager settingsManager, ChatThread chatThread, ILogger logger) public string PrepareSystemPrompt(SettingsManager settingsManager, ChatThread chatThread, ILogger logger)
{ {
var isAugmentedDataAvailable = !string.IsNullOrWhiteSpace(chatThread.AugmentedData);
var systemPromptWithAugmentedData = isAugmentedDataAvailable switch
{
true => $"""
{chatThread.SystemPrompt}
{chatThread.AugmentedData}
""",
false => chatThread.SystemPrompt,
};
if(isAugmentedDataAvailable)
logger.LogInformation("Augmented data is available for the chat thread.");
else
logger.LogInformation("No augmented data is available for the chat thread.");
// //
// Prepare the system prompt: // Prepare the system prompt:
// //
string systemPromptText; string systemPromptText;
var logMessage = $"Using no profile for chat thread '{chatThread.Name}'."; var logMessage = $"Using no profile for chat thread '{chatThread.Name}'.";
if (string.IsNullOrWhiteSpace(chatThread.SelectedProfile)) if (string.IsNullOrWhiteSpace(chatThread.SelectedProfile))
systemPromptText = chatThread.SystemPrompt; systemPromptText = systemPromptWithAugmentedData;
else else
{ {
if(!Guid.TryParse(chatThread.SelectedProfile, out var profileId)) if(!Guid.TryParse(chatThread.SelectedProfile, out var profileId))
systemPromptText = chatThread.SystemPrompt; systemPromptText = systemPromptWithAugmentedData;
else else
{ {
if(chatThread.SelectedProfile == Profile.NO_PROFILE.Id || profileId == Guid.Empty) if(chatThread.SelectedProfile == Profile.NO_PROFILE.Id || profileId == Guid.Empty)
systemPromptText = chatThread.SystemPrompt; systemPromptText = systemPromptWithAugmentedData;
else else
{ {
var profile = settingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == chatThread.SelectedProfile); var profile = settingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == chatThread.SelectedProfile);
if(profile == default) if(profile == default)
systemPromptText = chatThread.SystemPrompt; systemPromptText = systemPromptWithAugmentedData;
else else
{ {
logMessage = $"Using profile '{profile.Name}' for chat thread '{chatThread.Name}'."; logMessage = $"Using profile '{profile.Name}' for chat thread '{chatThread.Name}'.";
systemPromptText = $""" systemPromptText = $"""
{chatThread.SystemPrompt} {systemPromptWithAugmentedData}
{profile.ToSystemPrompt()} {profile.ToSystemPrompt()}
"""; """;

View File

@ -66,22 +66,8 @@ public sealed class AugmentationOne : IAugmentationProcess
// Let's convert all retrieval contexts to Markdown: // Let's convert all retrieval contexts to Markdown:
await retrievalContexts.AsMarkdown(sb, token); await retrievalContexts.AsMarkdown(sb, token);
// // Add the augmented data to the chat thread:
// Append the entire augmentation to the chat thread, chatThread.AugmentedData = sb.ToString();
// just before the user prompt:
//
chatThread.Blocks.Insert(chatThread.Blocks.Count - 1, new()
{
Role = ChatRole.RAG,
Time = DateTimeOffset.UtcNow,
ContentType = ContentType.TEXT,
HideFromUser = true,
Content = new ContentText
{
Text = sb.ToString(),
}
});
return chatThread; return chatThread;
} }

View File

@ -5,5 +5,6 @@
- Improved the error handling in the ERI data source info dialog in cases where servers respond with an invalid message. - Improved the error handling in the ERI data source info dialog in cases where servers respond with an invalid message.
- Improved the error handling for the entire RAG process. - Improved the error handling for the entire RAG process.
- Improved chat thread persistence after modifications through the RAG process. - Improved chat thread persistence after modifications through the RAG process.
- Improved the augmentation and generation part of RAG by passing the augmented data into the system prompt.
- Fixed the chat thread we use for the data retrieval by removing the last block, which is meant to be for the final AI answer. - Fixed the chat thread we use for the data retrieval by removing the last block, which is meant to be for the final AI answer.
- Fixed the data source name for ERI data sources when performing data retrieval. - Fixed the data source name for ERI data sources when performing data retrieval.