From 9393508a4680036362dccdfd29e7913a57c5eedc Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 30 Aug 2023 17:40:45 -0500 Subject: [PATCH] Add Pizza bot example. --- BotSharp.sln | 13 ++++++++++++- .../Routing/Models/RoutingRecord.cs | 2 +- src/WebStarter/WebStarter.csproj | 1 + src/WebStarter/appsettings.json | 11 +++++++---- .../BotSharp.Plugin.PizzaBot.csproj | 13 +++++++++++++ .../Functions/GetPizzaPricesFn.cs | 14 ++++++++++++++ .../Functions/GetPizzaTypesFn.cs | 14 ++++++++++++++ .../Functions/MakeOrderFn.cs | 14 ++++++++++++++ .../Functions/OrderFoundFn.cs | 14 ++++++++++++++ .../Hooks/PizzaBotAgentHook.cs | 17 +++++++++++++++++ .../PizzaBotPlugin.cs | 19 +++++++++++++++++++ tests/BotSharp.Plugin.PizzaBot/Using.cs | 12 ++++++++++++ 12 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj create mode 100644 tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs create mode 100644 tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaTypesFn.cs create mode 100644 tests/BotSharp.Plugin.PizzaBot/Functions/MakeOrderFn.cs create mode 100644 tests/BotSharp.Plugin.PizzaBot/Functions/OrderFoundFn.cs create mode 100644 tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs create mode 100644 tests/BotSharp.Plugin.PizzaBot/PizzaBotPlugin.cs create mode 100644 tests/BotSharp.Plugin.PizzaBot/Using.cs diff --git a/BotSharp.sln b/BotSharp.sln index ec114349..95a2b229 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -45,7 +45,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MetaMesseng EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.OpenAPI", "src\Infrastructure\BotSharp.OpenAPI\BotSharp.OpenAPI.csproj", "{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.LLamaSharp", "src\Plugins\BotSharp.Plugin.LLamaSharp\BotSharp.Plugin.LLamaSharp.csproj", "{46B7B54F-1425-4C9D-824A-9B826855D249}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.LLamaSharp", "src\Plugins\BotSharp.Plugin.LLamaSharp\BotSharp.Plugin.LLamaSharp.csproj", "{46B7B54F-1425-4C9D-824A-9B826855D249}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PizzaBot", "tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj", "{A1118A2C-C6D7-4E22-9462-964AEC7CC46E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -167,6 +169,14 @@ Global {46B7B54F-1425-4C9D-824A-9B826855D249}.Release|Any CPU.Build.0 = Release|Any CPU {46B7B54F-1425-4C9D-824A-9B826855D249}.Release|x64.ActiveCfg = Release|Any CPU {46B7B54F-1425-4C9D-824A-9B826855D249}.Release|x64.Build.0 = Release|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Debug|x64.ActiveCfg = Debug|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Debug|x64.Build.0 = Debug|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|Any CPU.Build.0 = Release|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|x64.ActiveCfg = Release|Any CPU + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -190,6 +200,7 @@ Global {8300F66D-9EB8-438A-BF0F-70DFBE07D9DE} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00} {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749} {46B7B54F-1425-4C9D-824A-9B826855D249} = {D5293208-2BEF-42FC-A64C-5954F61720BA} + {A1118A2C-C6D7-4E22-9462-964AEC7CC46E} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs index f0d6d742..73fd4d1e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRecord.cs @@ -14,7 +14,7 @@ public class RoutingRecord public string Description { get; set; } [JsonPropertyName("required")] - public List RequiredFields { get; set; } + public List RequiredFields { get; set; } = new List(); [JsonPropertyName("redirect_to")] public string RedirectTo { get; set; } diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index e8ef139f..311957bb 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -34,6 +34,7 @@ + diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index d111f6b1..41ef16d5 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -14,8 +14,8 @@ }, "Router": { - "RouterId": "", - "ReasonerId": "" + "RouterId": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a", + "ReasonerId": "03d3fb55-9ada-423b-a6b4-f9ecddf4b26e" }, "Agent": { @@ -26,6 +26,7 @@ "Conversation": { "DataDir": "conversations", + "ShowVerboseLog": false, "ChatCompletion": "AzureOpenAI.Providers.ChatCompletionProvider" // "ChatCompletion": "LLamaSharp.ChatCompletionProvider" }, @@ -103,7 +104,8 @@ "BotSharp.Plugin.MetaAI", "BotSharp.Plugin.Qdrant", "BotSharp.Plugin.PaddleSharp", - "BotSharp.Plugin.WeChat" + "BotSharp.Plugin.WeChat", + "BotSharp.Plugin.PizzaBot" ], "Plugins": [ "KnowledgeBasePlugin", @@ -113,7 +115,8 @@ "MetaAiPlugin", "QdrantPlugin", "PaddleSharpPlugin", - "WeChatPlugin" + "WeChatPlugin", + "PizzaBotPlugin" ] } } diff --git a/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj b/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj new file mode 100644 index 00000000..a451378c --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/BotSharp.Plugin.PizzaBot.csproj @@ -0,0 +1,13 @@ + + + + netstandard2.1 + enable + 10 + + + + + + + diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs new file mode 100644 index 00000000..cf1d3a0c --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaPricesFn.cs @@ -0,0 +1,14 @@ +using BotSharp.Abstraction.Conversations.Models; + +namespace BotSharp.Plugin.PizzaBot.Functions; + +public class GetPizzaPricesFn : IFunctionCallback +{ + public string Name => "get_pizza_price"; + + public async Task Execute(RoleDialogModel message) + { + message.ExecutionResult = "Pepperoni Pizza: $3.5/slice, Cheese Pizza: $2.5/slice, Margherita Pizza: $3.0/slice"; + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaTypesFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaTypesFn.cs new file mode 100644 index 00000000..0e505d09 --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/GetPizzaTypesFn.cs @@ -0,0 +1,14 @@ +using BotSharp.Abstraction.Conversations.Models; + +namespace BotSharp.Plugin.PizzaBot.Functions; + +public class GetPizzaTypesFn : IFunctionCallback +{ + public string Name => "get_pizza_types"; + + public async Task Execute(RoleDialogModel message) + { + message.ExecutionResult = "Pepperoni Pizza, Cheese Pizza, Margherita Pizza"; + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/MakeOrderFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/MakeOrderFn.cs new file mode 100644 index 00000000..68405157 --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/MakeOrderFn.cs @@ -0,0 +1,14 @@ +using BotSharp.Abstraction.Conversations.Models; + +namespace BotSharp.Plugin.PizzaBot.Functions; + +public class MakeOrderFn : IFunctionCallback +{ + public string Name => "make_order"; + + public async Task Execute(RoleDialogModel message) + { + message.ExecutionResult = "The order number is P123-01"; + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Functions/OrderFoundFn.cs b/tests/BotSharp.Plugin.PizzaBot/Functions/OrderFoundFn.cs new file mode 100644 index 00000000..90a85b50 --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/Functions/OrderFoundFn.cs @@ -0,0 +1,14 @@ +using BotSharp.Abstraction.Conversations.Models; + +namespace BotSharp.Plugin.PizzaBot.Functions; + +public class OrderFoundFn : IFunctionCallback +{ + public string Name => "order_found"; + + public async Task Execute(RoleDialogModel message) + { + message.ExecutionResult = "The order number is P123-01"; + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs new file mode 100644 index 00000000..be1bbe6f --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotAgentHook.cs @@ -0,0 +1,17 @@ +namespace BotSharp.Plugin.PizzaBot.Hooks; + +public class PizzaBotAgentHook : AgentHookBase +{ + public PizzaBotAgentHook(IServiceProvider services, AgentSettings settings) + : base(services, settings) + { + } + + public override bool OnInstructionLoaded(string template, Dictionary dict) + { + dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}"; + dict["current_time"] = $"{DateTime.Now:hh:mm t}"; + dict["current_weekday"] = $"{DateTime.Now:dddd}"; + return true; + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/PizzaBotPlugin.cs b/tests/BotSharp.Plugin.PizzaBot/PizzaBotPlugin.cs new file mode 100644 index 00000000..87b2c7ae --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/PizzaBotPlugin.cs @@ -0,0 +1,19 @@ +using BotSharp.Abstraction.Agents; +using BotSharp.Plugin.PizzaBot.Hooks; + +namespace BotSharp.Plugin.PizzaBot; + +public class PizzaBotPlugin : IBotSharpPlugin +{ + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + // Register callback function + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + // Register hooks + services.AddScoped(); + } +} diff --git a/tests/BotSharp.Plugin.PizzaBot/Using.cs b/tests/BotSharp.Plugin.PizzaBot/Using.cs new file mode 100644 index 00000000..e1f3009b --- /dev/null +++ b/tests/BotSharp.Plugin.PizzaBot/Using.cs @@ -0,0 +1,12 @@ +global using System; +global using System.Collections.Generic; +global using System.Text; +global using System.Threading.Tasks; +global using System.ComponentModel.DataAnnotations; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using BotSharp.Abstraction.Plugins; +global using BotSharp.Abstraction.Functions; +global using BotSharp.Plugin.PizzaBot.Functions; +global using BotSharp.Abstraction.Agents.Settings; +global using BotSharp.Core.Agents.Services; \ No newline at end of file