From 649cba561e6066bfdc31d4b3a029cf1f7a708dff Mon Sep 17 00:00:00 2001 From: hchen Date: Wed, 27 Sep 2023 22:31:58 -0500 Subject: [PATCH] print functions in verbose. --- .../Functions/Models/FunctionDef.cs | 2 +- .../Agents/Services/AgentService.LoadAgent.cs | 4 +--- .../BotSharpServiceCollectionExtensions.cs | 3 +-- .../Conversations/Services/TokenStatistics.cs | 2 +- .../BotSharp.Core/Routing/Hooks/AgentHook.cs | 16 -------------- .../Routing/Hooks/RoutingAgentHook.cs | 21 +++++++++++++++++++ .../Providers/ChatCompletionProvider.cs | 10 ++++++++- 7 files changed, 34 insertions(+), 24 deletions(-) delete mode 100644 src/Infrastructure/BotSharp.Core/Routing/Hooks/AgentHook.cs create mode 100644 src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingAgentHook.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionDef.cs b/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionDef.cs index c74ca533..153fb473 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionDef.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionDef.cs @@ -4,7 +4,7 @@ public class FunctionDef { public string Name { get; set; } public string Description { get; set; } - public FunctionParametersDef Parameters { get; set; } + public FunctionParametersDef Parameters { get; set; } = new FunctionParametersDef(); public override string ToString() { diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 59926c43..553d6436 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -1,6 +1,4 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Routing; -using BotSharp.Abstraction.Routing.Settings; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Agents.Services; @@ -34,7 +32,7 @@ public partial class AgentService hook.OnInstructionLoaded(agent.Instruction, templateDict); } - if (!agent.Functions.IsNullOrEmpty()) + if (agent.Functions != null) { var functions = agent.Functions; hook.OnFunctionsLoaded(ref functions); diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index cb42139a..2d8852ed 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -9,7 +9,6 @@ using BotSharp.Abstraction.Templating; using BotSharp.Core.Instructs; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Routing; -using System.Reflection; using BotSharp.Core.Routing.Hooks; namespace BotSharp.Core; @@ -64,7 +63,7 @@ public static class BotSharpServiceCollectionExtensions services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); return services; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs index 1654b357..b86364ea 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs @@ -51,7 +51,7 @@ public class TokenStatistics : ITokenStatistics public void PrintStatistics() { - var stats = $"Token Usage: {_promptTokenCount} prompt + {_completionTokenCount} completion = {Total} total tokens. One-Way cost: ${Cost:C4}, accumulated cost: ${AccumulatedCost:C4}. Model: {_model}"; + var stats = $"Token Usage: {_promptTokenCount} prompt + {_completionTokenCount} completion = {Total} total tokens. One-Way cost: {Cost:C4}, accumulated cost: {AccumulatedCost:C4}. [{_model}]"; #if DEBUG Console.WriteLine(stats, Color.DarkGray); #else diff --git a/src/Infrastructure/BotSharp.Core/Routing/Hooks/AgentHook.cs b/src/Infrastructure/BotSharp.Core/Routing/Hooks/AgentHook.cs deleted file mode 100644 index 006ff5a1..00000000 --- a/src/Infrastructure/BotSharp.Core/Routing/Hooks/AgentHook.cs +++ /dev/null @@ -1,16 +0,0 @@ -using BotSharp.Abstraction.Functions.Models; - -namespace BotSharp.Core.Routing.Hooks; - -public class AgentHook : AgentHookBase -{ - public AgentHook(IServiceProvider services, AgentSettings settings) - : base(services, settings) - { - } - - public override bool OnFunctionsLoaded(ref List functions) - { - return base.OnFunctionsLoaded(ref functions); - } -} diff --git a/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingAgentHook.cs b/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingAgentHook.cs new file mode 100644 index 00000000..17ed9c34 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingAgentHook.cs @@ -0,0 +1,21 @@ +using BotSharp.Abstraction.Functions.Models; + +namespace BotSharp.Core.Routing.Hooks; + +public class RoutingAgentHook : AgentHookBase +{ + public RoutingAgentHook(IServiceProvider services, AgentSettings settings) + : base(services, settings) + { + } + + public override bool OnFunctionsLoaded(ref List functions) + { + functions.Add(new FunctionDef + { + Name = "fallback_to_router", + Description = "If the user's request is beyond your capabilities, you can call this function for help." + }); + return base.OnFunctionsLoaded(ref functions); + } +} diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index 13d196a0..7a4b9914 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -290,7 +290,7 @@ public class ChatCompletionProvider : IChatCompletion if (convSetting.ShowVerboseLog) { _logger.LogInformation("VERBOSE COMPLETION MESSAGES"); - var verbose = string.Join("\n", chatCompletionsOptions.Messages.Select(x => + var verbose = string.Join("\r\n", chatCompletionsOptions.Messages.Select(x => { return x.Role == ChatRole.Function ? $"{x.Role}: {x.Name} => {x.Content}" : @@ -298,6 +298,14 @@ public class ChatCompletionProvider : IChatCompletion })); _logger.LogInformation(verbose); + + _logger.LogInformation("VERBOSE FUNCTIONS"); + verbose = string.Join("\r\n", chatCompletionsOptions.Functions.Select(x => + { + return $"{x.Name}: {x.Description}\r\n{x.Parameters}"; + })); + + _logger.LogInformation(verbose); } return chatCompletionsOptions;