print functions in verbose.

This commit is contained in:
hchen 2023-09-27 22:31:58 -05:00
parent 6ce664d618
commit 649cba561e
7 changed files with 34 additions and 24 deletions

View file

@ -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()
{

View file

@ -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);

View file

@ -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<IInstructService, InstructService>();
services.AddScoped<ITokenStatistics, TokenStatistics>();
services.AddScoped<IAgentHook, AgentHook>();
services.AddScoped<IAgentHook, RoutingAgentHook>();
return services;
}

View file

@ -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

View file

@ -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<FunctionDef> functions)
{
return base.OnFunctionsLoaded(ref functions);
}
}

View file

@ -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<FunctionDef> 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);
}
}

View file

@ -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;