Skip to content

Commit bfc9f2e

Browse files
Added a retrieval context validation agent (#289)
1 parent 96e6372 commit bfc9f2e

26 files changed

+829
-135
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Things we are currently working on:
2020
- [ ] Runtime: Integration of the vector database [LanceDB](https://github.com/lancedb/lancedb)
2121
- [ ] App: Implement the continuous process of vectorizing data
2222
- [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))~~
23-
- [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))~~
23+
- [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))~~
2424
- [x] ~~App: Integrate data sources in chats (PR [#282](https://github.com/MindWorkAI/AI-Studio/pull/282))~~
2525

2626

app/MindWork AI Studio/Agents/AgentBase.cs

+19-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ namespace AIStudio.Agents;
1111

1212
public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager settingsManager, DataSourceService dataSourceService, ThreadSafeRandom rng) : IAgent
1313
{
14+
protected static readonly ContentBlock EMPTY_BLOCK = new()
15+
{
16+
Content = null,
17+
ContentType = ContentType.NONE,
18+
Role = ChatRole.AGENT,
19+
Time = DateTimeOffset.UtcNow,
20+
};
21+
1422
protected static readonly JsonSerializerOptions JSON_SERIALIZER_OPTIONS = new()
1523
{
1624
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
@@ -24,8 +32,6 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
2432

2533
protected ILogger<AgentBase> Logger { get; init; } = logger;
2634

27-
protected IContent? lastUserPrompt;
28-
2935
/// <summary>
3036
/// Represents the type or category of this agent.
3137
/// </summary>
@@ -72,10 +78,10 @@ public abstract class AgentBase(ILogger<AgentBase> logger, SettingsManager setti
7278
Blocks = [],
7379
};
7480

75-
protected DateTimeOffset AddUserRequest(ChatThread thread, string request)
81+
protected UserRequest AddUserRequest(ChatThread thread, string request)
7682
{
7783
var time = DateTimeOffset.Now;
78-
this.lastUserPrompt = new ContentText
84+
var lastUserPrompt = new ContentText
7985
{
8086
Text = request,
8187
};
@@ -85,13 +91,17 @@ protected DateTimeOffset AddUserRequest(ChatThread thread, string request)
8591
Time = time,
8692
ContentType = ContentType.TEXT,
8793
Role = ChatRole.USER,
88-
Content = this.lastUserPrompt,
94+
Content = lastUserPrompt,
8995
});
90-
91-
return time;
96+
97+
return new()
98+
{
99+
Time = time,
100+
UserPrompt = lastUserPrompt,
101+
};
92102
}
93103

94-
protected async Task AddAIResponseAsync(ChatThread thread, DateTimeOffset time)
104+
protected async Task AddAIResponseAsync(ChatThread thread, IContent lastUserPrompt, DateTimeOffset time)
95105
{
96106
if(this.ProviderSettings is null)
97107
return;
@@ -117,6 +127,6 @@ protected async Task AddAIResponseAsync(ChatThread thread, DateTimeOffset time)
117127
// Use the selected provider to get the AI response.
118128
// By awaiting this line, we wait for the entire
119129
// content to be streamed.
120-
await aiText.CreateFromProviderAsync(providerSettings.CreateProvider(this.Logger), providerSettings.Model, this.lastUserPrompt, thread);
130+
await aiText.CreateFromProviderAsync(providerSettings.CreateProvider(this.Logger), providerSettings.Model, lastUserPrompt, thread);
121131
}
122132
}

app/MindWork AI Studio/Agents/AgentDataSourceSelection.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ namespace AIStudio.Agents;
1212

1313
public sealed class AgentDataSourceSelection (ILogger<AgentDataSourceSelection> logger, ILogger<AgentBase> baseLogger, SettingsManager settingsManager, DataSourceService dataSourceService, ThreadSafeRandom rng) : AgentBase(baseLogger, settingsManager, dataSourceService, rng)
1414
{
15-
private static readonly ContentBlock EMPTY_BLOCK = new()
16-
{
17-
Content = null,
18-
ContentType = ContentType.NONE,
19-
Role = ChatRole.AGENT,
20-
Time = DateTimeOffset.UtcNow,
21-
};
22-
2315
private readonly List<ContentBlock> answers = new();
2416

2517
#region Overrides of AgentBase
@@ -119,8 +111,8 @@ public override async Task<ContentBlock> ProcessInput(ContentBlock input, IDicti
119111
return EMPTY_BLOCK;
120112

121113
var thread = this.CreateChatThread(this.SystemPrompt(availableDataSources));
122-
var time = this.AddUserRequest(thread, text.Text);
123-
await this.AddAIResponseAsync(thread, time);
114+
var userRequest = this.AddUserRequest(thread, text.Text);
115+
await this.AddAIResponseAsync(thread, userRequest.UserPrompt, userRequest.Time);
124116

125117
var answer = thread.Blocks[^1];
126118

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

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

357349
/// <summary>
358350
/// Extracts the JSON list from the given text. The text may contain additional

0 commit comments

Comments
 (0)