Skip to content

Commit a13f925

Browse files
authored
Merge pull request #809 from iceljc/master
refine agent knowledge base
2 parents 699f913 + bcb2a9d commit a13f925

File tree

11 files changed

+82
-44
lines changed

11 files changed

+82
-44
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public static Agent Clone(Agent agent)
155155
Profiles = agent.Profiles,
156156
RoutingRules = agent.RoutingRules,
157157
LlmConfig = agent.LlmConfig,
158+
KnowledgeBases = agent.KnowledgeBases,
158159
CreatedDateTime = agent.CreatedDateTime,
159160
UpdatedDateTime = agent.UpdatedDateTime,
160161
};
@@ -168,43 +169,49 @@ public Agent SetInstruction(string instruction)
168169

169170
public Agent SetChannelInstructions(List<ChannelInstruction> instructions)
170171
{
171-
ChannelInstructions = instructions ?? new List<ChannelInstruction>();
172+
ChannelInstructions = instructions ?? [];
172173
return this;
173174
}
174175

175176
public Agent SetTemplates(List<AgentTemplate> templates)
176177
{
177-
Templates = templates ?? new List<AgentTemplate>();
178+
Templates = templates ?? [];
178179
return this;
179180
}
180181

181182
public Agent SetTasks(List<AgentTask> tasks)
182183
{
183-
Tasks = tasks ?? new List<AgentTask>();
184+
Tasks = tasks ?? [];
184185
return this;
185186
}
186187

187188
public Agent SetFunctions(List<FunctionDef> functions)
188189
{
189-
Functions = functions ?? new List<FunctionDef>();
190+
Functions = functions ?? [];
190191
return this;
191192
}
192193

193194
public Agent SetSamples(List<string> samples)
194195
{
195-
Samples = samples ?? new List<string>();
196+
Samples = samples ?? [];
196197
return this;
197198
}
198199

199200
public Agent SetUtilities(List<AgentUtility> utilities)
200201
{
201-
Utilities = utilities ?? new List<AgentUtility>();
202+
Utilities = utilities ?? [];
203+
return this;
204+
}
205+
206+
public Agent SetKnowledgeBases(List<AgentKnowledgeBase> knowledgeBases)
207+
{
208+
knowledgeBases = knowledgeBases ?? [];
202209
return this;
203210
}
204211

205212
public Agent SetResponses(List<AgentResponse> responses)
206213
{
207-
Responses = responses ?? new List<AgentResponse>(); ;
214+
Responses = responses ?? [];
208215
return this;
209216
}
210217

@@ -252,13 +259,13 @@ public Agent SetAgentType(string type)
252259

253260
public Agent SetProfiles(List<string> profiles)
254261
{
255-
Profiles = profiles ?? new List<string>();
262+
Profiles = profiles ?? [];
256263
return this;
257264
}
258265

259266
public Agent SetRoutingRules(List<RoutingRule> rules)
260267
{
261-
RoutingRules = rules ?? new List<RoutingRule>();
268+
RoutingRules = rules ?? [];
262269
return this;
263270
}
264271

src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentKnowledgeBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ namespace BotSharp.Abstraction.Agents.Models;
22

33
public class AgentKnowledgeBase
44
{
5-
public string? Name { get; set; }
5+
public string Name { get; set; }
6+
public string Type { get; set; }
67
public bool Disabled { get; set; }
78

89
public AgentKnowledgeBase()
910
{
1011

1112
}
1213

13-
public AgentKnowledgeBase(string name, bool enabled)
14+
public AgentKnowledgeBase(string name, string type, bool enabled)
1415
{
1516
Name = name;
17+
Type = type;
1618
Disabled = enabled;
1719
}
1820

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IKnowledgeService
99
Task<bool> ExistVectorCollection(string collectionName);
1010
Task<bool> CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model);
1111
Task<bool> DeleteVectorCollection(string collectionName);
12-
Task<IEnumerable<string>> GetVectorCollections(string type);
12+
Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(string? type = null);
1313
Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options);
1414
Task<StringIdPagedItems<VectorSearchResult>> GetPagedVectorCollectionData(string collectionName, VectorFilter filter);
1515
Task<bool> DeleteVectorCollectionData(string collectionName, string id);

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public async Task<string> UpdateAgentFromFile(string id)
103103
.SetResponses(foundAgent.Responses)
104104
.SetSamples(foundAgent.Samples)
105105
.SetUtilities(foundAgent.Utilities)
106+
.SetKnowledgeBases(foundAgent.KnowledgeBases)
106107
.SetLlmConfig(foundAgent.LlmConfig);
107108

108109
_db.UpdateAgent(clonedAgent, AgentField.All);

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public class AgentController : ControllerBase
1010
private readonly IUserIdentity _user;
1111
private readonly IServiceProvider _services;
1212

13-
public AgentController(IAgentService agentService, IUserIdentity user, IServiceProvider services)
13+
public AgentController(
14+
IAgentService agentService,
15+
IUserIdentity user,
16+
IServiceProvider services)
1417
{
1518
_agentService = agentService;
1619
_user = user;

src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public async Task<bool> ExistVectorCollection([FromRoute] string collection)
2626
}
2727

2828
[HttpGet("knowledge/vector/collections")]
29-
public async Task<IEnumerable<string>> GetVectorCollections([FromQuery] string type)
29+
public async Task<IEnumerable<VectorCollectionConfigViewModel>> GetVectorCollections([FromQuery] string? type = null)
3030
{
31-
return await _knowledgeService.GetVectorCollections(type);
31+
var collections = await _knowledgeService.GetVectorCollections(type);
32+
return collections.Select(x => VectorCollectionConfigViewModel.From(x));
3233
}
3334

3435
[HttpPost("knowledge/vector/create-collection")]

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Functions.Models;
3-
using BotSharp.Abstraction.Routing.Models;
43

54
namespace BotSharp.OpenAPI.ViewModels.Agents;
65

@@ -55,6 +54,7 @@ public class AgentCreationModel
5554

5655
public List<AgentUtility> Utilities { get; set; } = new();
5756
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
57+
public List<AgentKnowledgeBase> KnowledgeBases { get; set; } = new();
5858
public AgentLlmConfig? LlmConfig { get; set; }
5959

6060
public Agent ToAgent()
@@ -76,8 +76,9 @@ public Agent ToAgent()
7676
MergeUtility = MergeUtility,
7777
MaxMessageCount = MaxMessageCount,
7878
Profiles = Profiles,
79-
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
80-
LlmConfig = LlmConfig
79+
LlmConfig = LlmConfig,
80+
KnowledgeBases = KnowledgeBases,
81+
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [],
8182
};
8283
}
8384
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using BotSharp.Abstraction.VectorStorage.Models;
2+
3+
namespace BotSharp.OpenAPI.ViewModels.Knowledges;
4+
5+
public class VectorCollectionConfigViewModel : VectorCollectionConfig
6+
{
7+
public static VectorCollectionConfigViewModel From(VectorCollectionConfig model)
8+
{
9+
return new VectorCollectionConfigViewModel
10+
{
11+
Name = model.Name,
12+
Type = model.Type,
13+
VectorStore = model.VectorStore,
14+
TextEmbedding = model.TextEmbedding
15+
};
16+
}
17+
}

src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,25 @@ public async Task<bool> CreateVectorCollection(string collectionName, string col
6969
}
7070
}
7171

72-
public async Task<IEnumerable<string>> GetVectorCollections(string type)
72+
public async Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(string? type = null)
7373
{
7474
try
7575
{
7676
var db = _services.GetRequiredService<IBotSharpRepository>();
77-
var collectionNames = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
77+
var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
7878
{
79-
CollectionTypes = new[] { type },
80-
VectorStroageProviders = new[] { _settings.VectorDb.Provider }
81-
}).Select(x => x.Name).ToList();
79+
CollectionTypes = !string.IsNullOrEmpty(type) ? [type] : null,
80+
VectorStroageProviders = [_settings.VectorDb.Provider]
81+
}).ToList();
8282

8383
var vectorDb = GetVectorDb();
84-
var vectorCollections = await vectorDb.GetCollections();
85-
return vectorCollections.Where(x => collectionNames.Contains(x));
84+
var dbCollections = await vectorDb.GetCollections();
85+
return configs.Where(x => dbCollections.Contains(x.Name));
8686
}
8787
catch (Exception ex)
8888
{
8989
_logger.LogWarning($"Error when getting vector db collections. {ex.Message}\r\n{ex.InnerException}");
90-
return Enumerable.Empty<string>();
90+
return Enumerable.Empty<VectorCollectionConfig>();
9191
}
9292
}
9393

src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentKnowledgeBaseMongoElement.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ namespace BotSharp.Plugin.MongoStorage.Models;
55
public class AgentKnowledgeBaseMongoElement
66
{
77
public string Name { get; set; }
8+
public string Type { get; set; }
89
public bool Disabled { get; set; }
10+
911
public static AgentKnowledgeBaseMongoElement ToMongoElement(AgentKnowledgeBase knowledgeBase)
1012
{
1113
return new AgentKnowledgeBaseMongoElement
1214
{
13-
Name = knowledgeBase.Name ?? string.Empty,
14-
Disabled = knowledgeBase.Disabled,
15+
Name = knowledgeBase.Name,
16+
Type = knowledgeBase.Type,
17+
Disabled = knowledgeBase.Disabled
1518
};
1619
}
1720

@@ -20,6 +23,7 @@ public static AgentKnowledgeBase ToDomainElement(AgentKnowledgeBaseMongoElement
2023
return new AgentKnowledgeBase
2124
{
2225
Name = knowledgeBase.Name,
26+
Type = knowledgeBase.Type,
2327
Disabled = knowledgeBase.Disabled
2428
};
2529
}

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,22 @@ public void BulkInsertAgents(List<Agent> agents)
439439
IconUrl = x.IconUrl,
440440
Description = x.Description,
441441
Instruction = x.Instruction,
442-
ChannelInstructions = x.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?.ToList() ?? [],
443-
Templates = x.Templates?.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?.ToList() ?? [],
444-
Functions = x.Functions?.Select(f => FunctionDefMongoElement.ToMongoElement(f))?.ToList() ?? [],
445-
Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [],
446-
Samples = x.Samples ?? new List<string>(),
447-
Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [],
442+
Samples = x.Samples ?? [],
448443
IsPublic = x.IsPublic,
449444
Type = x.Type,
450445
InheritAgentId = x.InheritAgentId,
451446
Disabled = x.Disabled,
452447
MergeUtility = x.MergeUtility,
453448
MaxMessageCount = x.MaxMessageCount,
454-
Profiles = x.Profiles,
455-
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
449+
Profiles = x.Profiles ?? [],
456450
LlmConfig = AgentLlmConfigMongoElement.ToMongoElement(x.LlmConfig),
451+
ChannelInstructions = x.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?.ToList() ?? [],
452+
Templates = x.Templates?.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?.ToList() ?? [],
453+
Functions = x.Functions?.Select(f => FunctionDefMongoElement.ToMongoElement(f))?.ToList() ?? [],
454+
Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [],
455+
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
456+
Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [],
457+
KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [],
457458
CreatedTime = x.CreatedDateTime,
458459
UpdatedTime = x.UpdatedDateTime
459460
}).ToList();
@@ -530,21 +531,22 @@ private Agent TransformAgentDocument(AgentDocument? agentDoc)
530531
IconUrl = agentDoc.IconUrl,
531532
Description = agentDoc.Description,
532533
Instruction = agentDoc.Instruction,
533-
ChannelInstructions = agentDoc.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))?.ToList() ?? [],
534-
Templates = agentDoc.Templates?.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?.ToList() ?? [],
535-
Functions = agentDoc.Functions?.Select(f => FunctionDefMongoElement.ToDomainElement(f)).ToList() ?? [],
536-
Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [],
537-
RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [],
538-
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig),
539534
Samples = agentDoc.Samples ?? [],
540-
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
541535
IsPublic = agentDoc.IsPublic,
542536
Disabled = agentDoc.Disabled,
543537
MergeUtility = agentDoc.MergeUtility,
544538
Type = agentDoc.Type,
545539
InheritAgentId = agentDoc.InheritAgentId,
546540
Profiles = agentDoc.Profiles,
547-
MaxMessageCount = agentDoc.MaxMessageCount
541+
MaxMessageCount = agentDoc.MaxMessageCount,
542+
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig),
543+
ChannelInstructions = agentDoc.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))?.ToList() ?? [],
544+
Templates = agentDoc.Templates?.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?.ToList() ?? [],
545+
Functions = agentDoc.Functions?.Select(f => FunctionDefMongoElement.ToDomainElement(f)).ToList() ?? [],
546+
Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [],
547+
RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [],
548+
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
549+
KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? []
548550
};
549551
}
550552
}

0 commit comments

Comments
 (0)