Merge pull request #140 from iceljc/features/add-mongo-implementation
Features/add mongo implementation
This commit is contained in:
commit
65e8554b6e
|
|
@ -1,3 +1,5 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
public class Agent
|
||||
|
|
@ -11,26 +13,37 @@ public class Agent
|
|||
/// <summary>
|
||||
/// Instruction
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string Instruction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Templates
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public List<AgentTemplate> Templates { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Samples
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string Samples { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Functions
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public List<string> Functions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Responses
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public List<AgentResponse> Responses { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Domain knowledges
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string Knowledges { get; set; }
|
||||
|
||||
public bool IsPublic { get; set; }
|
||||
|
|
@ -63,6 +76,12 @@ public class Agent
|
|||
return this;
|
||||
}
|
||||
|
||||
public Agent SetTemplates(List<AgentTemplate> templates)
|
||||
{
|
||||
Templates = templates;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Agent SetFunctions(List<string> functions)
|
||||
{
|
||||
Functions = functions ?? new List<string>();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
namespace BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
public class AgentTemplate
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Content { get; set; }
|
||||
|
||||
public AgentTemplate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AgentTemplate(string name, string content)
|
||||
{
|
||||
Name = name;
|
||||
Content = content;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ public class BotSharpDatabaseSettings : DatabaseBasicSettings
|
|||
public string[] Assemblies { get; set; }
|
||||
public string FileRepository { get; set; }
|
||||
public string MongoDb { get; set; }
|
||||
public string TablePrefix { get; set; }
|
||||
public DbConnectionSetting BotSharp { get; set; }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,4 +36,6 @@ public interface IBotSharpRepository
|
|||
|
||||
Conversation GetConversation(string conversationId);
|
||||
List<Conversation> GetConversations(string userId);
|
||||
|
||||
string GetAgentTemplate(string agentId, string templateName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ public partial class AgentService
|
|||
.SetDescription(foundAgent.Description)
|
||||
.SetIsPublic(foundAgent.IsPublic)
|
||||
.SetInstruction(foundAgent.Instruction)
|
||||
.SetTemplates(foundAgent.Templates)
|
||||
.SetFunctions(foundAgent.Functions)
|
||||
.SetResponses(foundAgent.Responses);
|
||||
}
|
||||
|
|
@ -79,7 +80,9 @@ public partial class AgentService
|
|||
var functions = FetchFunctionsFromFile(dir);
|
||||
var instruction = FetchInstructionFromFile(dir);
|
||||
var responses = FetchResponsesFromFile(dir);
|
||||
var templates = FetchTemplatesFromFile(dir);
|
||||
return agent.SetInstruction(instruction)
|
||||
.SetTemplates(templates)
|
||||
.SetFunctions(functions)
|
||||
.SetResponses(responses);
|
||||
}
|
||||
|
|
@ -97,6 +100,25 @@ public partial class AgentService
|
|||
return instruction;
|
||||
}
|
||||
|
||||
private List<AgentTemplate> FetchTemplatesFromFile(string fileDir)
|
||||
{
|
||||
var templates = new List<AgentTemplate>();
|
||||
foreach (var file in Directory.GetFiles(fileDir))
|
||||
{
|
||||
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
|
||||
var splits = fileName.ToLower().Split('.');
|
||||
var name = splits[0];
|
||||
var extension = splits[1];
|
||||
if (name != "instruction" && extension == "liquid")
|
||||
{
|
||||
var content = File.ReadAllText(file);
|
||||
templates.Add(new AgentTemplate(name, content));
|
||||
}
|
||||
}
|
||||
|
||||
return templates;
|
||||
}
|
||||
|
||||
private List<string> FetchFunctionsFromFile(string fileDir)
|
||||
{
|
||||
var file = Path.Combine(fileDir, "functions.json");
|
||||
|
|
|
|||
|
|
@ -151,4 +151,9 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string GetAgentTemplate(string agentId, string templateName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef;
|
|||
using BotSharp.Abstraction.Users.Models;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.Core.Repository;
|
||||
|
||||
public class FileRepository : IBotSharpRepository
|
||||
|
|
@ -158,7 +157,7 @@ public class FileRepository : IBotSharpRepository
|
|||
var filePath = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, "routing-profile.json");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
_routingProfiles = JsonSerializer.Deserialize<List<RoutingProfile>>(File.ReadAllText(filePath).ToLower(), _options);
|
||||
_routingProfiles = JsonSerializer.Deserialize<List<RoutingProfile>>(File.ReadAllText(filePath), _options);
|
||||
}
|
||||
|
||||
return _routingProfiles.AsQueryable();
|
||||
|
|
@ -560,4 +559,25 @@ public class FileRepository : IBotSharpRepository
|
|||
|
||||
return records;
|
||||
}
|
||||
|
||||
public string GetAgentTemplate(string agentId, string templateName)
|
||||
{
|
||||
var fileDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId);
|
||||
if (!Directory.Exists(fileDir)) return string.Empty;
|
||||
|
||||
var lowerTemplateName = templateName?.ToLower();
|
||||
foreach (var file in Directory.GetFiles(fileDir))
|
||||
{
|
||||
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
|
||||
var splits = fileName.ToLower().Split('.');
|
||||
var name = splits[0];
|
||||
var extension = splits[1];
|
||||
if (name == lowerTemplateName && extension == "liquid")
|
||||
{
|
||||
return File.ReadAllText(file);
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ public class AgentCreationModel
|
|||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Instruction { get; set; }
|
||||
public List<AgentTemplate> Templates { get; set; }
|
||||
public List<string> Functions { get; set; }
|
||||
public List<AgentResponse> Responses { get; set; }
|
||||
public bool IsPublic { get; set; }
|
||||
|
|
@ -18,6 +19,7 @@ public class AgentCreationModel
|
|||
Name = Name,
|
||||
Description = Description,
|
||||
Instruction = Instruction,
|
||||
Templates = Templates,
|
||||
Functions = Functions,
|
||||
Responses = Responses,
|
||||
IsPublic = IsPublic
|
||||
|
|
|
|||
|
|
@ -12,6 +12,11 @@ public class AgentUpdateModel
|
|||
/// </summary>
|
||||
public string? Instruction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Templates
|
||||
/// </summary>
|
||||
public List<AgentTemplate>? Templates { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Samples
|
||||
/// </summary>
|
||||
|
|
@ -20,12 +25,12 @@ public class AgentUpdateModel
|
|||
/// <summary>
|
||||
/// Functions
|
||||
/// </summary>
|
||||
public List<string> Functions { get; set; }
|
||||
public List<string>? Functions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Routes
|
||||
/// </summary>
|
||||
public List<AgentResponse> Responses { get; set; }
|
||||
public List<AgentResponse>? Responses { get; set; }
|
||||
|
||||
public Agent ToAgent()
|
||||
{
|
||||
|
|
@ -40,6 +45,9 @@ public class AgentUpdateModel
|
|||
if (Instruction != null)
|
||||
agent.Instruction = Instruction;
|
||||
|
||||
if (!Templates.IsNullOrEmpty())
|
||||
agent.Templates = Templates;
|
||||
|
||||
if (Samples != null)
|
||||
agent.Samples = Samples;
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ public class AgentViewModel
|
|||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Instruction { get; set; }
|
||||
public List<AgentTemplate> Templates { get; set; }
|
||||
public List<string> Functions { get; set; }
|
||||
public List<AgentResponse> Responses { get; set; }
|
||||
public bool IsPublic { get; set; }
|
||||
|
|
@ -21,6 +22,7 @@ public class AgentViewModel
|
|||
Name = agent.Name,
|
||||
Description = agent.Description,
|
||||
Instruction = agent.Instruction,
|
||||
Templates = agent.Templates,
|
||||
Functions = agent.Functions,
|
||||
Responses = agent.Responses,
|
||||
IsPublic= agent.IsPublic,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ public class AgentCollection : MongoBase
|
|||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Instruction { get; set; }
|
||||
public List<AgentTemplate> Templates { get; set; }
|
||||
public List<string> Functions { get; set; }
|
||||
public List<AgentResponse> Responses { get; set; }
|
||||
public bool IsPublic { get; set; }
|
||||
|
|
|
|||
|
|
@ -6,11 +6,14 @@ public class MongoDbContext
|
|||
{
|
||||
private readonly MongoClient _mongoClient;
|
||||
private readonly string _mongoDbDatabaseName;
|
||||
private readonly string _collectionPrefix;
|
||||
|
||||
public MongoDbContext(string mongoDbConnectionString)
|
||||
public MongoDbContext(BotSharpDatabaseSettings dbSettings)
|
||||
{
|
||||
var mongoDbConnectionString = dbSettings.MongoDb;
|
||||
_mongoClient = new MongoClient(mongoDbConnectionString);
|
||||
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
|
||||
_collectionPrefix = dbSettings.TablePrefix;
|
||||
}
|
||||
|
||||
private string GetDatabaseName(string mongoDbConnectionString)
|
||||
|
|
@ -26,23 +29,23 @@ public class MongoDbContext
|
|||
private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }
|
||||
|
||||
public IMongoCollection<AgentCollection> Agents
|
||||
=> Database.GetCollection<AgentCollection>("OneBrainAgents");
|
||||
=> Database.GetCollection<AgentCollection>($"{_collectionPrefix}_Agents");
|
||||
|
||||
public IMongoCollection<ConversationCollection> Conversations
|
||||
=> Database.GetCollection<ConversationCollection>("OneBrainConversations");
|
||||
=> Database.GetCollection<ConversationCollection>($"{_collectionPrefix}_Conversations");
|
||||
|
||||
public IMongoCollection<ConversationDialogCollection> ConversationDialogs
|
||||
=> Database.GetCollection<ConversationDialogCollection>("OneBrainConversationDialogs");
|
||||
=> Database.GetCollection<ConversationDialogCollection>($"{_collectionPrefix}_ConversationDialogs");
|
||||
|
||||
public IMongoCollection<UserCollection> Users
|
||||
=> Database.GetCollection<UserCollection>("OneBrainUsers");
|
||||
=> Database.GetCollection<UserCollection>($"{_collectionPrefix}_Users");
|
||||
|
||||
public IMongoCollection<UserAgentCollection> UserAgents
|
||||
=> Database.GetCollection<UserAgentCollection>("OneBrainUserAgents");
|
||||
=> Database.GetCollection<UserAgentCollection>($"{_collectionPrefix}_UserAgents");
|
||||
|
||||
public IMongoCollection<RoutingItemCollection> RoutingItems
|
||||
=> Database.GetCollection<RoutingItemCollection>("OneBrainRoutingItems");
|
||||
=> Database.GetCollection<RoutingItemCollection>($"{_collectionPrefix}_RoutingItems");
|
||||
|
||||
public IMongoCollection<RoutingProfileCollection> RoutingProfiles
|
||||
=> Database.GetCollection<RoutingProfileCollection>("OneBrainRoutingProfiles");
|
||||
=> Database.GetCollection<RoutingProfileCollection>($"{_collectionPrefix}_RoutingProfiles");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ public class MongoStoragePlugin : IBotSharpPlugin
|
|||
services.AddScoped((IServiceProvider x) =>
|
||||
{
|
||||
var dbSettings = x.GetRequiredService<BotSharpDatabaseSettings>();
|
||||
return new MongoDbContext(dbSettings.MongoDb);
|
||||
return new MongoDbContext(dbSettings);
|
||||
});
|
||||
|
||||
services.AddScoped<IBotSharpRepository, MongoRepository>();
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ public class MongoRepository : IBotSharpRepository
|
|||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Instruction = x.Instruction,
|
||||
Templates = x.Templates,
|
||||
Functions = x.Functions,
|
||||
Responses = x.Responses,
|
||||
IsPublic = x.IsPublic,
|
||||
|
|
@ -250,6 +251,7 @@ public class MongoRepository : IBotSharpRepository
|
|||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
Instruction = x.Instruction,
|
||||
Templates = x.Templates,
|
||||
Functions = x.Functions,
|
||||
Responses = x.Responses,
|
||||
IsPublic = x.IsPublic,
|
||||
|
|
@ -264,6 +266,7 @@ public class MongoRepository : IBotSharpRepository
|
|||
.Set(x => x.Name, agent.Name)
|
||||
.Set(x => x.Description, agent.Description)
|
||||
.Set(x => x.Instruction, agent.Instruction)
|
||||
.Set(x => x.Templates, agent.Templates)
|
||||
.Set(x => x.Functions, agent.Functions)
|
||||
.Set(x => x.Responses, agent.Responses)
|
||||
.Set(x => x.IsPublic, agent.IsPublic)
|
||||
|
|
@ -596,4 +599,12 @@ public class MongoRepository : IBotSharpRepository
|
|||
|
||||
return records;
|
||||
}
|
||||
|
||||
public string GetAgentTemplate(string agentId, string templateName)
|
||||
{
|
||||
var agent = Agents.FirstOrDefault(x => x.Id == agentId);
|
||||
if (agent == null) return string.Empty;
|
||||
|
||||
return agent.Templates?.FirstOrDefault(x => x.Name == templateName.ToLower())?.Content ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@
|
|||
|
||||
"Database": {
|
||||
"Default": "FileRepository",
|
||||
"TablePrefix": "BotSharp",
|
||||
"MongoDb": "",
|
||||
"BotSharp": {
|
||||
"Master": "Data Source=(localdb)\\ProjectModels;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False",
|
||||
|
|
|
|||
Loading…
Reference in a new issue