herolib_ai/lib.rs
1//! herolib-ai: AI client with multi-provider support.
2//!
3//! This crate provides a unified AI client that supports multiple providers
4//! (Groq, OpenRouter, SambaNova) with automatic failover and verification support.
5//!
6//! # Features
7//!
8//! - **Multi-provider support**: Automatically tries providers in order of preference
9//! - **OpenAI-compatible API**: Works with any OpenAI-compatible endpoint
10//! - **Automatic failover**: Falls back to alternative providers on failure
11//! - **Verification support**: Retry with feedback until response passes validation
12//! - **Model abstraction**: Use our model names, mapped to provider-specific IDs
13//!
14//! # Example
15//!
16//! ```no_run
17//! use herolib_ai::{AiClient, Model, PromptBuilderExt};
18//!
19//! // Create client from environment variables
20//! let client = AiClient::from_env();
21//!
22//! // Simple chat
23//! let response = client
24//! .prompt()
25//! .model(Model::Llama3_3_70B)
26//! .system("You are a helpful coding assistant")
27//! .user("Write a hello world in Rust")
28//! .execute_content()
29//! .unwrap();
30//!
31//! println!("{}", response);
32//! ```
33//!
34//! # Verification Example
35//!
36//! ```no_run
37//! use herolib_ai::{AiClient, Model, PromptBuilderExt};
38//!
39//! let client = AiClient::from_env();
40//!
41//! let response = client
42//! .prompt()
43//! .model(Model::Qwen2_5Coder32B)
44//! .system("You are a JSON generator. Only output valid JSON.")
45//! .user("Generate a JSON object with name and age fields")
46//! .verify(|content| {
47//! // Verify the response is valid JSON
48//! match serde_json::from_str::<serde_json::Value>(content) {
49//! Ok(_) => Ok(()),
50//! Err(e) => Err(format!("Invalid JSON: {}. Please output only valid JSON.", e)),
51//! }
52//! })
53//! .max_retries(3)
54//! .execute_verified()
55//! .unwrap();
56//! ```
57//!
58//! # Environment Variables
59//!
60//! Set API keys using environment variables:
61//! - `GROQ_API_KEY` - Groq API key
62//! - `OPENROUTER_API_KEY` - OpenRouter API key
63//! - `SAMBANOVA_API_KEY` - SambaNova API key
64
65pub mod client;
66pub mod embedding;
67pub mod error;
68pub mod model;
69pub mod prompt;
70pub mod provider;
71pub mod transcription;
72pub mod types;
73
74#[cfg(feature = "rhai")]
75pub mod rhai;
76
77// Re-export commonly used types
78pub use client::{AiClient, chat_simple};
79pub use embedding::{EmbeddingModel, EmbeddingRequest, EmbeddingResponse};
80pub use error::{AiError, AiResult};
81pub use model::Model;
82pub use prompt::{PromptBuilder, PromptBuilderExt, VerifyFn};
83pub use provider::{Provider, ProviderConfig};
84pub use transcription::{
85 TranscriptionModel, TranscriptionOptions, TranscriptionResponse, VerboseTranscriptionResponse,
86};
87pub use types::{ChatCompletionRequest, ChatCompletionResponse, Message, Role, Usage};