ai_client
A Rust crate for interacting with AI language model APIs, supporting multiple providers (Grok, Anthropic, OpenAI) through a unified ChatCompletionClient trait.
Features
- Unified interface for chat completions across different LLM providers
- Caching of responses using an LRU cache
- Exponential backoff for retrying failed requests
- Metrics tracking for requests, successes, errors, and cache hits
- Environment-based configuration
- Robust error handling
Installation
Add the following to your Cargo.toml:
[]
= { = "/path/to/ai_client" }
= { = "1.0", = ["full"] }
Or, if published to crates.io:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Usage
use ;
use Message;
use tokio;
async
Environment Variables
GROK_API_KEY: API key for Grok (required)GROK_API_ENDPOINT: API endpoint (default: https://api.x.ai/v1/chat/completions)GROK_MODEL: Model name (default: grok-3-mini-fast-latest)GROK_CACHE_SIZE: Cache size for responses (default: 100)
Building and Testing
License
Licensed under either MIT or Apache-2.0 at your option.