feat: Introduce a pluggable LLM provider system with token extraction, pricing, and updated setup configuration.
This commit is contained in:
70
Providers/ProviderFactory.cs
Normal file
70
Providers/ProviderFactory.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
namespace AnchorCli.Providers;
|
||||
|
||||
/// <summary>
|
||||
/// Factory for creating provider instances based on endpoint or provider name.
|
||||
/// </summary>
|
||||
internal static class ProviderFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a token extractor based on the provider name.
|
||||
/// </summary>
|
||||
public static ITokenExtractor CreateTokenExtractor(string providerName)
|
||||
{
|
||||
return providerName.ToLowerInvariant() switch
|
||||
{
|
||||
"openrouter" => new OpenRouterTokenExtractor(),
|
||||
"groq" => new GroqTokenExtractor(),
|
||||
"ollama" => new OllamaTokenExtractor(),
|
||||
_ => new GenericTokenExtractor()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a token extractor by auto-detecting from the endpoint URL.
|
||||
/// </summary>
|
||||
public static ITokenExtractor CreateTokenExtractorForEndpoint(string endpoint)
|
||||
{
|
||||
if (string.IsNullOrEmpty(endpoint))
|
||||
{
|
||||
return new GenericTokenExtractor();
|
||||
}
|
||||
|
||||
var url = endpoint.ToLowerInvariant();
|
||||
|
||||
if (url.Contains("openrouter"))
|
||||
{
|
||||
return new OpenRouterTokenExtractor();
|
||||
}
|
||||
|
||||
if (url.Contains("groq"))
|
||||
{
|
||||
return new GroqTokenExtractor();
|
||||
}
|
||||
|
||||
if (url.Contains("ollama") || url.Contains("localhost") || url.Contains("127.0.0.1"))
|
||||
{
|
||||
return new OllamaTokenExtractor();
|
||||
}
|
||||
|
||||
return new GenericTokenExtractor();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a pricing provider based on the provider name.
|
||||
/// Only OpenRouter has a pricing API currently.
|
||||
/// </summary>
|
||||
public static IPricingProvider? CreatePricingProvider(string providerName)
|
||||
{
|
||||
return providerName.ToLowerInvariant() switch
|
||||
{
|
||||
"openrouter" => new OpenRouterProvider(),
|
||||
_ => null // Other providers don't have pricing APIs yet
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if an endpoint is OpenRouter.
|
||||
/// </summary>
|
||||
public static bool IsOpenRouter(string endpoint) =>
|
||||
!string.IsNullOrEmpty(endpoint) && endpoint.Contains("openrouter", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
Reference in New Issue
Block a user