feat: Introduce a pluggable LLM provider system with token extraction, pricing, and updated setup configuration.
This commit is contained in:
61
Providers/GroqProvider.cs
Normal file
61
Providers/GroqProvider.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace AnchorCli.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Token extractor for Groq responses.
|
||||
/// </summary>
|
||||
internal sealed class GroqTokenExtractor : ITokenExtractor
|
||||
{
|
||||
public string ProviderName => "Groq";
|
||||
|
||||
public (int inputTokens, int outputTokens)? ExtractTokens(HttpResponseHeaders headers, string? responseBody)
|
||||
{
|
||||
// Groq provides x-groq-tokens header (format: "n;<prompt_tokens>,n;<completion_tokens>")
|
||||
if (headers.TryGetValues("x-groq-tokens", out var values))
|
||||
{
|
||||
var tokenStr = values.FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty(tokenStr))
|
||||
{
|
||||
// Parse format: "n;123,n;45" where first is prompt, second is completion
|
||||
var parts = tokenStr.Split(',');
|
||||
if (parts.Length >= 2)
|
||||
{
|
||||
var inputPart = parts[0].Trim();
|
||||
var outputPart = parts[1].Trim();
|
||||
|
||||
// Extract numbers after "n;"
|
||||
if (inputPart.StartsWith("n;") && outputPart.StartsWith("n;"))
|
||||
{
|
||||
if (int.TryParse(inputPart[2..], out var input) &&
|
||||
int.TryParse(outputPart[2..], out var output))
|
||||
{
|
||||
return (input, output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: try parsing from response body
|
||||
if (!string.IsNullOrEmpty(responseBody))
|
||||
{
|
||||
// TODO: Parse usage from JSON body if headers aren't available
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public int? ExtractLatency(HttpResponseHeaders headers)
|
||||
{
|
||||
if (headers.TryGetValues("x-groq-response-time", out var values))
|
||||
{
|
||||
if (int.TryParse(values.FirstOrDefault(), out var latency))
|
||||
{
|
||||
return latency;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user