rig_core/providers/gemini/
mod.rs1#[cfg(feature = "audio")]
17#[cfg_attr(docsrs, doc(cfg(feature = "audio")))]
18pub mod audio_generation;
19pub mod cached_content;
20pub mod caching;
21pub mod completion;
22pub mod embedding;
23pub mod extension;
24#[cfg(feature = "image")]
25#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
26pub mod image_generation;
27pub mod interactions_api;
28pub mod model_listing;
29mod options;
30pub mod streaming;
31pub mod transcription;
32
33pub use crate::client::gemini::Gemini;
34pub use crate::client::gemini_caching::Caching;
35#[cfg(feature = "audio")]
36pub use audio_generation::{GEMINI_3_8_FLASH_LITE_TTS, GEMINI_3_8_FLASH_TTS};
37pub use cached_content::{CacheExpiry, CachedContent, CachedContents, NewCachedContent};
38pub use caching::{AutoCache, CacheBook, CacheEvent, CacheReport, Clock, CreatedCache, Lease};
39pub use completion::ThoughtReplay;
40pub use embedding::{EMBEDDING_001, EMBEDDING_004};
41#[cfg(feature = "image")]
42pub use image_generation::{
43 GEMINI_2_5_FLASH_IMAGE, GEMINI_3_1_FLASH_IMAGE, GEMINI_3_1_FLASH_LITE_IMAGE,
44 GEMINI_3_PRO_IMAGE, GEMINI_NANO_BANANA_2_1,
45};
46pub use interactions_api::{DEEP_RESEARCH_MAX_PREVIEW_04_2026, DEEP_RESEARCH_PREVIEW_04_2026};
47pub use model_listing::*;
48
49use crate::client::env::{self, EnvError};
50use crate::wire::Secret;
51
52pub use completion::PROVIDER_NAME;
55
56pub const BASE_URL: &str = "https://generativelanguage.googleapis.com";
58
59pub const API_KEY_ENV: &str = "GEMINI_API_KEY";
61
62#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
66#[serde(deny_unknown_fields)]
67pub struct GeminiConfig {
68 pub api_key: Secret,
70 pub base_url: String,
72}
73
74impl GeminiConfig {
75 pub fn new(api_key: impl Into<Secret>) -> Self {
77 Self {
78 api_key: api_key.into(),
79 base_url: BASE_URL.to_owned(),
80 }
81 }
82
83 pub fn from_env() -> Result<Self, EnvError> {
85 Ok(Self::new(env::required(API_KEY_ENV)?))
86 }
87
88 pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
90 self.base_url = base_url.into();
91 self
92 }
93
94 pub(crate) fn uri(&self, path: &str) -> String {
97 let trimmed = path.trim_start_matches('/');
98 let separator = if trimmed.contains('?') { "&" } else { "?" };
99 let base = self.base_url.trim_end_matches('/');
100 format!("{base}/{trimmed}{separator}key={}", self.api_key.expose())
101 }
102
103 pub(crate) fn interactions_uri(&self, path: &str) -> String {
106 let base = self.base_url.trim_end_matches('/');
107 format!("{base}/{}", path.trim_start_matches('/'))
108 }
109
110 pub(crate) const INTERACTIONS_KEY_HEADER: &'static str = "x-goog-api-key";
112
113 pub(crate) fn completion(&self, model: impl Into<String>) -> completion::GenerateContent {
115 completion::GenerateContent::new(self.clone(), model)
116 }
117
118 pub(crate) fn interactions(&self, model: impl Into<String>) -> interactions_api::Interactions {
120 interactions_api::Interactions::new(self.clone(), model)
121 }
122}
123
124#[non_exhaustive]
127#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
128pub enum ImageSize {
129 #[serde(rename = "512")]
131 FiveTwelve,
132 #[serde(rename = "1K")]
134 OneK,
135 #[serde(rename = "2K")]
137 TwoK,
138 #[serde(rename = "4K")]
140 FourK,
141}
142
143#[cfg(test)]
144mod tests;