herolib-ai
AI client with multi-provider support (Groq, OpenRouter, SambaNova) and automatic failover.
Overview
This crate provides a unified AI client that supports multiple providers with:
- Multi-provider support: Automatically tries providers in order of preference
- OpenAI-compatible API: Works with any OpenAI-compatible endpoint
- Automatic failover: Falls back to alternative providers on failure
- Verification support: Retry with feedback until response passes validation
- Model abstraction: Use our model names, mapped to provider-specific IDs
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Environment Variables
Set API keys using environment variables:
Usage
Simple Chat
use ;
let client = from_env;
let response = client
.prompt
.model
.system
.user
.execute_content
.unwrap;
println!;
With Verification
use ;
/// Verifies that the response is valid JSON.
/// Returns Ok(()) if valid, or Err with feedback for the AI to retry.
let client = from_env;
let response = client
.prompt
.model
.system
.user
.verify
.max_retries
.execute_verified
.unwrap;
Manual Provider Configuration
use ;
let client = new
.with_provider
.with_provider
.with_default_temperature
.with_default_max_tokens;
Available Models
| Model | Description | Providers |
|---|---|---|
Llama3_3_70B |
Fast, capable model for general tasks | Groq, SambaNova, OpenRouter |
Llama3_1_70B |
Versatile model for various tasks | Groq, SambaNova, OpenRouter |
Llama3_1_8B |
Small, fast model for simple tasks | Groq, SambaNova, OpenRouter |
Qwen2_5Coder32B |
Specialized for code generation | Groq, SambaNova, OpenRouter |
DeepSeekCoderV2_5 |
Advanced coding model | OpenRouter, SambaNova |
DeepSeekV3 |
Latest DeepSeek model | OpenRouter, SambaNova |
Llama3_1_405B |
Largest Llama model for complex tasks | SambaNova, OpenRouter |
Mixtral8x7B |
Efficient mixture of experts model | Groq, OpenRouter |
Llama3_2_90BVision |
Multimodal model with vision | Groq, OpenRouter |
Llama3_2_11BVision |
Smaller vision model | Groq, SambaNova, OpenRouter |
Providers
Groq
- Fast inference provider
- API:
https://api.groq.com/openai/v1/chat/completions - Env:
GROQ_API_KEY
OpenRouter
- Unified API for multiple models
- API:
https://openrouter.ai/api/v1/chat/completions - Env:
OPENROUTER_API_KEY
SambaNova
- High-performance AI inference
- API:
https://api.sambanova.ai/v1/chat/completions - Env:
SAMBANOVA_API_KEY
Model Test Utility
The modeltest binary tests model availability across all configured providers.
What it does
- Queries provider model lists - Fetches available models from each provider's API
- Validates model mappings - Checks if our configured model IDs exist on each provider
- Tests each model - Sends a simple "whoami" query to verify the model works
- Generates a report - Shows success/failure status for each model on each provider
Running the test
# Build and run
# Or after building
Example output
herolib-ai Model Test Utility
Testing model availability across all providers
Configured providers:
- Groq
- OpenRouter
======================================================================
Phase 1: Querying Provider Model Lists
======================================================================
Querying Groq... OK (42 models)
Querying OpenRouter... OK (256 models)
======================================================================
Phase 2: Validating Model Mappings
======================================================================
Llama 3.3 70B (Llama 3.3 70B - Fast, capable model for general tasks):
Groq llama-3.3-70b-versatile -> OK
OpenRouter meta-llama/llama-3.3-70b-instruct -> OK
======================================================================
Phase 3: Testing Models with 'whoami' Query
======================================================================
--------------------------------------------------
Testing: Llama 3.3 70B
--------------------------------------------------
Groq (llama-3.3-70b-versatile)... OK (523ms)
Response: I'm LLaMA, a large language model trained by Meta AI.
======================================================================
Final Report
======================================================================
Test Summary:
Total tests: 20
Successful: 18
Failed: 2
Success rate: 90.0%
All tests passed!
Building
Testing
License
Apache-2.0