Improve TokenStatistics.

This commit is contained in:
hchen2020 2023-09-26 11:18:42 -05:00
parent 3eaccfbe6a
commit ffdb33e234
6 changed files with 30 additions and 14 deletions

View file

@ -5,18 +5,9 @@ namespace BotSharp.Abstraction.Functions.Models;
public class FunctionCallFromLlm : RoutingArgs
{
[JsonPropertyName("function")]
public string Function { get; set; } = string.Empty;
[JsonPropertyName("reason")]
public string Reason { get; set; } = string.Empty;
[JsonPropertyName("question")]
public string? Question { get; set; }
[JsonPropertyName("answer")]
public string Answer { get; set; } = string.Empty;
[JsonPropertyName("args")]
public JsonDocument Arguments { get; set; } = JsonDocument.Parse("{}");

View file

@ -2,11 +2,29 @@ namespace BotSharp.Abstraction.Routing.Models;
public class RoutingArgs
{
[JsonPropertyName("function")]
public string Function { get; set; } = string.Empty;
[JsonPropertyName("reason")]
public string Reason { get; set; } = string.Empty;
[JsonPropertyName("answer")]
public string Answer { get; set; } = string.Empty;
[JsonPropertyName("agent")]
public string AgentName { get; set; } = string.Empty;
public override string ToString()
{
return AgentName;
var route = string.IsNullOrEmpty(AgentName) ? "" : $"<Route to {AgentName.ToUpper()} because {Reason}>";
if (string.IsNullOrEmpty(Answer))
{
return $"[{Function} {route}]";
}
else
{
return $"[{Function} {route}] => {Answer}";
}
}
}

View file

@ -51,7 +51,7 @@ public class TokenStatistics : ITokenStatistics
public void PrintStatistics()
{
var stats = $"Token Usage: {_promptTokenCount} prompt + {_completionTokenCount} completion = {Total} total (${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: {_model}";
#if DEBUG
Console.WriteLine(stats, Color.DarkGray);
#else

View file

@ -9,7 +9,7 @@ public class ResponseToUserRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
public string Name => "response_to_user";
public string Description => "You know how to response according to the context, don't need to ask specific agent.";
public string Description => "You know how to response according to the context without asking specific agent. For example user greeting.";
public List<NameDesc> Parameters => new List<NameDesc>
{

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Templating;
using System.Drawing;
@ -37,7 +38,12 @@ public abstract class RoutingHandlerBase
public async Task<FunctionCallFromLlm> GetNextInstructionFromReasoner(string prompt)
{
var responseFormat = JsonSerializer.Serialize(new FunctionCallFromLlm());
var responseFormat = _settings.EnableReasoning ?
JsonSerializer.Serialize(new FunctionCallFromLlm()) :
JsonSerializer.Serialize(new RoutingArgs
{
Function = "route_to_agent"
});
var content = $"{prompt} Response must be in JSON format {responseFormat}";
var chatCompletion = CompletionProvider.GetChatCompletion(_services,

View file

@ -81,7 +81,8 @@ public class RoutingService : IRoutingService
{
loopCount++;
var inst = await handler.GetNextInstructionFromReasoner($"Tell me the next step?");
var prompt = _settings.EnableReasoning ? "Tell me the next step?" : "Which agent is suitable to handle user's request?";
var inst = await handler.GetNextInstructionFromReasoner(prompt);
inst.Question = inst.Question ?? message;
handler = handlers.FirstOrDefault(x => x.Name == inst.Function);