BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs

95 lines
2.9 KiB
C#
Raw Normal View History

2023-12-13 18:12:25 +00:00
using BotSharp.Abstraction.MLTasks;
using System.Diagnostics;
2023-09-24 21:32:58 +00:00
using System.Drawing;
namespace BotSharp.Core.Conversations.Services;
public class TokenStatistics : ITokenStatistics
{
private int _promptTokenCount = 0;
2023-09-26 03:04:04 +00:00
private float _promptCost = 0f;
2023-09-24 21:32:58 +00:00
private int _completionTokenCount = 0;
2023-09-26 03:04:04 +00:00
private float _completionCost = 0f;
2023-09-24 21:32:58 +00:00
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public int Total => _promptTokenCount + _completionTokenCount;
2023-09-26 03:04:04 +00:00
public string _model;
private Stopwatch _timer;
2023-09-24 21:32:58 +00:00
2023-09-26 03:04:04 +00:00
public float Cost => _promptCost + _completionCost;
2023-09-24 21:32:58 +00:00
public float AccumulatedCost
{
get
{
var stat = _services.GetRequiredService<IConversationStateService>();
2023-09-26 03:04:04 +00:00
return float.Parse(stat.GetState("llm_total_cost", "0"));
2023-09-24 21:32:58 +00:00
}
}
public TokenStatistics(IServiceProvider services, ILogger<TokenStatistics> logger)
{
_services = services;
_logger = logger;
}
2023-09-26 03:04:04 +00:00
public void AddToken(TokenStatsModel stats)
2023-09-24 21:32:58 +00:00
{
2023-09-26 03:04:04 +00:00
_model = stats.Model;
_promptTokenCount += stats.PromptCount;
_completionTokenCount += stats.CompletionCount;
2023-12-13 18:12:25 +00:00
var settingsService = _services.GetRequiredService<ILlmProviderSettingService>();
var settings = settingsService.GetSetting(stats.Provider, _model);
_promptCost += stats.PromptCount / 1000f * settings.PromptCost;
_completionCost += stats.CompletionCount / 1000f * settings.CompletionCost;
2023-09-24 21:32:58 +00:00
// Accumulated Token
var stat = _services.GetRequiredService<IConversationStateService>();
2023-12-13 18:12:25 +00:00
var inputCount = int.Parse(stat.GetState("prompt_total", "0"));
stat.SetState("prompt_total", stats.PromptCount + inputCount, false);
2023-12-13 18:12:25 +00:00
var outputCount = int.Parse(stat.GetState("completion_total", "0"));
stat.SetState("completion_total", stats.CompletionCount + outputCount, false);
2023-09-26 03:04:04 +00:00
// Total cost
2023-12-13 18:12:25 +00:00
var total_cost = float.Parse(stat.GetState("llm_total_cost", "0"));
total_cost += Cost;
stat.SetState("llm_total_cost", total_cost, false);
2023-09-24 21:32:58 +00:00
}
public void PrintStatistics()
{
2024-01-13 21:17:40 +00:00
if (_timer == null)
{
_timer = Stopwatch.StartNew();
}
else
{
_timer.Start();
}
var stats = $"Token Usage: {_promptTokenCount} prompt + {_completionTokenCount} completion = {Total} total tokens ({_timer.ElapsedMilliseconds / 1000f:f2}s). One-Way cost: {Cost:C4}, accumulated cost: {AccumulatedCost:C4}. [{_model}]";
2023-09-24 21:32:58 +00:00
#if DEBUG
2023-09-26 03:04:04 +00:00
Console.WriteLine(stats, Color.DarkGray);
2023-09-24 21:32:58 +00:00
#else
2023-09-26 03:04:04 +00:00
_logger.LogInformation(stats);
2023-09-24 21:32:58 +00:00
#endif
}
public void StartTimer()
{
if (_timer == null)
{
_timer = Stopwatch.StartNew();
}
else
{
_timer.Start();
}
}
public void StopTimer()
{
_timer.Stop();
}
2023-09-24 21:32:58 +00:00
}