llm-edge-providers
A unified Rust library for interfacing with multiple Large Language Model (LLM) providers through a consistent API. This crate provides production-ready adapters with built-in retry logic, error handling, and observability.
Features
- Unified API: Single interface for all LLM providers
- Multiple Providers: Support for 5 major LLM providers:
- OpenAI (GPT-4, GPT-3.5, o1)
- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus/Haiku)
- Google (Gemini Pro, Gemini Ultra)
- AWS Bedrock (Multi-model support)
- Azure OpenAI (Enterprise-ready)
- Async/Await: Built on Tokio for efficient async operations
- Retry Logic: Automatic retry with exponential backoff
- Security: Secure API key handling with
secrecy - Observability: OpenTelemetry integration for tracing
- Type Safety: Strongly typed requests and responses
- Cost Tracking: Built-in pricing information and usage tracking
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Usage
Basic Example
use ;
async
OpenAI Provider
use OpenAIAdapter;
let provider = new;
// Get pricing information
if let Some = provider.get_pricing
// Check provider health
let health = provider.health.await;
Anthropic Provider
use AnthropicAdapter;
let provider = new;
let request = UnifiedRequest ;
let response = provider.send.await?;
Multi-Provider Usage
use ;
async
// Use with any provider
let openai = new;
let anthropic = new;
let result1 = send_to_provider.await?;
let result2 = send_to_provider.await?;
Error Handling
use ProviderError;
match provider.send.await
Provider Configuration
OpenAI
Set your API key:
Supported models: gpt-4, gpt-4-turbo, gpt-3.5-turbo, o1-preview, o1-mini
Anthropic
Set your API key:
Supported models: claude-3-5-sonnet-20240229, claude-3-opus-20240229, claude-3-haiku-20240307
Google (Gemini)
Set your API key:
Supported models: gemini-pro, gemini-ultra
AWS Bedrock
Configure AWS credentials:
Azure OpenAI
Configure Azure credentials:
API Documentation
Core Types
-
UnifiedRequest: Standard request format across all providersmodel: Model identifiermessages: Conversation messagestemperature: Sampling temperature (0.0-2.0)max_tokens: Maximum tokens to generatestream: Enable streaming responsesmetadata: Custom metadata
-
UnifiedResponse: Standard response formatid: Unique response identifiermodel: Model usedchoices: Response choicesusage: Token usage statisticsmetadata: Response metadata (provider, latency, cost)
-
LLMProvider: Core trait implemented by all adaptersname(): Provider namesend(): Send request to providerget_pricing(): Get model pricinghealth(): Check provider health
Error Types
All errors are represented by ProviderError:
Http: HTTP client errorsSerialization: JSON serialization errorsApiError: Provider API errors with status codeTimeout: Request timeoutRateLimitExceeded: Rate limit hitConfiguration: Invalid configurationInternal: Internal errors
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Contributing
Contributions are welcome! Please see the repository for contribution guidelines.
Related Crates
llm-edge-core- Core abstractions and typesllm-edge-router- Request routing and load balancingllm-edge-cache- Response cachingllm-edge-backend- Complete backend service