Refactored the system prompt to use a dynamic date and time processing

This commit is contained in:
Thorsten Sommer 2026-01-15 22:05:32 +01:00
parent 0b006cd330
commit a72ec276b8
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
16 changed files with 42 additions and 39 deletions

View File

@ -99,7 +99,7 @@ public partial class AssistantAgenda : AssistantBaseCore<SettingsDialogAgenda>
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -120,7 +120,7 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<SettingsDialo
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -28,7 +28,7 @@ public partial class AssistantEMail : AssistantBaseCore<SettingsDialogWritingEMa
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -43,7 +43,7 @@ public partial class AssistantGrammarSpelling : AssistantBaseCore<SettingsDialog
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -53,7 +53,7 @@ public partial class AssistantJobPostings : AssistantBaseCore<SettingsDialogJobP
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -30,7 +30,7 @@ public partial class AssistantLegalCheck : AssistantBaseCore<SettingsDialogLegal
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -33,7 +33,7 @@ public partial class AssistantMyTasks : AssistantBaseCore<SettingsDialogMyTasks>
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -44,7 +44,7 @@ public partial class AssistantRewriteImprove : AssistantBaseCore<SettingsDialogR
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -55,7 +55,7 @@ public partial class AssistantSynonyms : AssistantBaseCore<SettingsDialogSynonym
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -32,7 +32,7 @@ public partial class AssistantTextSummarizer : AssistantBaseCore<SettingsDialogT
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -31,7 +31,7 @@ public partial class AssistantTranslation : AssistantBaseCore<SettingsDialogTran
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
};
protected override void ResetForm()

View File

@ -1,3 +1,5 @@
using System.Globalization;
using AIStudio.Components;
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
@ -91,7 +93,7 @@ public sealed record ChatThread
// Use the information from the chat template, if provided. Otherwise, use the default system prompt
//
string systemPromptTextWithChatTemplate;
var logMessage = $"Using no chat template for chat thread '{chatThread.Name}'.";
var firstLogMessage = $"Using no chat template for chat thread '{chatThread.Name}'.";
if (string.IsNullOrWhiteSpace(chatThread.SelectedChatTemplate))
systemPromptTextWithChatTemplate = chatThread.SystemPrompt;
else
@ -109,7 +111,7 @@ public sealed record ChatThread
systemPromptTextWithChatTemplate = chatThread.SystemPrompt;
else
{
logMessage = $"Using chat template '{chatTemplate.Name}' for chat thread '{chatThread.Name}'.";
firstLogMessage = $"Using chat template '{chatTemplate.Name}' for chat thread '{chatThread.Name}'.";
this.allowProfile = chatTemplate.AllowProfileUsage;
systemPromptTextWithChatTemplate = chatTemplate.ToSystemPrompt();
}
@ -122,8 +124,8 @@ public sealed record ChatThread
// default system prompt:
chatThread = chatThread with { SystemPrompt = systemPromptTextWithChatTemplate };
LOGGER.LogInformation(logMessage);
LOGGER.LogInformation(firstLogMessage);
//
// Add augmented data, if available:
//
@ -149,7 +151,7 @@ public sealed record ChatThread
// Add information from the profile if available and allowed:
//
string systemPromptText;
logMessage = $"Using no profile for chat thread '{chatThread.Name}'.";
var secondLogMessage = $"Using no profile for chat thread '{chatThread.Name}'.";
if (string.IsNullOrWhiteSpace(chatThread.SelectedProfile) || this.allowProfile is false)
systemPromptText = systemPromptWithAugmentedData;
else
@ -163,11 +165,11 @@ public sealed record ChatThread
else
{
var profile = settingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == chatThread.SelectedProfile);
if(profile == default)
if(profile is null)
systemPromptText = systemPromptWithAugmentedData;
else
{
logMessage = $"Using profile '{profile.Name}' for chat thread '{chatThread.Name}'.";
secondLogMessage = $"Using profile '{profile.Name}' for chat thread '{chatThread.Name}'.";
systemPromptText = $"""
{systemPromptWithAugmentedData}
@ -178,8 +180,23 @@ public sealed record ChatThread
}
}
LOGGER.LogInformation(logMessage);
return systemPromptText;
LOGGER.LogInformation(secondLogMessage);
//
// Prepend the current date and time to the system prompt:
//
var nowUtc = DateTime.UtcNow;
var nowLocal = DateTime.Now;
var currentDateTime = string.Create(
new CultureInfo("en-US"),
$"Today is {nowUtc:MMMM d, yyyy h:mm tt} (UTC) and {nowLocal:MMMM d, yyyy h:mm tt} (local time)."
);
return $"""
{currentDateTime}
{systemPromptText}
""";
}
/// <summary>

View File

@ -1,20 +1,6 @@
using System.Globalization;
namespace AIStudio.Chat;
public static class SystemPrompts
{
public static string Default
{
get
{
var nowUtc = DateTime.UtcNow;
var nowLocal = DateTime.Now;
return string.Create(
new CultureInfo("en-US"),
$"Today is {nowUtc:MMMM d, yyyy h:mm tt} (UTC) and {nowLocal:MMMM d, yyyy h:mm tt} (local time)."
);
}
}
public const string DEFAULT = "You are a helpful assistant.";
}

View File

@ -439,7 +439,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
SelectedProvider = this.Provider.Id,
SelectedProfile = this.currentProfile.Id,
SelectedChatTemplate = this.currentChatTemplate.Id,
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
WorkspaceId = this.currentWorkspaceId,
ChatId = Guid.NewGuid(),
DataSourceOptions = this.earlyDataSourceOptions,
@ -679,7 +679,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
SelectedProvider = this.Provider.Id,
SelectedProfile = this.currentProfile.Id,
SelectedChatTemplate = this.currentChatTemplate.Id,
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
WorkspaceId = this.currentWorkspaceId,
ChatId = Guid.NewGuid(),
Name = string.Empty,

View File

@ -573,7 +573,7 @@ public partial class Workspaces : MSGComponentBase
WorkspaceId = workspaceId,
ChatId = Guid.NewGuid(),
Name = string.Empty,
SystemPrompt = SystemPrompts.Default,
SystemPrompt = SystemPrompts.DEFAULT,
Blocks = [],
};

View File

@ -263,7 +263,7 @@ public partial class ChatTemplateDialog : MSGComponentBase
private void UseDefaultSystemPrompt()
{
this.DataSystemPrompt = SystemPrompts.Default;
this.DataSystemPrompt = SystemPrompts.DEFAULT;
}
private void Cancel() => this.MudDialog.Cancel();