1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! herolib-ai: AI client with multi-provider support.
//!
//! This crate provides a unified AI client that supports multiple providers
//! (Groq, OpenRouter, SambaNova) with automatic failover and verification support.
//!
//! # Features
//!
//! - **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
//!
//! # Example
//!
//! ```no_run
//! use herolib_ai::{AiClient, Model, PromptBuilderExt};
//!
//! // Create client from environment variables
//! let client = AiClient::from_env();
//!
//! // Simple chat
//! let response = client
//! .prompt()
//! .model(Model::Llama3_3_70B)
//! .system("You are a helpful coding assistant")
//! .user("Write a hello world in Rust")
//! .execute_content()
//! .unwrap();
//!
//! println!("{}", response);
//! ```
//!
//! # Verification Example
//!
//! ```no_run
//! use herolib_ai::{AiClient, Model, PromptBuilderExt};
//!
//! let client = AiClient::from_env();
//!
//! let response = client
//! .prompt()
//! .model(Model::Qwen2_5Coder32B)
//! .system("You are a JSON generator. Only output valid JSON.")
//! .user("Generate a JSON object with name and age fields")
//! .verify(|content| {
//! // Verify the response is valid JSON
//! match serde_json::from_str::<serde_json::Value>(content) {
//! Ok(_) => Ok(()),
//! Err(e) => Err(format!("Invalid JSON: {}. Please output only valid JSON.", e)),
//! }
//! })
//! .max_retries(3)
//! .execute_verified()
//! .unwrap();
//! ```
//!
//! # Environment Variables
//!
//! Set API keys using environment variables:
//! - `GROQ_API_KEY` - Groq API key
//! - `OPENROUTER_API_KEY` - OpenRouter API key
//! - `SAMBANOVA_API_KEY` - SambaNova API key
// Re-export commonly used types
pub use ;
pub use ;
pub use ;
pub use Model;
pub use ;
pub use ;
pub use ;
pub use ;