Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a retrieval context validation agent #289

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
74757c8
Refactored EMPTY_BLOCK into the agent base class
SommerEngineering Feb 18, 2025
bcb2c3c
Updated RAG tasks in README.md
SommerEngineering Feb 18, 2025
b816b09
Spelling
SommerEngineering Feb 18, 2025
4f33696
Ensure that RAG-related settings are hidden behind the RAG feature flag
SommerEngineering Feb 18, 2025
a9bf3d5
Fixed the extension method
SommerEngineering Feb 18, 2025
e41b5ff
Use the extension method for conversion
SommerEngineering Feb 18, 2025
50c1b45
Refactored the conversion from retrieval contexts to Markdown
SommerEngineering Feb 18, 2025
ccc3089
Optimization
SommerEngineering Feb 18, 2025
ea96f08
Added an agent to validate whether a retrieval context makes sense fo…
SommerEngineering Feb 18, 2025
129e0d0
Added an option to enable or disable the context validation
SommerEngineering Feb 18, 2025
9c446c5
Refactored the algorithm to determine an optimal confidence threshold…
SommerEngineering Feb 19, 2025
6870327
Fixed spelling
SommerEngineering Feb 19, 2025
bc4c4d1
Fixed method naming
SommerEngineering Feb 19, 2025
9232e27
Refactored the agent base in preparation for multithreading support
SommerEngineering Feb 19, 2025
4838904
Refactored the retrieval context validation agent to enable parallel …
SommerEngineering Feb 19, 2025
1d93a69
Added the retrieval context to the retrieval context validation result
SommerEngineering Feb 22, 2025
3b42a4e
Refactored a target window struct with multiple strategies
SommerEngineering Feb 22, 2025
a9f821b
Added an option for how any validation agents are run simultaneously
SommerEngineering Feb 22, 2025
3485d3c
Implemented a parallel execution for the validation of many retrieval…
SommerEngineering Feb 22, 2025
b2e3ddf
Added the retrieval context validation
SommerEngineering Feb 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Things we are currently working on:
- [ ] Runtime: Integration of the vector database [LanceDB](https://github.com/lancedb/lancedb)
- [ ] App: Implement the continuous process of vectorizing data
- [x] ~~App: Define a common retrieval context interface for the integration of RAG processes in chats (PR [#281](https://github.com/MindWorkAI/AI-Studio/pull/281), [#284](https://github.com/MindWorkAI/AI-Studio/pull/284), [#286](https://github.com/MindWorkAI/AI-Studio/pull/286), [#287](https://github.com/MindWorkAI/AI-Studio/pull/287))~~
- [x] ~~App: Define a common augmentation interface for the integration of RAG processes in chats (PR [#288](https://github.com/MindWorkAI/AI-Studio/pull/288))~~
- [x] ~~App: Define a common augmentation interface for the integration of RAG processes in chats (PR [#288](https://github.com/MindWorkAI/AI-Studio/pull/288), [#289](https://github.com/MindWorkAI/AI-Studio/pull/289))~~
- [x] ~~App: Integrate data sources in chats (PR [#282](https://github.com/MindWorkAI/AI-Studio/pull/282))~~


Expand Down
28 changes: 19 additions & 9 deletions app/MindWork AI Studio/Agents/AgentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ namespace AIStudio.Agents;

public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager settingsManager, DataSourceService dataSourceService, ThreadSafeRandom rng) : IAgent
{
protected static readonly ContentBlock EMPTY_BLOCK = new()
{
Content = null,
ContentType = ContentType.NONE,
Role = ChatRole.AGENT,
Time = DateTimeOffset.UtcNow,
};

protected static readonly JsonSerializerOptions JSON_SERIALIZER_OPTIONS = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
Expand All @@ -24,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>
Expand Down Expand Up @@ -72,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,
};
Expand All @@ -85,13 +91,17 @@ protected DateTimeOffset AddUserRequest(ChatThread thread, string request)
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;
Expand All @@ -117,6 +127,6 @@ protected async Task AddAIResponseAsync(ChatThread thread, DateTimeOffset time)
// 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);
}
}
16 changes: 4 additions & 12 deletions app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ namespace AIStudio.Agents;

public sealed class AgentDataSourceSelection (ILogger<AgentDataSourceSelection> logger, ILogger<AgentBase> baseLogger, SettingsManager settingsManager, DataSourceService dataSourceService, ThreadSafeRandom rng) : AgentBase(baseLogger, settingsManager, dataSourceService, rng)
{
private static readonly ContentBlock EMPTY_BLOCK = new()
{
Content = null,
ContentType = ContentType.NONE,
Role = ChatRole.AGENT,
Time = DateTimeOffset.UtcNow,
};

private readonly List<ContentBlock> answers = new();

#region Overrides of AgentBase
Expand Down Expand Up @@ -119,8 +111,8 @@ public override async Task<ContentBlock> ProcessInput(ContentBlock input, IDicti
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];

Expand Down Expand Up @@ -306,7 +298,7 @@ public async Task<List<SelectedDataSource>> PerformSelectionAsync(IProvider prov
// We know how bad LLM may be in generating JSON without surrounding text.
// Thus, we expect the worst and try to extract the JSON list from the text:
//
var json = this.ExtractJson(selectedDataSourcesJson);
var json = ExtractJson(selectedDataSourcesJson);

try
{
Expand Down Expand Up @@ -352,7 +344,7 @@ public async Task<List<SelectedDataSource>> PerformSelectionAsync(IProvider prov
/// </remarks>
/// <param name="text">The text that may contain the JSON list.</param>
/// <returns>The extracted JSON list.</returns>
private string ExtractJson(string text) => ExtractJson(text.AsSpan()).ToString();
private static string ExtractJson(string text) => ExtractJson(text.AsSpan()).ToString();

/// <summary>
/// Extracts the JSON list from the given text. The text may contain additional
Expand Down
Loading