diff --git a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj index c49d5625..f8f906a2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj +++ b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 diff --git a/src/Infrastructure/BotSharp.Abstraction/Knowledges/Models/KnowledgeFeedModel.cs b/src/Infrastructure/BotSharp.Abstraction/Knowledges/Models/KnowledgeFeedModel.cs index 75c69782..7e3a315e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Knowledges/Models/KnowledgeFeedModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Knowledges/Models/KnowledgeFeedModel.cs @@ -3,6 +3,5 @@ namespace BotSharp.Abstraction.Knowledges.Models; public class KnowledgeFeedModel { public string AgentId { get; set; } = string.Empty; - public string Name { get; set; } = string.Empty; public string Content { get; set; } = string.Empty; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IVectorDb.cs b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs similarity index 85% rename from src/Infrastructure/BotSharp.Abstraction/Knowledges/IVectorDb.cs rename to src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs index cf799979..ccd41e35 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Knowledges/IVectorDb.cs +++ b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Abstraction.Knowledges; +namespace BotSharp.Abstraction.VectorStorage; public interface IVectorDb { diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs index fbff072c..8bbf6e99 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs @@ -11,9 +11,11 @@ namespace BotSharp.Core.Agents; public class AgentController : ControllerBase, IApiAdapter { private readonly IAgentService _agentService; - public AgentController(IAgentService agentService) + private readonly IUserIdentity _user; + public AgentController(IAgentService agentService, IUserIdentity user) { _agentService = agentService; + _user = user; } [HttpPost("/agent")] @@ -23,6 +25,16 @@ public class AgentController : ControllerBase, IApiAdapter return AgentViewModel.FromAgent(createdAgent); } + [HttpPut("/agent/{agentId}")] + public async Task UpdateAgent([FromRoute] string agentId, + [FromBody] AgentUpdateModel agent) + { + var model = agent.ToAgent(); + model.Id = agentId; + model.OwerId = _user.Id; + await _agentService.UpdateAgent(model); + } + [HttpGet("/agents")] public async Task> GetAgents() { diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs index 7dc5d48b..ffc3ae04 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs @@ -11,6 +11,6 @@ public partial class AgentService : IChatServiceZone /// public async Task Serving(ContentContainer content) { - + } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index c3a0c423..42179abd 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -6,6 +6,17 @@ public partial class AgentService { public async Task UpdateAgent(Agent agent) { - throw new NotImplementedException(); + var db = _services.GetRequiredService(); + + db.Transaction(delegate + { + var record = db.Agent.FirstOrDefault(x => x.OwnerId == agent.OwerId && x.Id == agent.Id); + + record.Name = agent.Name; + record.Description = agent.Description; + record.Instruction = agent.Instruction; + record.Samples = agent.Samples; + record.UpdatedDateTime = DateTime.UtcNow; + }); } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs index 2f000c1f..0dc886e0 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs @@ -1,8 +1,30 @@ +using BotSharp.Abstraction.Agents.Models; + namespace BotSharp.Core.Agents.ViewModels; public class AgentUpdateModel { - public string Id { get; set; } public string Name { get; set; } public string Description { get; set; } + + /// + /// Instruction + /// + public string Instruction { get; set; } + + /// + /// Samples + /// + public string Samples { get; set; } + + public Agent ToAgent() + { + return new Agent + { + Name = Name, + Description = Description, + Instruction = Instruction, + Samples = Samples + }; + } } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 87eb5259..eeaa3b2d 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -67,7 +67,7 @@ - + diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index 40e994b1..2ae1a0c7 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -17,9 +17,6 @@ public static class BotSharpServiceCollectionExtensions services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); RegisterRepository(services, config); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs index 210751fa..714f3500 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs @@ -1,5 +1,3 @@ -using Microsoft.AspNetCore.Mvc; - namespace BotSharp.Core.Conversations.ViewModels; public class NewMessageModel diff --git a/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs b/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs deleted file mode 100644 index b1cdb8aa..00000000 --- a/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeBase.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace BotSharp.Core.Knowledges; - -public class KnowledgeBase : IVectorDb -{ - public Task CreateCollection(string collectionName, int dim) - { - throw new NotImplementedException(); - } - - public Task> GetCollections() - { - throw new NotImplementedException(); - } - - public Task> Search(string collectionName, float[] vector, int limit = 10) - { - throw new NotImplementedException(); - } - - public Task Upsert(string collectionName, int id, float[] vector) - { - throw new NotImplementedException(); - } -} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBasePlugin.cs b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBasePlugin.cs new file mode 100644 index 00000000..e0c74e83 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBasePlugin.cs @@ -0,0 +1,17 @@ +using BotSharp.Core.Plugins.Knowledges.Services; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Plugins.Knowledges; + +public class KnowledgeBasePlugin : IBotSharpPlugin +{ + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + var settings = new KnowledgeBaseSettings(); + config.Bind("KnowledgeBase", settings); + services.AddSingleton(x => settings); + + services.AddScoped(); + services.AddScoped(); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBaseSettings.cs b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBaseSettings.cs new file mode 100644 index 00000000..b2b3acdd --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeBaseSettings.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Core.Plugins.Knowledges; + +public class KnowledgeBaseSettings +{ + public string VectorDb { get; set; } + public string TextEmbedding { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeController.cs b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs similarity index 92% rename from src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeController.cs rename to src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs index dec3ef3e..5f613e5e 100644 --- a/src/Infrastructure/BotSharp.Core/Knowledges/KnowledgeController.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.ApiAdapters; -using BotSharp.Abstraction.Knowledges; using BotSharp.Abstraction.Knowledges.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; @@ -8,7 +7,7 @@ using System.IO; using UglyToad.PdfPig.Content; using UglyToad.PdfPig; -namespace BotSharp.Core.Knowledges; +namespace BotSharp.Core.Plugins.Knowledges; [Authorize] [ApiController] @@ -31,7 +30,7 @@ public class KnowledgeController : ControllerBase, IApiAdapter } [HttpPost("/knowledge/{agentId}")] - public async Task FeedKnowledge([FromRoute] string agentId, [FromForm] string name, List files) + public async Task FeedKnowledge([FromRoute] string agentId, List files) { long size = files.Sum(f => f.Length); @@ -62,7 +61,6 @@ public class KnowledgeController : ControllerBase, IApiAdapter await _knowledgeService.Feed(new KnowledgeFeedModel { AgentId = agentId, - Name = name, Content = content }); } diff --git a/src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.cs b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/KnowledgeService.cs similarity index 57% rename from src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.cs rename to src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/KnowledgeService.cs index d54a6e02..cf12ae62 100644 --- a/src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/KnowledgeService.cs @@ -1,25 +1,26 @@ using BotSharp.Abstraction.Knowledges.Models; using System.IO; using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.VectorStorage; -namespace BotSharp.Core.Knowledges.Services; +namespace BotSharp.Core.Plugins.Knowledges.Services; public class KnowledgeService : IKnowledgeService { - private readonly ITextEmbedding _textEmbedding; + private readonly IServiceProvider _services; + private readonly KnowledgeBaseSettings _settings; private readonly ITextCompletion _textCompletion; private readonly ITextChopper _textChopper; - private readonly IVectorDb _db; - - public KnowledgeService(ITextEmbedding textEmbedding, - ITextCompletion textCompletion, - ITextChopper textChopper, - IVectorDb db) + + public KnowledgeService(IServiceProvider services, + KnowledgeBaseSettings settings, + ITextCompletion textCompletion, + ITextChopper textChopper) { - _textEmbedding = textEmbedding; + _services = services; + _settings = settings; _textCompletion = textCompletion; _textChopper = textChopper; - _db = db; } public async Task Feed(KnowledgeFeedModel knowledge) @@ -33,25 +34,30 @@ public class KnowledgeService : IKnowledgeService // Store chunks in local file system var knowledgeStoreDir = Path.Combine("knowledge_chunks", knowledge.AgentId); - if(!Directory.Exists(knowledgeStoreDir)) + if (!Directory.Exists(knowledgeStoreDir)) { Directory.CreateDirectory(knowledgeStoreDir); } - var knowledgePath = Path.Combine(knowledgeStoreDir, knowledge.Name); + var knowledgePath = Path.Combine(knowledgeStoreDir, "chuncks"); File.WriteAllLines(knowledgePath + ".txt", lines); - await _db.CreateCollection(knowledge.Name, _textEmbedding.Dimension); + var db = GetVectorDb(); + var textEmbedding = GetTextEmbedding(); + + await db.CreateCollection(knowledge.AgentId, textEmbedding.Dimension); foreach (var line in lines) { - await _db.Upsert(knowledge.Name, idStart, _textEmbedding.GetVector(line)); + var vec = textEmbedding.GetVector(line); + await db.Upsert(knowledge.AgentId, idStart, vec); idStart++; } } public async Task GetAnswer(KnowledgeRetrievalModel retrievalModel) { - var vector = _textEmbedding.GetVector(retrievalModel.Question); + var textEmbedding = GetTextEmbedding(); + var vector = textEmbedding.GetVector(retrievalModel.Question); // Scan local knowledge directory var knowledgeName = ""; @@ -64,7 +70,7 @@ public class KnowledgeService : IKnowledgeService } // Vector search - var result = await _db.Search(knowledgeName, vector); + var result = await GetVectorDb().Search(knowledgeName, vector); // Restore var prompt = ""; @@ -80,4 +86,18 @@ public class KnowledgeService : IKnowledgeService var completion = await _textCompletion.GetCompletion(prompt); return completion; } + + public IVectorDb GetVectorDb() + { + var db = _services.GetServices() + .FirstOrDefault(x => x.GetType().Name == _settings.VectorDb); + return db; + } + + public ITextEmbedding GetTextEmbedding() + { + var embedding = _services.GetServices() + .FirstOrDefault(x => x.GetType().Name == _settings.TextEmbedding); + return embedding; + } } diff --git a/src/Infrastructure/BotSharp.Core/Knowledges/Services/TextChopperService.cs b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/TextChopperService.cs similarity index 87% rename from src/Infrastructure/BotSharp.Core/Knowledges/Services/TextChopperService.cs rename to src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/TextChopperService.cs index c803f988..44554136 100644 --- a/src/Infrastructure/BotSharp.Core/Knowledges/Services/TextChopperService.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/Services/TextChopperService.cs @@ -1,6 +1,6 @@ using BotSharp.Abstraction.Knowledges.Models; -namespace BotSharp.Core.Knowledges.Services; +namespace BotSharp.Core.Plugins.Knowledges.Services; public class TextChopperService : ITextChopper { @@ -10,8 +10,8 @@ public class TextChopperService : ITextChopper var currentPos = 0; while (currentPos < content.Length) { - var len = content.Length - currentPos > option.Size ? - option.Size : + var len = content.Length - currentPos > option.Size ? + option.Size : content.Length - currentPos; var chunk = content.Substring(currentPos, len); chunks.Add(chunk); diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs index 190737c4..48a853a0 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs @@ -1,7 +1,7 @@ using LLama; using System.IO; -namespace BotSharp.Plugins.LLamaSharp; +namespace BotSharp.Core.Plugins.LLamaSharp; public class ChatCompletionProvider : IChatServiceZone { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LLamaSharpPlugin.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LLamaSharpPlugin.cs index 0641f449..b01f913d 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LLamaSharpPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LLamaSharpPlugin.cs @@ -1,6 +1,6 @@ using Microsoft.Extensions.Configuration; -namespace BotSharp.Plugins.LLamaSharp; +namespace BotSharp.Core.Plugins.LLamaSharp; public class LLamaSharpPlugin : IBotSharpPlugin { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LlamaSharpSettings.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LlamaSharpSettings.cs index eae4e4a8..26f51cff 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LlamaSharpSettings.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/LlamaSharpSettings.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Plugins.LLamaSharp; +namespace BotSharp.Core.Plugins.LLamaSharp; public class LlamaSharpSettings { diff --git a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVecDbPlugin.cs b/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVecDbPlugin.cs new file mode 100644 index 00000000..01110674 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVecDbPlugin.cs @@ -0,0 +1,12 @@ +using BotSharp.Abstraction.VectorStorage; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Plugins.MemVecDb; + +public class MemVecDbPlugin : IBotSharpPlugin +{ + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddSingleton(); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVectorDatabase.cs b/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVectorDatabase.cs new file mode 100644 index 00000000..be09c97d --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/MemVectorDatabase.cs @@ -0,0 +1,36 @@ +using BotSharp.Abstraction.VectorStorage; + +namespace BotSharp.Core.Plugins.MemVecDb; + +public class MemVectorDatabase : IVectorDb +{ + private readonly Dictionary _collections = new Dictionary(); + private readonly Dictionary> _vectors = new Dictionary>(); + public Task CreateCollection(string collectionName, int dim) + { + _collections[collectionName] = dim; + _vectors[collectionName] = new List(); + return Task.CompletedTask; + } + + public Task> GetCollections() + { + return Task.FromResult(_collections.Select(x => x.Key).ToList()); + } + + public Task> Search(string collectionName, float[] vector, int limit = 10) + { + throw new NotImplementedException(); + } + + public Task Upsert(string collectionName, int id, float[] vector) + { + _vectors[collectionName].Add(new VecRecord + { + Id = id, + Vector = vector + }); + + return Task.CompletedTask; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/VecRecord.cs b/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/VecRecord.cs new file mode 100644 index 00000000..b5938c4d --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Plugins/MemVecDb/VecRecord.cs @@ -0,0 +1,8 @@ +namespace BotSharp.Core.Plugins.MemVecDb; + +public class VecRecord +{ + public int Id { get; set; } + public float[] Vector { get; set; } + public string Text { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs b/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs index 11cfbfa7..c44fdc3c 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs @@ -11,13 +11,25 @@ public class AgentRecord : DbRecord, IAgentTable [MaxLength(64)] public string Name { get; set; } = string.Empty; - [MaxLength(512)] - public string? Description { get; set; } - [Required] [MaxLength(36)] public string OwnerId { get; set; } = string.Empty; + [MaxLength(512)] + public string Description { get; set; } + + /// + /// Instruction + /// + [StringLength(int.MaxValue)] + public string Instruction { get; set; } + + /// + /// Samples + /// + [StringLength(int.MaxValue)] + public string Samples { get; set; } + [Required] public DateTime CreatedDateTime { get; set; } diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs index b1a8a4a4..b5929e45 100644 --- a/src/Infrastructure/BotSharp.Core/Using.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -19,6 +19,5 @@ global using BotSharp.Core.Repository.DbTables; global using BotSharp.Core.Agents.Services; global using BotSharp.Core.Conversations.Services; global using BotSharp.Core.Infrastructures; -global using BotSharp.Core.Knowledges.Services; global using BotSharp.Core.Plugins; global using BotSharp.Core.Users.Services; \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs index 4eea23e4..43419eaa 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs @@ -19,6 +19,6 @@ public class AzureOpenAiPlugin : IBotSharpPlugin services.AddSingleton(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); } } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatService.cs similarity index 85% rename from src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs rename to src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatService.cs index edd28e7c..71f57a08 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatService.cs @@ -7,11 +7,11 @@ using System.Threading.Tasks; namespace BotSharp.Plugin.AzureOpenAI.Services; -public class ChatCompletionService : IChatServiceZone +public class ChatService : IChatServiceZone { private readonly IChatCompletion _chatCompletion; - public ChatCompletionService(IChatCompletion chatCompletion) + public ChatService(IChatCompletion chatCompletion) { _chatCompletion = chatCompletion; } diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs b/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs index e7437837..f9516687 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs @@ -1,6 +1,6 @@ -using BotSharp.Abstraction.Knowledges; using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.VectorStorage; using BotSharp.Plugin.MetaAI.Providers; using BotSharp.Plugin.MetaAI.Settings; using Microsoft.Extensions.Configuration; diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/FaissDb.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/FaissDb.cs index 54e02882..1a22e11c 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/FaissDb.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/FaissDb.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.Knowledges; +using BotSharp.Abstraction.VectorStorage; using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs index cd5ef067..6ca628da 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs @@ -10,7 +10,17 @@ public class fastTextEmbeddingProvider : ITextEmbedding private FastTextWrapper _fastText; private readonly fastTextSetting _settings; - public int Dimension => _fastText.GetModelDimension(); + public int Dimension + { + get + { + if (!_fastText.IsModelReady()) + { + _fastText.LoadModel(_settings.ModelPath); + } + return _fastText.GetModelDimension(); + } + } public fastTextEmbeddingProvider(fastTextSetting settings) { @@ -21,15 +31,15 @@ public class fastTextEmbeddingProvider : ITextEmbedding { throw new FileNotFoundException($"Can't load pre-trained word vectors from {settings.ModelPath}.\n Try to download from https://fasttext.cc/docs/en/english-vectors.html."); } - - if (!_fastText.IsModelReady()) - { - _fastText.LoadModel(_settings.ModelPath); - } } public float[] GetVector(string text) { + if (!_fastText.IsModelReady()) + { + _fastText.LoadModel(_settings.ModelPath); + } + return _fastText.GetSentenceVector(text); } } diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs index 627aab8d..97a46e28 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.Knowledges; +using BotSharp.Abstraction.VectorStorage; using QdrantCSharp; using QdrantCSharp.Enums; using QdrantCSharp.Models; diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs index 6d372215..faa0e5e5 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantPlugin.cs @@ -1,5 +1,5 @@ -using BotSharp.Abstraction.Knowledges; using BotSharp.Abstraction.Plugins; +using BotSharp.Abstraction.VectorStorage; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index bd849f57..c7f705b8 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -67,6 +67,10 @@ "WeixinAppSecret": "#{WeixinAppSecret}#" }, + "KnowledgeBase": { + "VectorDb": "MemVecDbProvider" + }, + "PluginLoader": { "Assemblies": [ "BotSharp.Core", @@ -77,6 +81,8 @@ "BotSharp.Plugin.WeChat" ], "Plugins": [ + "KnowledgeBasePlugin", + "MemVecDbPlugin", // "LLamaSharpPlugin", "AzureOpenAiPlugin", "MetaAiPlugin",