40 lines
1.0 KiB
C#
40 lines
1.0 KiB
C#
using System.Net.Http.Headers;
|
|
|
|
namespace AnchorCli.Providers;
|
|
|
|
/// <summary>
|
|
/// Token extractor for Ollama responses.
|
|
/// Ollama doesn't provide official token counts, so we estimate.
|
|
/// </summary>
|
|
internal sealed class OllamaTokenExtractor : ITokenExtractor
|
|
{
|
|
public string ProviderName => "Ollama";
|
|
|
|
public (int inputTokens, int outputTokens)? ExtractTokens(HttpResponseHeaders headers, string? responseBody)
|
|
{
|
|
// Ollama doesn't provide token headers
|
|
return null;
|
|
}
|
|
|
|
public int? ExtractLatency(HttpResponseHeaders headers)
|
|
{
|
|
// Ollama doesn't provide latency headers
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Estimates token count from text length (rough approximation).
|
|
/// Assumes ~4 characters per token on average.
|
|
/// </summary>
|
|
public static int EstimateTokens(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// Rough estimate: 4 characters per token
|
|
return text.Length / 4;
|
|
}
|
|
}
|