Skip to content

refine agent knowledge base #809

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

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 16 additions & 9 deletions src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public static Agent Clone(Agent agent)
Profiles = agent.Profiles,
RoutingRules = agent.RoutingRules,
LlmConfig = agent.LlmConfig,
KnowledgeBases = agent.KnowledgeBases,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime,
};
Expand All @@ -168,43 +169,49 @@ public Agent SetInstruction(string instruction)

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;
}

Expand Down Expand Up @@ -252,13 +259,13 @@ public Agent SetAgentType(string type)

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ 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()
{

}

public AgentKnowledgeBase(string name, bool enabled)
public AgentKnowledgeBase(string name, string type, bool enabled)
{
Name = name;
Type = type;
Disabled = enabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public async Task<string> UpdateAgentFromFile(string id)
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples)
.SetUtilities(foundAgent.Utilities)
.SetKnowledgeBases(foundAgent.KnowledgeBases)
.SetLlmConfig(foundAgent.LlmConfig);

_db.UpdateAgent(clonedAgent, AgentField.All);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public async Task<bool> ExistVectorCollection([FromRoute] string collection)
}

[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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.OpenAPI.ViewModels.Agents;

Expand Down Expand Up @@ -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()
Expand All @@ -76,8 +76,9 @@ public Agent ToAgent()
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() ?? [],
};
}
}
Original file line number Diff line number Diff line change
@@ -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
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,25 @@ public async Task<bool> CreateVectorCollection(string collectionName, string col
}
}

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>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

Expand All @@ -20,6 +23,7 @@ public static AgentKnowledgeBase ToDomainElement(AgentKnowledgeBaseMongoElement
return new AgentKnowledgeBase
{
Name = knowledgeBase.Name,
Type = knowledgeBase.Type,
Disabled = knowledgeBase.Disabled
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,21 +439,22 @@ public void BulkInsertAgents(List<Agent> agents)
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();
Expand Down Expand Up @@ -530,21 +531,22 @@ private Agent TransformAgentDocument(AgentDocument? agentDoc)
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() ?? []
};
}
}