Refactored the agent base in preparation for multithreading support

This commit is contained in:
Thorsten Sommer 2025-02-19 12:06:01 +01:00
parent bc4c4d1ecf
commit 9232e27f71
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 36 additions and 15 deletions

View File

@ -32,8 +32,6 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
protected ILogger<AgentBase> Logger { get; init; } = logger; protected ILogger<AgentBase> Logger { get; init; } = logger;
protected IContent? lastUserPrompt;
/// <summary> /// <summary>
/// Represents the type or category of this agent. /// Represents the type or category of this agent.
/// </summary> /// </summary>
@ -80,10 +78,10 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
Blocks = [], Blocks = [],
}; };
protected DateTimeOffset AddUserRequest(ChatThread thread, string request) protected UserRequest AddUserRequest(ChatThread thread, string request)
{ {
var time = DateTimeOffset.Now; var time = DateTimeOffset.Now;
this.lastUserPrompt = new ContentText var lastUserPrompt = new ContentText
{ {
Text = request, Text = request,
}; };
@ -93,13 +91,17 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
Time = time, Time = time,
ContentType = ContentType.TEXT, ContentType = ContentType.TEXT,
Role = ChatRole.USER, Role = ChatRole.USER,
Content = this.lastUserPrompt, Content = lastUserPrompt,
}); });
return time; return new()
{
Time = time,
UserPrompt = lastUserPrompt,
};
} }
protected async Task AddAIResponseAsync(ChatThread thread, DateTimeOffset time) protected async Task AddAIResponseAsync(ChatThread thread, IContent lastUserPrompt, DateTimeOffset time)
{ {
if(this.ProviderSettings is null) if(this.ProviderSettings is null)
return; return;
@ -125,6 +127,6 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
// Use the selected provider to get the AI response. // Use the selected provider to get the AI response.
// By awaiting this line, we wait for the entire // By awaiting this line, we wait for the entire
// content to be streamed. // content to be streamed.
await aiText.CreateFromProviderAsync(providerSettings.CreateProvider(this.Logger), providerSettings.Model, this.lastUserPrompt, thread); await aiText.CreateFromProviderAsync(providerSettings.CreateProvider(this.Logger), providerSettings.Model, lastUserPrompt, thread);
} }
} }

View File

@ -111,8 +111,8 @@ public sealed class AgentDataSourceSelection (ILogger<AgentDataSourceSelection>
return EMPTY_BLOCK; return EMPTY_BLOCK;
var thread = this.CreateChatThread(this.SystemPrompt(availableDataSources)); var thread = this.CreateChatThread(this.SystemPrompt(availableDataSources));
var time = this.AddUserRequest(thread, text.Text); var userRequest = this.AddUserRequest(thread, text.Text);
await this.AddAIResponseAsync(thread, time); await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time);
var answer = thread.Blocks[^1]; var answer = thread.Blocks[^1];

View File

@ -96,8 +96,8 @@ public sealed class AgentRetrievalContextValidation (ILogger<AgentRetrievalConte
return EMPTY_BLOCK; return EMPTY_BLOCK;
var thread = this.CreateChatThread(this.SystemPrompt(retrievalContext)); var thread = this.CreateChatThread(this.SystemPrompt(retrievalContext));
var time = this.AddUserRequest(thread, text.Text); var userRequest = this.AddUserRequest(thread, text.Text);
await this.AddAIResponseAsync(thread, time); await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time);
return thread.Blocks[^1]; return thread.Blocks[^1];
} }

View File

@ -65,8 +65,8 @@ public sealed class AgentTextContentCleaner(ILogger<AgentBase> logger, SettingsM
return EMPTY_BLOCK; return EMPTY_BLOCK;
var thread = this.CreateChatThread(this.SystemPrompt(sourceURL)); var thread = this.CreateChatThread(this.SystemPrompt(sourceURL));
var time = this.AddUserRequest(thread, text.Text); var userRequest = this.AddUserRequest(thread, text.Text);
await this.AddAIResponseAsync(thread, time); await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time);
var answer = thread.Blocks[^1]; var answer = thread.Blocks[^1];
this.answers.Add(answer); this.answers.Add(answer);

View File

@ -0,0 +1,19 @@
using AIStudio.Chat;
namespace AIStudio.Agents;
/// <summary>
/// The created user request.
/// </summary>
public sealed class UserRequest
{
/// <summary>
/// The time when the request was created.
/// </summary>
public required DateTimeOffset Time { get; init; }
/// <summary>
/// The user prompt.
/// </summary>
public required IContent UserPrompt { get; init; }
}