Merge pull request #809 from iceljc/master

refine agent knowledge base
This commit is contained in:
iceljc 2024-12-29 21:50:13 -06:00 committed by GitHub
commit a13f925916
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 82 additions and 44 deletions

View file

@ -155,6 +155,7 @@ public class Agent
Profiles = agent.Profiles,
RoutingRules = agent.RoutingRules,
LlmConfig = agent.LlmConfig,
KnowledgeBases = agent.KnowledgeBases,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime,
};
@ -168,43 +169,49 @@ public class Agent
public Agent SetChannelInstructions(List<ChannelInstruction> instructions)
{
ChannelInstructions = instructions ?? new List<ChannelInstruction>();
ChannelInstructions = instructions ?? [];
return this;
}
public Agent SetTemplates(List<AgentTemplate> templates)
{
Templates = templates ?? new List<AgentTemplate>();
Templates = templates ?? [];
return this;
}
public Agent SetTasks(List<AgentTask> tasks)
{
Tasks = tasks ?? new List<AgentTask>();
Tasks = tasks ?? [];
return this;
}
public Agent SetFunctions(List<FunctionDef> functions)
{
Functions = functions ?? new List<FunctionDef>();
Functions = functions ?? [];
return this;
}
public Agent SetSamples(List<string> samples)
{
Samples = samples ?? new List<string>();
Samples = samples ?? [];
return this;
}
public Agent SetUtilities(List<AgentUtility> utilities)
{
Utilities = utilities ?? new List<AgentUtility>();
Utilities = utilities ?? [];
return this;
}
public Agent SetKnowledgeBases(List<AgentKnowledgeBase> knowledgeBases)
{
knowledgeBases = knowledgeBases ?? [];
return this;
}
public Agent SetResponses(List<AgentResponse> responses)
{
Responses = responses ?? new List<AgentResponse>(); ;
Responses = responses ?? [];
return this;
}
@ -252,13 +259,13 @@ public class Agent
public Agent SetProfiles(List<string> profiles)
{
Profiles = profiles ?? new List<string>();
Profiles = profiles ?? [];
return this;
}
public Agent SetRoutingRules(List<RoutingRule> rules)
{
RoutingRules = rules ?? new List<RoutingRule>();
RoutingRules = rules ?? [];
return this;
}

View file

@ -2,7 +2,8 @@ namespace BotSharp.Abstraction.Agents.Models;
public class AgentKnowledgeBase
{
public string? Name { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public bool Disabled { get; set; }
public AgentKnowledgeBase()
@ -10,9 +11,10 @@ public class AgentKnowledgeBase
}
public AgentKnowledgeBase(string name, bool enabled)
public AgentKnowledgeBase(string name, string type, bool enabled)
{
Name = name;
Type = type;
Disabled = enabled;
}

View file

@ -9,7 +9,7 @@ public interface IKnowledgeService
Task<bool> ExistVectorCollection(string collectionName);
Task<bool> CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model);
Task<bool> DeleteVectorCollection(string collectionName);
Task<IEnumerable<string>> GetVectorCollections(string type);
Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(string? type = null);
Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options);
Task<StringIdPagedItems<VectorSearchResult>> GetPagedVectorCollectionData(string collectionName, VectorFilter filter);
Task<bool> DeleteVectorCollectionData(string collectionName, string id);

View file

@ -103,6 +103,7 @@ public partial class AgentService
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples)
.SetUtilities(foundAgent.Utilities)
.SetKnowledgeBases(foundAgent.KnowledgeBases)
.SetLlmConfig(foundAgent.LlmConfig);
_db.UpdateAgent(clonedAgent, AgentField.All);

View file

@ -10,7 +10,10 @@ public class AgentController : ControllerBase
private readonly IUserIdentity _user;
private readonly IServiceProvider _services;
public AgentController(IAgentService agentService, IUserIdentity user, IServiceProvider services)
public AgentController(
IAgentService agentService,
IUserIdentity user,
IServiceProvider services)
{
_agentService = agentService;
_user = user;

View file

@ -26,9 +26,10 @@ public class KnowledgeBaseController : ControllerBase
}
[HttpGet("knowledge/vector/collections")]
public async Task<IEnumerable<string>> GetVectorCollections([FromQuery] string type)
public async Task<IEnumerable<VectorCollectionConfigViewModel>> GetVectorCollections([FromQuery] string? type = null)
{
return await _knowledgeService.GetVectorCollections(type);
var collections = await _knowledgeService.GetVectorCollections(type);
return collections.Select(x => VectorCollectionConfigViewModel.From(x));
}
[HttpPost("knowledge/vector/create-collection")]

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.OpenAPI.ViewModels.Agents;
@ -55,6 +54,7 @@ public class AgentCreationModel
public List<AgentUtility> Utilities { get; set; } = new();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
public List<AgentKnowledgeBase> KnowledgeBases { get; set; } = new();
public AgentLlmConfig? LlmConfig { get; set; }
public Agent ToAgent()
@ -76,8 +76,9 @@ public class AgentCreationModel
MergeUtility = MergeUtility,
MaxMessageCount = MaxMessageCount,
Profiles = Profiles,
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
LlmConfig = LlmConfig
LlmConfig = LlmConfig,
KnowledgeBases = KnowledgeBases,
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [],
};
}
}

View file

@ -0,0 +1,17 @@
using BotSharp.Abstraction.VectorStorage.Models;
namespace BotSharp.OpenAPI.ViewModels.Knowledges;
public class VectorCollectionConfigViewModel : VectorCollectionConfig
{
public static VectorCollectionConfigViewModel From(VectorCollectionConfig model)
{
return new VectorCollectionConfigViewModel
{
Name = model.Name,
Type = model.Type,
VectorStore = model.VectorStore,
TextEmbedding = model.TextEmbedding
};
}
}

View file

@ -69,25 +69,25 @@ public partial class KnowledgeService
}
}
public async Task<IEnumerable<string>> GetVectorCollections(string type)
public async Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(string? type = null)
{
try
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var collectionNames = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
{
CollectionTypes = new[] { type },
VectorStroageProviders = new[] { _settings.VectorDb.Provider }
}).Select(x => x.Name).ToList();
CollectionTypes = !string.IsNullOrEmpty(type) ? [type] : null,
VectorStroageProviders = [_settings.VectorDb.Provider]
}).ToList();
var vectorDb = GetVectorDb();
var vectorCollections = await vectorDb.GetCollections();
return vectorCollections.Where(x => collectionNames.Contains(x));
var dbCollections = await vectorDb.GetCollections();
return configs.Where(x => dbCollections.Contains(x.Name));
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting vector db collections. {ex.Message}\r\n{ex.InnerException}");
return Enumerable.Empty<string>();
return Enumerable.Empty<VectorCollectionConfig>();
}
}

View file

@ -5,13 +5,16 @@ namespace BotSharp.Plugin.MongoStorage.Models;
public class AgentKnowledgeBaseMongoElement
{
public string Name { get; set; }
public string Type { get; set; }
public bool Disabled { get; set; }
public static AgentKnowledgeBaseMongoElement ToMongoElement(AgentKnowledgeBase knowledgeBase)
{
return new AgentKnowledgeBaseMongoElement
{
Name = knowledgeBase.Name ?? string.Empty,
Disabled = knowledgeBase.Disabled,
Name = knowledgeBase.Name,
Type = knowledgeBase.Type,
Disabled = knowledgeBase.Disabled
};
}
@ -20,6 +23,7 @@ public class AgentKnowledgeBaseMongoElement
return new AgentKnowledgeBase
{
Name = knowledgeBase.Name,
Type = knowledgeBase.Type,
Disabled = knowledgeBase.Disabled
};
}

View file

@ -439,21 +439,22 @@ public partial class MongoRepository
IconUrl = x.IconUrl,
Description = x.Description,
Instruction = x.Instruction,
ChannelInstructions = x.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?.ToList() ?? [],
Templates = x.Templates?.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?.ToList() ?? [],
Functions = x.Functions?.Select(f => FunctionDefMongoElement.ToMongoElement(f))?.ToList() ?? [],
Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [],
Samples = x.Samples ?? new List<string>(),
Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [],
Samples = x.Samples ?? [],
IsPublic = x.IsPublic,
Type = x.Type,
InheritAgentId = x.InheritAgentId,
Disabled = x.Disabled,
MergeUtility = x.MergeUtility,
MaxMessageCount = x.MaxMessageCount,
Profiles = x.Profiles,
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
Profiles = x.Profiles ?? [],
LlmConfig = AgentLlmConfigMongoElement.ToMongoElement(x.LlmConfig),
ChannelInstructions = x.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToMongoElement(i))?.ToList() ?? [],
Templates = x.Templates?.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?.ToList() ?? [],
Functions = x.Functions?.Select(f => FunctionDefMongoElement.ToMongoElement(f))?.ToList() ?? [],
Responses = x.Responses?.Select(r => AgentResponseMongoElement.ToMongoElement(r))?.ToList() ?? [],
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [],
KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [],
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
}).ToList();
@ -530,21 +531,22 @@ public partial class MongoRepository
IconUrl = agentDoc.IconUrl,
Description = agentDoc.Description,
Instruction = agentDoc.Instruction,
ChannelInstructions = agentDoc.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))?.ToList() ?? [],
Templates = agentDoc.Templates?.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?.ToList() ?? [],
Functions = agentDoc.Functions?.Select(f => FunctionDefMongoElement.ToDomainElement(f)).ToList() ?? [],
Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [],
RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [],
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig),
Samples = agentDoc.Samples ?? [],
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
IsPublic = agentDoc.IsPublic,
Disabled = agentDoc.Disabled,
MergeUtility = agentDoc.MergeUtility,
Type = agentDoc.Type,
InheritAgentId = agentDoc.InheritAgentId,
Profiles = agentDoc.Profiles,
MaxMessageCount = agentDoc.MaxMessageCount
MaxMessageCount = agentDoc.MaxMessageCount,
LlmConfig = AgentLlmConfigMongoElement.ToDomainElement(agentDoc.LlmConfig),
ChannelInstructions = agentDoc.ChannelInstructions?.Select(i => ChannelInstructionMongoElement.ToDomainElement(i))?.ToList() ?? [],
Templates = agentDoc.Templates?.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?.ToList() ?? [],
Functions = agentDoc.Functions?.Select(f => FunctionDefMongoElement.ToDomainElement(f)).ToList() ?? [],
Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [],
RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [],
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? []
};
}
}