refine global stats by iceljc · Pull Request #1050 · SciSharp/BotSharp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ namespace BotSharp.Abstraction.Statistics.Models;

public class BotSharpStats
{
[JsonPropertyName("metric")]
public string Metric { get; set; } = null!;
[JsonPropertyName("agent_id")]
public string AgentId { get; set; } = null!;

[JsonPropertyName("dimension")]
public string Dimension { get; set; } = null!;
[JsonPropertyName("count")]
public StatsCount Count { get; set; } = new();

[JsonPropertyName("dim_ref_val")]
public string DimRefVal { get; set; } = null!;

[JsonPropertyName("data")]
public IDictionary<string, double> Data { get; set; } = new Dictionary<string, double>();
[JsonPropertyName("llm_cost")]
public StatsLlmCost LlmCost { get; set; } = new();

[JsonPropertyName("record_time")]
public DateTime RecordTime { get; set; } = DateTime.UtcNow;
Expand Down Expand Up @@ -44,11 +41,13 @@ public string Interval
[JsonPropertyName("end_time")]
public DateTime EndTime { get; set; }


public override string ToString()
{
return $"{Metric}-{Dimension}-{DimRefVal} ({Interval}): {Data?.Count ?? 0}";
return $"Stats: {AgentId}-{IntervalType}";
}


public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsInterval interval)
{
DateTime startTime = recordTime;
Expand All @@ -75,4 +74,25 @@ public static (DateTime, DateTime) BuildTimeInterval(DateTime recordTime, StatsI
endTime = DateTime.SpecifyKind(endTime, DateTimeKind.Utc);
return (startTime, endTime);
}
}

public class StatsCount
{
[JsonPropertyName("agent_call_count")]
public long AgentCallCount { get; set; }
}

public class StatsLlmCost
{
[JsonPropertyName("prompt_tokens")]
public long PromptTokens { get; set; }

[JsonPropertyName("completion_tokens")]
public long CompletionTokens { get; set; }

[JsonPropertyName("prompt_total_cost")]
public float PromptTotalCost { get; set; }

[JsonPropertyName("completion_total_cost")]
public float CompletionTotalCost { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BotSharp.Abstraction.Statistics.Enums;

namespace BotSharp.Abstraction.Statistics.Models;

public class BotSharpStatsDelta
{
public string AgentId { get; set; } = null!;
public StatsCountDelta CountDelta { get; set; } = new();
public StatsLlmCostDelta LlmCostDelta { get; set; } = new();
public DateTime RecordTime { get; set; } = DateTime.UtcNow;
public StatsInterval IntervalType { get; set; } = StatsInterval.Day;

public string Interval
{
get
{
return IntervalType.ToString();
}
set
{
if (Enum.TryParse(value, out StatsInterval type))
{
IntervalType = type;
}
}
}
}

public class StatsCountDelta
{
public int AgentCallCountDelta { get; set; }
}

public class StatsLlmCostDelta
{
public int PromptTokensDelta { get; set; }
public int CompletionTokensDelta { get; set; }
public float PromptTotalCostDelta { get; set; }
public float CompletionTotalCostDelta { get; set; }
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace BotSharp.Abstraction.Statistics.Services;

public interface IBotSharpStatsService
{
bool UpdateStats(string resourceKey, BotSharpStatsInput input);
bool UpdateStats(string @event, BotSharpStatsDelta delta);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,20 @@ public void AddToken(TokenStatsModel stats, RoleDialogModel message)
var dim = "agent";
var agentId = message.CurrentAgentId ?? string.Empty;
var globalStats = _services.GetRequiredService<IBotSharpStatsService>();
var body = new BotSharpStatsInput
var delta = new BotSharpStatsDelta
{
Metric = metric,
Dimension = dim,
DimRefVal = agentId,
AgentId = agentId,
RecordTime = DateTime.UtcNow,
IntervalType = StatsInterval.Day,
Data = [
new StatsKeyValuePair("prompt_token_count_total", stats.TotalInputTokens),
new StatsKeyValuePair("completion_token_count_total", stats.TotalOutputTokens),
new StatsKeyValuePair("prompt_cost_total", deltaPromptCost),
new StatsKeyValuePair("completion_cost_total", deltaCompletionCost)
]
LlmCostDelta = new()
{
PromptTokensDelta = stats.TotalInputTokens,
CompletionTokensDelta = stats.TotalOutputTokens,
PromptTotalCostDelta = deltaPromptCost,
CompletionTotalCostDelta = deltaCompletionCost
}
};
globalStats.UpdateStats($"global-{metric}-{dim}-{agentId}", body);
globalStats.UpdateStats($"global-{metric}-{dim}-{agentId}", delta);
}

public void PrintStatistics()
Expand Down
Loading