Skip to main content

adk_model/
lib.rs

1//! # adk-model
2#![allow(clippy::result_large_err)]
3//!
4//! LLM model integrations for ADK (Gemini, OpenAI, OpenRouter, Anthropic, DeepSeek, Groq,
5//! Ollama, Fireworks AI, Together AI, Mistral AI, Perplexity, Cerebras, SambaNova, Amazon
6//! Bedrock, Azure AI Inference).
7//!
8//! ## Overview
9//!
10//! This crate provides LLM implementations for ADK agents. Currently supports:
11//!
12//! - [`GeminiModel`] - Google's Gemini models (3 Pro, 2.5 Flash, etc.)
13//! - `OpenAIClient` - OpenAI models (GPT-5, GPT-5-mini, o3, etc.) — requires `openai` feature
14//! - `AzureOpenAIClient` - Azure OpenAI Service — requires `openai` feature
15//! - `OpenAICompatible` - Any OpenAI-compatible API (xAI, Fireworks, Together, Mistral, Perplexity, Cerebras, SambaNova, or custom) — requires `openai` feature, use `OpenAICompatibleConfig` presets
16//! - `AnthropicClient` - Anthropic Claude models — requires `anthropic` feature
17//! - `DeepSeekClient` - DeepSeek models — requires `deepseek` feature
18//! - `GroqClient` - Groq ultra-fast inference — requires `groq` feature
19//! - `OpenRouterClient` - OpenRouter-native chat, responses, and discovery APIs — requires `openrouter` feature
20//! - `OllamaModel` - Local LLMs via Ollama — requires `ollama` feature
21//! - `BedrockClient` - Amazon Bedrock via AWS SDK — requires `bedrock` feature
22//! - `AzureAIClient` - Azure AI Inference endpoints — requires `azure-ai` feature
23//! - [`MockLlm`] - Mock LLM for testing
24//!
25//! ## Quick Start
26//!
27//! ### Gemini
28//!
29//! ```rust,no_run
30//! use adk_model::GeminiModel;
31//! use std::sync::Arc;
32//!
33//! let api_key = std::env::var("GOOGLE_API_KEY").unwrap();
34//! let model = GeminiModel::new(&api_key, "gemini-2.5-flash").unwrap();
35//! ```
36//!
37//! ### OpenAI
38//!
39//! ```rust,ignore
40//! use adk_model::openai::{OpenAIClient, OpenAIConfig};
41//!
42//! let model = OpenAIClient::new(OpenAIConfig::new(
43//!     std::env::var("OPENAI_API_KEY").unwrap(),
44//!     "gpt-5-mini",
45//! )).unwrap();
46//! ```
47//!
48//! ### OpenRouter
49//!
50//! ```rust,ignore
51//! use adk_core::{Content, Llm, LlmRequest};
52//! use adk_model::openrouter::{OpenRouterClient, OpenRouterConfig};
53//! use futures::StreamExt;
54//!
55//! let client = OpenRouterClient::new(
56//!     OpenRouterConfig::new(
57//!         std::env::var("OPENROUTER_API_KEY").unwrap(),
58//!         "openai/gpt-4.1-mini",
59//!     )
60//!     .with_http_referer("https://github.com/zavora-ai/adk-rust")
61//!     .with_title("ADK-Rust"),
62//! ).unwrap();
63//!
64//! let request = LlmRequest::new(
65//!     "openai/gpt-4.1-mini",
66//!     vec![Content::new("user").with_text("Reply in one short sentence.")],
67//! );
68//!
69//! # tokio_test::block_on(async {
70//! let mut stream = client.generate_content(request, true).await.unwrap();
71//! while let Some(item) = stream.next().await {
72//!     let _response = item.unwrap();
73//! }
74//! # });
75//! ```
76//!
77//! Native OpenRouter APIs such as model discovery, credits, routing configuration, plugins, and
78//! exact Responses payload access remain available on `OpenRouterClient` itself.
79//!
80//! ### Anthropic
81//!
82//! ```rust,ignore
83//! use adk_model::anthropic::{AnthropicClient, AnthropicConfig};
84//!
85//! let model = AnthropicClient::new(AnthropicConfig::new(
86//!     std::env::var("ANTHROPIC_API_KEY").unwrap(),
87//!     "claude-sonnet-4-5-20250929",
88//! )).unwrap();
89//! ```
90//!
91//! ### DeepSeek
92//!
93//! ```rust,ignore
94//! use adk_model::deepseek::{DeepSeekClient, DeepSeekConfig};
95//!
96//! // Chat model
97//! let chat = DeepSeekClient::chat(std::env::var("DEEPSEEK_API_KEY").unwrap()).unwrap();
98//!
99//! // Reasoner with thinking mode
100//! let reasoner = DeepSeekClient::reasoner(std::env::var("DEEPSEEK_API_KEY").unwrap()).unwrap();
101//! ```
102//!
103//! ### OpenAI-Compatible Providers (Fireworks, Together, Mistral, Perplexity, Cerebras, SambaNova, xAI)
104//!
105//! All OpenAI-compatible providers use `OpenAICompatible` with provider presets:
106//!
107//! ```rust,ignore
108//! use adk_model::openai_compatible::{OpenAICompatible, OpenAICompatibleConfig};
109//!
110//! // Fireworks AI
111//! let model = OpenAICompatible::new(OpenAICompatibleConfig::fireworks(
112//!     std::env::var("FIREWORKS_API_KEY").unwrap(),
113//!     "accounts/fireworks/models/llama-v3p1-8b-instruct",
114//! )).unwrap();
115//!
116//! // Together AI
117//! let model = OpenAICompatible::new(OpenAICompatibleConfig::together(
118//!     std::env::var("TOGETHER_API_KEY").unwrap(),
119//!     "meta-llama/Llama-3.3-70B-Instruct-Turbo",
120//! )).unwrap();
121//!
122//! // Or any custom OpenAI-compatible endpoint
123//! let model = OpenAICompatible::new(
124//!     OpenAICompatibleConfig::new("your-api-key", "your-model")
125//!         .with_base_url("https://your-endpoint.com/v1")
126//!         .with_provider_name("my-provider"),
127//! ).unwrap();
128//! ```
129//!
130//! ### Amazon Bedrock
131//!
132//! ```rust,ignore
133//! use adk_model::bedrock::{BedrockClient, BedrockConfig};
134//!
135//! // Uses AWS IAM credentials from the environment (no API key needed)
136//! let config = BedrockConfig::new("us-east-1", "anthropic.claude-sonnet-4-20250514-v1:0");
137//! let model = BedrockClient::new(config).await.unwrap();
138//! ```
139//!
140//! ### Azure AI Inference
141//!
142//! ```rust,ignore
143//! use adk_model::azure_ai::{AzureAIClient, AzureAIConfig};
144//!
145//! let model = AzureAIClient::new(AzureAIConfig::new(
146//!     "https://my-endpoint.eastus.inference.ai.azure.com",
147//!     std::env::var("AZURE_AI_API_KEY").unwrap(),
148//!     "meta-llama-3.1-8b-instruct",
149//! )).unwrap();
150//! ```
151//!
152//! ### Ollama (Local)
153//!
154//! ```rust,ignore
155//! use adk_model::ollama::{OllamaModel, OllamaConfig};
156//!
157//! // Default: localhost:11434
158//! let model = OllamaModel::new(OllamaConfig::new("llama3.2")).unwrap();
159//! ```
160//!
161//! ## Supported Models
162//!
163//! ### Gemini
164//! | Model | Description |
165//! |-------|-------------|
166//! | `gemini-3-pro-preview` | Most intelligent, complex agentic workflows (1M context) |
167//! | `gemini-3-flash-preview` | Frontier intelligence at Flash speed (1M context) |
168//! | `gemini-2.5-pro` | Advanced reasoning and multimodal (1M context) |
169//! | `gemini-2.5-flash` | Balanced speed and capability, recommended (1M context) |
170//! | `gemini-2.5-flash-lite` | Ultra-fast for high-volume tasks (1M context) |
171//!
172//! ### OpenAI
173//! | Model | Description |
174//! |-------|-------------|
175//! | `gpt-5` | Strongest coding and agentic model with adaptive reasoning |
176//! | `gpt-5-mini` | Efficient variant for most tasks |
177//! | `o3` | Advanced reasoning model for complex problem solving |
178//! | `o4-mini` | Efficient reasoning model (200K context) |
179//! | `gpt-4.1` | General purpose model with 1M context |
180//!
181//! ### Anthropic
182//! | Model | Description |
183//! |-------|-------------|
184//! | `claude-opus-4-5-20251101` | Most capable for complex autonomous tasks |
185//! | `claude-sonnet-4-5-20250929` | Best balance of intelligence, speed, and cost |
186//! | `claude-haiku-4-5-20251001` | Ultra-efficient for high-volume workloads |
187//! | `claude-opus-4-20250514` | Hybrid model with extended thinking |
188//! | `claude-sonnet-4-20250514` | Balanced model with extended thinking |
189//!
190//! ### DeepSeek
191//! | Model | Description |
192//! |-------|-------------|
193//! | `deepseek-chat` | V3.2 non-thinking mode for fast general-purpose tasks |
194//! | `deepseek-reasoner` | V3.2 thinking mode with chain-of-thought reasoning |
195//!
196//! ### Groq
197//! | Model | Description |
198//! |-------|-------------|
199//! | `meta-llama/llama-4-scout-17b-16e-instruct` | Llama 4 Scout via Groq LPU |
200//! | `llama-3.3-70b-versatile` | Versatile large model |
201//! | `llama-3.1-8b-instant` | Ultra-fast at 560 T/s |
202//!
203//! ### OpenAI-Compatible Providers (via `openai` feature)
204//!
205//! Use `OpenAICompatibleConfig` presets — one client, one feature flag:
206//!
207//! | Provider | Preset | Env Var |
208//! |----------|--------|---------|
209//! | Fireworks AI | `OpenAICompatibleConfig::fireworks()` | `FIREWORKS_API_KEY` |
210//! | Together AI | `OpenAICompatibleConfig::together()` | `TOGETHER_API_KEY` |
211//! | Mistral AI | `OpenAICompatibleConfig::mistral()` | `MISTRAL_API_KEY` |
212//! | Perplexity | `OpenAICompatibleConfig::perplexity()` | `PERPLEXITY_API_KEY` |
213//! | Cerebras | `OpenAICompatibleConfig::cerebras()` | `CEREBRAS_API_KEY` |
214//! | SambaNova | `OpenAICompatibleConfig::sambanova()` | `SAMBANOVA_API_KEY` |
215//! | xAI (Grok) | `OpenAICompatibleConfig::xai()` | `XAI_API_KEY` |
216//!
217//! ### Other Providers
218//!
219//! | Provider | Feature Flag | Env Var |
220//! |----------|-------------|---------|
221//! | Amazon Bedrock | `bedrock` | AWS IAM credentials |
222//! | Azure AI Inference | `azure-ai` | `AZURE_AI_API_KEY` |
223//! | OpenRouter | `openrouter` | `OPENROUTER_API_KEY` |
224//!
225//! ## Features
226//!
227//! - Async streaming with backpressure
228//! - Tool/function calling support
229//! - Multimodal input (text, images, audio, video, PDF)
230//! - Generation configuration (temperature, top_p, etc.)
231//! - OpenAI-compatible APIs (Ollama, vLLM, etc.)
232//!
233//! ## OpenRouter Scope Boundary
234//!
235//! `OpenRouterClient` exposes the provider's native surfaces:
236//!
237//! - chat completions
238//! - responses
239//! - model discovery and credits
240//! - provider routing and fallback
241//! - OpenRouter plugins and built-in tools
242//! - exact provider metadata and annotations
243//!
244//! The generic `Llm` adapter exists for agent compatibility and covers text generation,
245//! streaming, reasoning parts, tool/function calling, and OpenRouter request options through
246//! `openrouter::OpenRouterRequestOptions`.
247//!
248//! Use the native OpenRouter APIs whenever you need exact request or response parity, discovery,
249//! or provider-specific features that do not fit the generic `LlmRequest` / `LlmResponse`
250//! shape without information loss.
251//!
252//! For end-to-end agentic validation, see the local `examples/openrouter` crate and the
253//! `adk-model/examples/openrouter_*` binaries.
254
255#[cfg(feature = "anthropic")]
256pub mod anthropic;
257pub(crate) mod attachment;
258#[cfg(feature = "azure-ai")]
259pub mod azure_ai;
260#[cfg(feature = "bedrock")]
261pub mod bedrock;
262#[cfg(feature = "deepseek")]
263pub mod deepseek;
264#[cfg(feature = "gemini")]
265pub mod gemini;
266#[cfg(feature = "groq")]
267pub mod groq;
268pub mod mock;
269#[cfg(feature = "ollama")]
270pub mod ollama;
271#[cfg(feature = "openai")]
272pub mod openai;
273#[cfg(feature = "openai")]
274pub mod openai_compatible;
275#[cfg(feature = "openrouter")]
276pub mod openrouter;
277pub mod provider;
278pub mod retry;
279#[cfg(any(
280    feature = "openai",
281    feature = "ollama",
282    feature = "deepseek",
283    feature = "groq",
284    feature = "bedrock",
285    feature = "azure-ai"
286))]
287pub(crate) mod tool_result;
288pub mod usage_tracking;
289
290#[cfg(feature = "anthropic")]
291pub use anthropic::AnthropicClient;
292#[cfg(feature = "azure-ai")]
293pub use azure_ai::{AzureAIClient, AzureAIConfig};
294#[cfg(feature = "bedrock")]
295pub use bedrock::{BedrockClient, BedrockConfig};
296#[cfg(feature = "deepseek")]
297pub use deepseek::{DeepSeekClient, DeepSeekConfig};
298#[cfg(feature = "gemini")]
299pub use gemini::GeminiModel;
300#[cfg(feature = "groq")]
301pub use groq::{GroqClient, GroqConfig};
302pub use mock::MockLlm;
303#[cfg(feature = "ollama")]
304pub use ollama::{OllamaConfig, OllamaModel};
305#[cfg(feature = "openai")]
306pub use openai::{AzureConfig, AzureOpenAIClient, OpenAIClient, OpenAIConfig, ReasoningEffort};
307#[cfg(feature = "openai")]
308pub use openai_compatible::{OpenAICompatible, OpenAICompatibleConfig};
309#[cfg(feature = "openrouter")]
310pub use openrouter::{OpenRouterApiMode, OpenRouterClient, OpenRouterConfig};
311pub use provider::ModelProvider;
312pub use retry::RetryConfig;
313pub use retry::ServerRetryHint;