From d72204cd2230f3b0ccf430001b832b06648ed5fc Mon Sep 17 00:00:00 2001 From: hchen Date: Wed, 26 Jul 2023 16:05:30 -0500 Subject: [PATCH] Add FunctionDefinition. --- .../Agents/Models/Agent.cs | 10 +++--- .../BotSharp.Abstraction.csproj | 1 + .../Conversations/Models/FunctionDef.cs | 10 ++++++ .../BotSharp.Core/Agents/AgentController.cs | 5 +-- .../Agents/Services/AgentService.GetAgents.cs | 21 +++++++++++- .../Services/AgentService.UpdateAgent.cs | 2 +- .../Agents/Services/AgentService.cs | 5 ++- .../Agents/ViewModels/AgentUpdateModel.cs | 34 ++++++++++++++----- .../Providers/ChatCompletionProvider.cs | 26 ++++++++++++++ 9 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Conversations/Models/FunctionDef.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 7330e31b..93276f70 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -18,13 +18,13 @@ public class Agent /// public string Samples { get; set; } + /// + /// Functions + /// + public string Functions { get; set; } + /// /// Domain knowledges /// public string Knowledges { get; set;} - - /// - /// Owner user id - /// - public string OwerId { get; set; } = string.Empty; } diff --git a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj index 6825140b..0553f6c5 100644 --- a/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj +++ b/src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj @@ -26,6 +26,7 @@ + diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/FunctionDef.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/FunctionDef.cs new file mode 100644 index 00000000..5c050b5a --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/FunctionDef.cs @@ -0,0 +1,10 @@ +using System.Text.Json; + +namespace BotSharp.Abstraction.Conversations.Models; + +public class FunctionDef +{ + public string Name { get; set; } + public string Description { get; set; } + public JsonDocument Parameters { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs index d668345e..3e17e193 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs @@ -10,11 +10,9 @@ namespace BotSharp.Core.Agents; public class AgentController : ControllerBase, IApiAdapter { private readonly IAgentService _agentService; - private readonly IUserIdentity _user; - public AgentController(IAgentService agentService, IUserIdentity user) + public AgentController(IAgentService agentService) { _agentService = agentService; - _user = user; } [HttpPost("/agent")] @@ -30,7 +28,6 @@ public class AgentController : ControllerBase, IApiAdapter { var model = agent.ToAgent(); model.Id = agentId; - model.OwerId = _user.Id; await _agentService.UpdateAgent(model); } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 583bfc27..e20b5bcc 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Agents.Models; +using Microsoft.Extensions.Logging; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -30,11 +31,29 @@ public partial class AgentService { profile.Instruction = File.ReadAllText(instructionFile); } + else + { + _logger.LogError($"Can't find instruction file from {instructionFile}"); + } var samplesFile = Path.Combine(dir, "samples.txt"); if (File.Exists(samplesFile)) { - profile.Samples = File.ReadAllText(Path.Combine(dir, "samples.txt")); + profile.Samples = File.ReadAllText(samplesFile); + } + else + { + _logger.LogWarning($"Can't find samples file from {samplesFile}"); + } + + var functionsFile = Path.Combine(dir, "functions.json"); + if (File.Exists(functionsFile)) + { + profile.Functions = File.ReadAllText(functionsFile); + } + else + { + _logger.LogInformation($"Can't find functions file from {functionsFile}"); } return profile; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 572b795e..dfbd1fd9 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -13,7 +13,7 @@ public partial class AgentService { var record = (from a in db.Agent join ua in db.UserAgent on a.Id equals ua.AgentId - where ua.UserId == agent.OwerId && a.Id == agent.Id + where ua.UserId == _user.Id && a.Id == agent.Id select a).First(); record.Name = agent.Name; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index 55238f8d..4b2ad947 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Logging; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -5,12 +6,14 @@ namespace BotSharp.Core.Agents.Services; public partial class AgentService : IAgentService { private readonly IServiceProvider _services; + private readonly ILogger _logger; private readonly IUserIdentity _user; private readonly AgentSettings _settings; - public AgentService(IServiceProvider services, IUserIdentity user, AgentSettings settings) + public AgentService(IServiceProvider services, ILogger logger, IUserIdentity user, AgentSettings settings) { _services = services; + _logger = logger; _user = user; _settings = settings; } diff --git a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs index 0dc886e0..5b519953 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs @@ -4,27 +4,43 @@ namespace BotSharp.Core.Agents.ViewModels; public class AgentUpdateModel { - public string Name { get; set; } - public string Description { get; set; } + public string Name { get; set; } = string.Empty; + public string? Description { get; set; } /// /// Instruction /// - public string Instruction { get; set; } + public string? Instruction { get; set; } /// /// Samples /// - public string Samples { get; set; } + public string? Samples { get; set; } + + /// + /// Functions + /// + public string? Functions { get; set; } public Agent ToAgent() { - return new Agent + var agent = new Agent { - Name = Name, - Description = Description, - Instruction = Instruction, - Samples = Samples + Name = Name }; + + if (Description != null) + agent.Description = Description; + + if (Instruction != null) + agent.Instruction = Instruction; + + if (Samples != null) + agent.Samples = Samples; + + if (Functions != null) + agent.Functions = Functions; + + return agent; } } diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index b2a47a67..1de10c9a 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -6,6 +6,7 @@ using BotSharp.Abstraction.MLTasks; using BotSharp.Plugin.AzureOpenAI.Settings; using System; using System.Collections.Generic; +using System.Text.Json; using System.Threading.Tasks; namespace BotSharp.Plugin.AzureOpenAI.Providers; @@ -70,6 +71,20 @@ public class ChatCompletionProvider : IChatCompletion return samples; } + public List GetFunctions(string functionsJson) + { + var functions = new List(); + if (!string.IsNullOrEmpty(functionsJson)) + { + functions = JsonSerializer.Deserialize>(functionsJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + AllowTrailingCommas = true + }); + } + + return functions; + } public async Task GetChatCompletionsStreamingAsync(Agent agent, List conversations) { @@ -114,6 +129,17 @@ public class ChatCompletionProvider : IChatCompletion chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)); } + var functions = GetFunctions(agent.Functions); + foreach (var function in functions) + { + chatCompletionsOptions.Functions.Add(new FunctionDefinition + { + Name = function.Name, + Description = function.Description, + Parameters = BinaryData.FromObjectAsJson(function.Parameters) + }); + } + foreach (var message in conversations) { chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));