From 37aa85af369b326eb53c1a47d0f0d6640e9fc565 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 20 Feb 2025 22:52:02 -0600 Subject: [PATCH] refine chat instruction --- .../BotSharp.Core/BotSharp.Core.csproj | 2 +- .../Instructs/InsturctionPlugin.cs | 23 +++++++++++++++++++ .../{ => Services}/InstructService.Execute.cs | 0 .../InstructService.Instruct.cs | 0 .../{ => Services}/InstructService.cs | 0 .../Controllers/InstructModeController.cs | 13 ++++++----- .../Instructs/InstructMessageModel.cs | 7 ++++++ 7 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs rename src/Infrastructure/BotSharp.Core/Instructs/{ => Services}/InstructService.Execute.cs (100%) rename src/Infrastructure/BotSharp.Core/Instructs/{ => Services}/InstructService.Instruct.cs (100%) rename src/Infrastructure/BotSharp.Core/Instructs/{ => Services}/InstructService.cs (100%) diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index ecc736f2..6bc7596c 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -1,4 +1,4 @@ - + $(TargetFramework) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs b/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs new file mode 100644 index 00000000..7b4596ff --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs @@ -0,0 +1,23 @@ +using BotSharp.Abstraction.Plugins.Models; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Instructs; + +public class InsturctionPlugin : IBotSharpPlugin +{ + public string Id => "8189e133-819c-4505-9f82-84f793bc1be0"; + public string Name => "Instruction"; + public string Description => "Handle agent instruction request"; + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + + } + + public bool AttachMenu(List menu) + { + var section = menu.First(x => x.Label == "Apps"); + menu.Add(new PluginMenuDef("Instruction", link: "page/instruction", icon: "bx bx-book-content", weight: section.Weight + 5)); + return true; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.Execute.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs similarity index 100% rename from src/Infrastructure/BotSharp.Core/Instructs/InstructService.Execute.cs rename to src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.Instruct.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Instruct.cs similarity index 100% rename from src/Infrastructure/BotSharp.Core/Instructs/InstructService.Instruct.cs rename to src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Instruct.cs diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.cs similarity index 100% rename from src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs rename to src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.cs diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index 433d691a..dbce9598 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -44,7 +44,7 @@ public class InstructModeController : ControllerBase } [HttpPost("/instruct/text-completion")] - public async Task TextCompletion([FromBody] IncomingMessageModel input) + public async Task TextCompletion([FromBody] IncomingInstructRequest input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -53,12 +53,12 @@ public class InstructModeController : ControllerBase .SetState("model_id", input.ModelId, source: StateSource.External); var textCompletion = CompletionProvider.GetTextCompletion(_services); - return await textCompletion.GetCompletion(input.Text, Guid.Empty.ToString(), Guid.NewGuid().ToString()); + return await textCompletion.GetCompletion(input.Text, input.AgentId ?? Guid.Empty.ToString(), Guid.NewGuid().ToString()); } #region Chat [HttpPost("/instruct/chat-completion")] - public async Task ChatCompletion([FromBody] IncomingMessageModel input) + public async Task ChatCompletion([FromBody] IncomingInstructRequest input) { var state = _services.GetRequiredService(); input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); @@ -66,10 +66,11 @@ public class InstructModeController : ControllerBase .SetState("model", input.Model, source: StateSource.External) .SetState("model_id", input.ModelId, source: StateSource.External); - var textCompletion = CompletionProvider.GetChatCompletion(_services); - var message = await textCompletion.GetChatCompletions(new Agent() + var completion = CompletionProvider.GetChatCompletion(_services); + var message = await completion.GetChatCompletions(new Agent() { - Id = Guid.Empty.ToString(), + Id = input.AgentId ?? Guid.Empty.ToString(), + Instruction = input.Instruction }, new List { new RoleDialogModel(AgentRole.User, input.Text) diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs index b736330f..3b0303fd 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs @@ -9,3 +9,10 @@ public class InstructMessageModel : IncomingMessageModel public override string Channel { get; set; } = ConversationChannel.OpenAPI; public string? Template { get; set; } } + + +public class IncomingInstructRequest : IncomingMessageModel +{ + public string? AgentId { get; set; } + public string? Instruction { get; set; } +} \ No newline at end of file