diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs deleted file mode 100644 index 6953dee9..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs +++ /dev/null @@ -1,46 +0,0 @@ -using BotSharp.Abstraction.Conversations.Models; - -namespace BotSharp.Abstraction.Agents; - -public abstract class AgentHookBase : IAgentHook -{ - protected Agent _agent; - public Agent Agent => _agent; - - public void SetAget(Agent agent) - { - _agent = agent; - } - - public virtual bool OnAgentLoading(ref string id) - { - return true; - } - - public virtual bool OnInstructionLoaded(ref string instruction) - { - _agent.Instruction = instruction; - return true; - } - - public virtual bool OnFunctionsLoaded(ref string functions) - { - _agent.Functions = functions; - return true; - } - - public virtual bool OnSamplesLoaded(ref string samples) - { - _agent.Samples = samples; - return true; - } - - public virtual void OnAgentLoaded(Agent agent) - { - } - - public virtual bool OnAgentRouting(RoleDialogModel message, ref string id) - { - return true; - } -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs index 7b0b73dd..f071caaf 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs @@ -13,7 +13,7 @@ public interface IAgentHook bool OnAgentLoading(ref string id); - bool OnInstructionLoaded(ref string instruction); + bool OnInstructionLoaded(string template, Dictionary dict); bool OnFunctionsLoaded(ref string functions); diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs index 6b092805..dfc10822 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs @@ -7,4 +7,5 @@ public class AgentSettings /// public string RouterId { get; set; } public string DataDir { get; set; } + public string TemplateFormat { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs new file mode 100644 index 00000000..88e3ec1f --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs @@ -0,0 +1,76 @@ +using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Conversations.Models; +using Fluid; +using Microsoft.AspNetCore.Mvc; + +namespace BotSharp.Core.Agents.Services; + +public abstract class AgentHookBase : IAgentHook +{ + protected Agent _agent; + public Agent Agent => _agent; + private static readonly FluidParser _parser = new FluidParser(); + + private readonly IServiceProvider _services; + + public AgentHookBase(IServiceProvider services) + { + _services = services; + } + + public void SetAget(Agent agent) + { + _agent = agent; + } + + public virtual bool OnAgentLoading(ref string id) + { + return true; + } + + public virtual bool OnInstructionLoaded(string template, Dictionary dict) + { + if (_parser.TryParse(template, out var t, out var error)) + { + PopulateStateTokens(dict); + var context = new TemplateContext(dict); + _agent.Instruction = t.Render(context); + return true; + } + else + { + return false; + } + } + + private void PopulateStateTokens(Dictionary dict) + { + var stateService = _services.GetRequiredService(); + var state = stateService.Load(); + foreach (var t in state) + { + dict[t.Key] = t.Value; + } + } + + public virtual bool OnFunctionsLoaded(ref string functions) + { + _agent.Functions = functions; + return true; + } + + public virtual bool OnSamplesLoaded(ref string samples) + { + _agent.Samples = samples; + return true; + } + + public virtual void OnAgentLoaded(Agent agent) + { + } + + public virtual bool OnAgentRouting(RoleDialogModel message, ref string id) + { + return true; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index dc9d417a..7bf27a44 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Agents.Models; -using Microsoft.Extensions.Logging; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -26,7 +25,7 @@ public partial class AgentService var profile = query.FirstOrDefault(); var dir = GetAgentDataDir(id); - var instructionFile = Path.Combine(dir, "instruction.txt"); + var instructionFile = Path.Combine(dir, $"instruction.{_settings.TemplateFormat}"); if (File.Exists(instructionFile)) { profile.Instruction = File.ReadAllText(instructionFile); @@ -36,7 +35,7 @@ public partial class AgentService _logger.LogError($"Can't find instruction file from {instructionFile}"); } - var samplesFile = Path.Combine(dir, "samples.txt"); + var samplesFile = Path.Combine(dir, $"samples.{_settings.TemplateFormat}"); if (File.Exists(samplesFile)) { profile.Samples = File.ReadAllText(samplesFile); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 09c463ee..f3b91e3e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -23,8 +23,7 @@ public partial class AgentService if (!string.IsNullOrEmpty(agent.Instruction)) { - var instruction = agent.Instruction; - hook.OnInstructionLoaded(ref instruction); + hook.OnInstructionLoaded(agent.Instruction, new Dictionary()); } if (!string.IsNullOrEmpty(agent.Functions)) diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 4f21e5b7..c3a188fd 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -75,6 +75,7 @@ + diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs index f1141a56..d6e1824d 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs @@ -26,26 +26,18 @@ public class fastTextEmbeddingProvider : ITextEmbedding public fastTextEmbeddingProvider(fastTextSetting settings) { _settings = settings; - _fastText = new FastTextWrapper(); - if (!File.Exists(settings.ModelPath)) - { - 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."); - } } public float[] GetVector(string text) { - if (!_fastText.IsModelReady()) - { - _fastText.LoadModel(_settings.ModelPath); - } - + LoadModel(); return _fastText.GetSentenceVector(text); } public List GetVectors(List texts) { + LoadModel(); var vectors = new List(); for (int i = 0; i < texts.Count; i++) { @@ -53,4 +45,22 @@ public class fastTextEmbeddingProvider : ITextEmbedding } return vectors; } + + private void LoadModel() + { + if (_fastText == null) + { + if (!File.Exists(_settings.ModelPath)) + { + 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."); + } + + _fastText = new FastTextWrapper(); + + if (!_fastText.IsModelReady()) + { + _fastText.LoadModel(_settings.ModelPath); + } + } + } } diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 61b34f0b..606ec0b2 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -14,7 +14,9 @@ }, "Agent": { - "DataDir": "agents" + "RouterId": "", + "DataDir": "agents", + "TemplateFormat": "liquid" }, "Conversation": {