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 IContent? lastUserPrompt;
/// <summary>
/// Represents the type or category of this agent.
/// </summary>
@ -80,10 +78,10 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
Blocks = [],
};
protected DateTimeOffset AddUserRequest(ChatThread thread, string request)
protected UserRequest AddUserRequest(ChatThread thread, string request)
{
var time = DateTimeOffset.Now;
this.lastUserPrompt = new ContentText
var lastUserPrompt = new ContentText
{
Text = request,
};
@ -93,13 +91,17 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
Time = time,
ContentType = ContentType.TEXT,
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)
return;
@ -125,6 +127,6 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
// Use the selected provider to get the AI response.
// By awaiting this line, we wait for the entire
// 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;
var thread = this.CreateChatThread(this.SystemPrompt(availableDataSources));
var time = this.AddUserRequest(thread, text.Text);
await this.AddAIResponseAsync(thread, time);
var userRequest = this.AddUserRequest(thread, text.Text);
await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time);
var answer = thread.Blocks[^1];

View File

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

View File

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