From bf9bb2b6d31d49006edf00135e44020432e33179 Mon Sep 17 00:00:00 2001 From: hchen Date: Thu, 19 Oct 2023 12:24:00 -0500 Subject: [PATCH] Allow agent to use few-shot learning. --- .../BotSharp.Abstraction/Agents/AgentHookBase.cs | 2 +- .../BotSharp.Abstraction/Agents/IAgentHook.cs | 2 +- .../BotSharp.Abstraction/Agents/Models/Agent.cs | 8 +++++++- .../BotSharp.Abstraction/Routing/IRoutingService.cs | 1 + .../Agents/Services/AgentService.LoadAgent.cs | 5 ++--- .../Services/ConversationService.SendMessage.cs | 2 ++ .../BotSharp.Core/Repository/FileRepository.cs | 10 ++++++++++ .../BotSharp.Core/Routing/RoutingService.cs | 5 +++++ .../Providers/ProviderHelper.cs | 9 ++------- 9 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs index 4d3a7779..715bfde5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs @@ -41,7 +41,7 @@ public abstract class AgentHookBase : IAgentHook return true; } - public virtual bool OnSamplesLoaded(ref string samples) + public virtual bool OnSamplesLoaded(List samples) { _agent.Samples = samples; return true; diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs index 047a0b03..f7b7379b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs @@ -19,7 +19,7 @@ public interface IAgentHook bool OnFunctionsLoaded(List functions); - bool OnSamplesLoaded(ref string samples); + bool OnSamplesLoaded(List samples); /// /// Triggered when agent is loaded completely. diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index ef264659..42710520 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -27,7 +27,7 @@ public class Agent /// Samples /// [JsonIgnore] - public string Samples { get; set; } + public List Samples { get; set; } /// /// Functions @@ -109,6 +109,12 @@ public class Agent return this; } + public Agent SetSamples(List samples) + { + Samples = samples ?? new List(); + return this; + } + public Agent SetResponses(List responses) { Responses = responses ?? new List(); ; diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs index 0881b1d3..8b4ebaf9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs @@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Routing; public interface IRoutingService { List Dialogs { get; } + void RefreshDialogs(); Task GetNextInstruction(); Task InvokeAgent(string agentId); Task InstructLoop(); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 47cfcd2b..f5f54a7e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -42,10 +42,9 @@ public partial class AgentService hook.OnFunctionsLoaded(agent.Functions); } - if (!string.IsNullOrEmpty(agent.Samples)) + if (agent.Samples != null) { - var samples = agent.Samples; - hook.OnSamplesLoaded(ref samples); + hook.OnSamplesLoaded(agent.Samples); } hook.OnAgentLoaded(agent); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index da44f034..58f71bd0 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -56,6 +56,8 @@ public partial class ConversationService var statistics = _services.GetRequiredService(); statistics.PrintStatistics(); + routing.RefreshDialogs(); + return true; } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 62328343..b585b764 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -519,10 +519,12 @@ public class FileRepository : IBotSharpRepository var instruction = FetchInstruction(dir); var functions = FetchFunctions(dir); + var samples = FetchSamples(dir); var templates = FetchTemplates(dir); var responses = FetchResponses(dir); return record.SetInstruction(instruction) .SetFunctions(functions) + .SetSamples(samples) .SetTemplates(templates) .SetResponses(responses); } @@ -771,6 +773,14 @@ public class FileRepository : IBotSharpRepository return functions; } + private List FetchSamples(string fileDir) + { + var file = Path.Combine(fileDir, "samples.txt"); + if (!File.Exists(file)) return new List(); + + return File.ReadAllLines(file).ToList(); + } + private List FetchTemplates(string fileDir) { var templates = new List(); diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs index 5c1b3f1d..e10239a8 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs @@ -25,6 +25,11 @@ public partial class RoutingService : IRoutingService } } + public void RefreshDialogs() + { + _dialogs = null; + } + public RoutingService(IServiceProvider services, RoutingSettings settings, ILogger logger, diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs index b829330f..dffc196d 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ProviderHelper.cs @@ -23,16 +23,11 @@ public class ProviderHelper } } - public static List GetChatSamples(string sampleText) + public static List GetChatSamples(List lines) { var samples = new List(); - if (string.IsNullOrEmpty(sampleText)) - { - return samples; - } - var lines = sampleText.Split('\n'); - for (int i = 0; i < lines.Length; i++) + for (int i = 0; i < lines.Count; i++) { var line = lines[i]; if (string.IsNullOrEmpty(line.Trim()))