Improved profile and system prompt handling (#241)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2025-01-02 14:50:54 +01:00
1 parent caec3bfd2c
commit 38a45955fb
15 files changed
+113 -46

No files matched your search

+58
View File
@@ -1,3 +1,5 @@
using AIStudio.Settings;
namespace AIStudio.Chat;
/// <summary>
@@ -20,6 +22,11 @@ public sealed record ChatThread
/// </summary>
public string SelectedProvider { get; set; } = string.Empty;
/// <summary>
/// Specifies the profile selected for the chat thread.
/// </summary>
public string SelectedProfile { get; set; } = string.Empty;
/// <summary>
/// The name of the chat thread. Usually generated by an AI model or manually edited by the user.
/// </summary>
@@ -39,4 +46,55 @@ public sealed record ChatThread
/// The content blocks of the chat thread.
/// </summary>
public List<ContentBlock> Blocks { get; init; } = [];
/// <summary>
/// Prepares the system prompt for the chat thread.
/// </summary>
/// <remarks>
/// The actual system prompt depends on the selected profile. If no profile is selected,
/// the system prompt is returned as is. When a profile is selected, the system prompt
/// is extended with the profile chosen.
/// </remarks>
/// <param name="settingsManager">The settings manager instance to use.</param>
/// <param name="chatThread">The chat thread to prepare the system prompt for.</param>
/// <param name="logger">The logger instance to use.</param>
/// <returns>The prepared system prompt.</returns>
public string PrepareSystemPrompt(SettingsManager settingsManager, ChatThread chatThread, ILogger logger)
{
//
// Prepare the system prompt:
//
string systemPromptText;
var logMessage = $"Using no profile for chat thread '{chatThread.Name}'.";
if (string.IsNullOrWhiteSpace(chatThread.SelectedProfile))
systemPromptText = chatThread.SystemPrompt;
else
{
if(!Guid.TryParse(chatThread.SelectedProfile, out var profileId))
systemPromptText = chatThread.SystemPrompt;
else
{
if(chatThread.SelectedProfile == Profile.NO_PROFILE.Id || profileId == Guid.Empty)
systemPromptText = chatThread.SystemPrompt;
else
{
var profile = settingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == chatThread.SelectedProfile);
if(profile == default)
systemPromptText = chatThread.SystemPrompt;
else
{
logMessage = $"Using profile '{profile.Name}' for chat thread '{chatThread.Name}'.";
systemPromptText = $"""
{chatThread.SystemPrompt}
{profile.ToSystemPrompt()}
""";
}
}
}
}
logger.LogInformation(logMessage);
return systemPromptText;
}
}