1use std::collections::BTreeMap;
19
20use serde::de::Error as _;
21use serde::{Deserialize, Serialize, Serializer};
22use serde_json::Value;
23
24use crate::completion::provider_options::{ExtensionOptions, ProviderExtension, ReplyExtras};
25use crate::message::Api;
26
27use super::ImageSize;
28use super::interactions_api::InteractionSpeechConfig;
29
30const GENERATE_CONTENT: &str = "gemini.generate_content";
32
33const INTERACTIONS: &str = "gemini.interactions";
35
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
38pub struct GeminiExt;
39
40impl ProviderExtension for GeminiExt {
41 const PROVIDER: &'static str = super::PROVIDER_NAME;
42 type Options = GeminiOptions;
43 type Extras = GeminiExtras;
44}
45
46fn is_default<T: Default + PartialEq>(value: &T) -> bool {
49 *value == T::default()
50}
51
52#[non_exhaustive]
61#[derive(Clone, Debug, Default, PartialEq, Serialize)]
62pub struct GeminiOptions {
63 #[serde(rename = "*")]
65 pub shared: GeminiShared,
66 #[serde(rename = "gemini.generate_content")]
68 pub generate_content: GenerateContentOptions,
69 #[serde(rename = "gemini.interactions")]
71 pub interactions: InteractionsOptions,
72}
73
74impl GeminiOptions {
75 pub fn new() -> Self {
77 Self::default()
78 }
79
80 pub fn store(mut self, store: bool) -> Self {
82 self.shared.store = Some(store);
83 self
84 }
85
86 pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
89 self.shared.labels.insert(key.into(), value.into());
90 self
91 }
92
93 pub fn generate_content(mut self, section: GenerateContentOptions) -> Self {
95 self.generate_content = section;
96 self
97 }
98
99 pub fn interactions(mut self, section: InteractionsOptions) -> Self {
101 self.interactions = section;
102 self
103 }
104
105 fn generation_config_entry(
107 mut self,
108 set: impl FnOnce(GenerationConfig) -> GenerationConfig,
109 ) -> Self {
110 let common = std::mem::take(&mut self.generate_content.generation_config.common);
111 self.generate_content.generation_config.common = set(common);
112 self
113 }
114
115 pub fn include_thoughts(self, include: bool) -> Self {
119 self.generation_config_entry(|config| config.include_thoughts(include))
120 }
121
122 pub fn top_k(self, top_k: u32) -> Self {
125 self.generation_config_entry(|config| config.top_k(top_k))
126 }
127
128 pub fn presence_penalty(self, penalty: f64) -> Self {
131 self.generation_config_entry(|config| config.presence_penalty(penalty))
132 }
133
134 pub fn frequency_penalty(self, penalty: f64) -> Self {
138 self.generation_config_entry(|config| config.frequency_penalty(penalty))
139 }
140
141 pub fn response_logprobs(self, enable: bool) -> Self {
145 self.generation_config_entry(|config| config.response_logprobs(enable))
146 }
147
148 pub fn logprobs(self, top: u32) -> Self {
151 self.generation_config_entry(|config| config.logprobs(top))
152 }
153
154 pub fn candidate_count(self, count: CandidateCount) -> Self {
157 self.generation_config_entry(|config| config.candidate_count(count))
158 }
159
160 pub fn response_modalities(
164 self,
165 modalities: impl IntoIterator<Item = ResponseModality>,
166 ) -> Self {
167 self.generation_config_entry(|config| config.response_modalities(modalities))
168 }
169
170 pub fn media_resolution(self, resolution: MediaResolution) -> Self {
174 self.generation_config_entry(|config| config.media_resolution(resolution))
175 }
176}
177
178impl ExtensionOptions for GeminiOptions {
179 type Ext = GeminiExt;
180}
181
182#[non_exhaustive]
184#[derive(Clone, Debug, Default, PartialEq, Serialize)]
185pub struct GeminiShared {
186 #[serde(skip_serializing_if = "Option::is_none")]
188 pub store: Option<bool>,
189 #[serde(skip_serializing_if = "BTreeMap::is_empty")]
192 pub labels: BTreeMap<String, String>,
193}
194
195#[non_exhaustive]
198#[derive(Clone, Debug, Default, PartialEq, Serialize)]
199pub struct GenerateContentOptions {
200 #[serde(rename = "generationConfig", skip_serializing_if = "is_default")]
203 pub generation_config: GeminiGenerationConfig,
204 #[serde(rename = "safetySettings", skip_serializing_if = "Vec::is_empty")]
206 pub safety_settings: Vec<SafetySetting>,
207}
208
209impl GenerateContentOptions {
210 pub fn new() -> Self {
212 Self::default()
213 }
214
215 pub fn generation_config(mut self, config: GenerationConfig) -> Self {
217 self.generation_config.common = config;
218 self
219 }
220
221 pub fn enable_enhanced_civic_answers(mut self, enable: bool) -> Self {
223 self.generation_config.enable_enhanced_civic_answers = Some(enable);
224 self
225 }
226
227 pub fn safety_setting(mut self, category: HarmCategory, threshold: HarmBlockThreshold) -> Self {
229 self.safety_settings
230 .push(SafetySetting::new(category, threshold));
231 self
232 }
233}
234
235#[non_exhaustive]
238#[derive(Clone, Debug, Default, PartialEq, Serialize)]
239pub struct GeminiGenerationConfig {
240 #[serde(flatten)]
242 pub common: GenerationConfig,
243 #[serde(
245 rename = "enableEnhancedCivicAnswers",
246 skip_serializing_if = "Option::is_none"
247 )]
248 pub enable_enhanced_civic_answers: Option<bool>,
249}
250
251#[non_exhaustive]
256#[derive(Clone, Debug, Default, PartialEq, Serialize)]
257#[serde(rename_all = "camelCase")]
258pub struct GenerationConfig {
259 #[serde(skip_serializing_if = "is_default")]
262 pub thinking_config: ThinkingConfig,
263 #[serde(skip_serializing_if = "Option::is_none")]
265 pub top_k: Option<u32>,
266 #[serde(skip_serializing_if = "Option::is_none")]
268 pub presence_penalty: Option<f64>,
269 #[serde(skip_serializing_if = "Option::is_none")]
271 pub frequency_penalty: Option<f64>,
272 #[serde(skip_serializing_if = "Option::is_none")]
274 pub response_logprobs: Option<bool>,
275 #[serde(skip_serializing_if = "Option::is_none")]
277 pub logprobs: Option<u32>,
278 #[serde(skip_serializing_if = "Option::is_none")]
280 pub candidate_count: Option<CandidateCount>,
281 #[serde(skip_serializing_if = "Vec::is_empty")]
283 pub response_modalities: Vec<ResponseModality>,
284 #[serde(skip_serializing_if = "Option::is_none")]
286 pub image_config: Option<ImageConfig>,
287 #[serde(skip_serializing_if = "Option::is_none")]
289 pub speech_config: Option<SpeechConfig>,
290 #[serde(skip_serializing_if = "Option::is_none")]
292 pub media_resolution: Option<MediaResolution>,
293}
294
295impl GenerationConfig {
296 pub fn new() -> Self {
298 Self::default()
299 }
300
301 pub fn include_thoughts(mut self, include: bool) -> Self {
303 self.thinking_config.include_thoughts = Some(include);
304 self
305 }
306
307 pub fn top_k(mut self, top_k: u32) -> Self {
309 self.top_k = Some(top_k);
310 self
311 }
312
313 pub fn presence_penalty(mut self, penalty: f64) -> Self {
315 self.presence_penalty = Some(penalty);
316 self
317 }
318
319 pub fn frequency_penalty(mut self, penalty: f64) -> Self {
321 self.frequency_penalty = Some(penalty);
322 self
323 }
324
325 pub fn response_logprobs(mut self, enable: bool) -> Self {
327 self.response_logprobs = Some(enable);
328 self
329 }
330
331 pub fn logprobs(mut self, top: u32) -> Self {
333 self.logprobs = Some(top);
334 self
335 }
336
337 pub fn candidate_count(mut self, count: CandidateCount) -> Self {
339 self.candidate_count = Some(count);
340 self
341 }
342
343 pub fn response_modalities(
345 mut self,
346 modalities: impl IntoIterator<Item = ResponseModality>,
347 ) -> Self {
348 self.response_modalities = modalities.into_iter().collect();
349 self
350 }
351
352 pub fn image_config(mut self, config: ImageConfig) -> Self {
354 self.image_config = Some(config);
355 self
356 }
357
358 pub fn speech_config(mut self, config: SpeechConfig) -> Self {
360 self.speech_config = Some(config);
361 self
362 }
363
364 pub fn media_resolution(mut self, resolution: MediaResolution) -> Self {
366 self.media_resolution = Some(resolution);
367 self
368 }
369}
370
371#[non_exhaustive]
373#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
374#[serde(rename_all = "camelCase")]
375pub struct ThinkingConfig {
376 #[serde(skip_serializing_if = "Option::is_none")]
378 pub include_thoughts: Option<bool>,
379}
380
381#[non_exhaustive]
384#[derive(Clone, Copy, Debug, PartialEq, Eq)]
385pub enum CandidateCount {
386 One,
388}
389
390impl Serialize for CandidateCount {
391 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
392 match self {
393 Self::One => serializer.serialize_u32(1),
394 }
395 }
396}
397
398#[non_exhaustive]
400#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
401#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
402pub enum ResponseModality {
403 Text,
405 Image,
407 Audio,
409}
410
411#[non_exhaustive]
413#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
414pub enum MediaResolution {
415 #[serde(rename = "MEDIA_RESOLUTION_LOW")]
417 Low,
418 #[serde(rename = "MEDIA_RESOLUTION_MEDIUM")]
420 Medium,
421 #[serde(rename = "MEDIA_RESOLUTION_HIGH")]
423 High,
424}
425
426#[non_exhaustive]
428#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
429#[serde(rename_all = "camelCase")]
430pub struct ImageConfig {
431 #[serde(skip_serializing_if = "Option::is_none")]
433 pub aspect_ratio: Option<String>,
434 #[serde(skip_serializing_if = "Option::is_none")]
436 pub image_size: Option<ImageSize>,
437}
438
439impl ImageConfig {
440 pub fn new() -> Self {
442 Self::default()
443 }
444
445 pub fn aspect_ratio(mut self, ratio: impl Into<String>) -> Self {
447 self.aspect_ratio = Some(ratio.into());
448 self
449 }
450
451 pub fn image_size(mut self, size: ImageSize) -> Self {
453 self.image_size = Some(size);
454 self
455 }
456}
457
458#[non_exhaustive]
460#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
461#[serde(rename_all = "camelCase")]
462pub struct SpeechConfig {
463 #[serde(skip_serializing_if = "Option::is_none")]
465 pub voice_config: Option<VoiceConfig>,
466 #[serde(skip_serializing_if = "Option::is_none")]
468 pub multi_speaker_voice_config: Option<MultiSpeakerVoiceConfig>,
469 #[serde(skip_serializing_if = "Option::is_none")]
471 pub language_code: Option<String>,
472}
473
474impl SpeechConfig {
475 pub fn voice(voice_name: impl Into<String>) -> Self {
477 Self {
478 voice_config: Some(VoiceConfig::prebuilt(voice_name)),
479 multi_speaker_voice_config: None,
480 language_code: None,
481 }
482 }
483
484 pub fn speakers<S: Into<String>, V: Into<String>>(
487 speakers: impl IntoIterator<Item = (S, V)>,
488 ) -> Self {
489 let speaker_voice_configs = speakers
490 .into_iter()
491 .map(|(speaker, voice)| SpeakerVoiceConfig {
492 speaker: speaker.into(),
493 voice_config: VoiceConfig::prebuilt(voice),
494 })
495 .collect();
496 Self {
497 voice_config: None,
498 multi_speaker_voice_config: Some(MultiSpeakerVoiceConfig {
499 speaker_voice_configs,
500 }),
501 language_code: None,
502 }
503 }
504
505 pub fn language_code(mut self, code: impl Into<String>) -> Self {
507 self.language_code = Some(code.into());
508 self
509 }
510}
511
512#[non_exhaustive]
514#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
515#[serde(rename_all = "camelCase")]
516pub struct VoiceConfig {
517 pub prebuilt_voice_config: PrebuiltVoiceConfig,
519}
520
521impl VoiceConfig {
522 pub fn prebuilt(voice_name: impl Into<String>) -> Self {
524 Self {
525 prebuilt_voice_config: PrebuiltVoiceConfig {
526 voice_name: voice_name.into(),
527 },
528 }
529 }
530}
531
532#[non_exhaustive]
534#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
535#[serde(rename_all = "camelCase")]
536pub struct PrebuiltVoiceConfig {
537 pub voice_name: String,
539}
540
541#[non_exhaustive]
543#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
544#[serde(rename_all = "camelCase")]
545pub struct MultiSpeakerVoiceConfig {
546 pub speaker_voice_configs: Vec<SpeakerVoiceConfig>,
548}
549
550#[non_exhaustive]
552#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
553#[serde(rename_all = "camelCase")]
554pub struct SpeakerVoiceConfig {
555 pub speaker: String,
557 pub voice_config: VoiceConfig,
559}
560
561#[non_exhaustive]
564#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
565pub enum HarmCategory {
566 #[serde(rename = "HARM_CATEGORY_HATE_SPEECH")]
568 HateSpeech,
569 #[serde(rename = "HARM_CATEGORY_DANGEROUS_CONTENT")]
571 DangerousContent,
572 #[serde(rename = "HARM_CATEGORY_HARASSMENT")]
574 Harassment,
575 #[serde(rename = "HARM_CATEGORY_SEXUALLY_EXPLICIT")]
577 SexuallyExplicit,
578 #[serde(rename = "HARM_CATEGORY_CIVIC_INTEGRITY")]
580 CivicIntegrity,
581 #[serde(rename = "HARM_CATEGORY_JAILBREAK")]
583 Jailbreak,
584}
585
586impl HarmCategory {
587 fn interactions(self) -> &'static str {
589 match self {
590 Self::HateSpeech => "hate_speech",
591 Self::DangerousContent => "dangerous_content",
592 Self::Harassment => "harassment",
593 Self::SexuallyExplicit => "sexually_explicit",
594 Self::CivicIntegrity => "civic_integrity",
595 Self::Jailbreak => "jailbreak",
596 }
597 }
598}
599
600#[non_exhaustive]
603#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
604#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
605pub enum HarmBlockThreshold {
606 BlockLowAndAbove,
608 BlockMediumAndAbove,
610 BlockOnlyHigh,
612 BlockNone,
614 Off,
616}
617
618impl HarmBlockThreshold {
619 fn interactions(self) -> &'static str {
621 match self {
622 Self::BlockLowAndAbove => "block_low_and_above",
623 Self::BlockMediumAndAbove => "block_medium_and_above",
624 Self::BlockOnlyHigh => "block_only_high",
625 Self::BlockNone => "block_none",
626 Self::Off => "off",
627 }
628 }
629}
630
631#[non_exhaustive]
636#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
637pub struct SafetySetting {
638 pub category: HarmCategory,
640 pub threshold: HarmBlockThreshold,
642}
643
644impl SafetySetting {
645 pub fn new(category: HarmCategory, threshold: HarmBlockThreshold) -> Self {
647 Self {
648 category,
649 threshold,
650 }
651 }
652}
653
654fn interactions_safety<S: Serializer>(
656 settings: &[SafetySetting],
657 serializer: S,
658) -> Result<S::Ok, S::Error> {
659 serializer.collect_seq(settings.iter().map(|setting| {
660 BTreeMap::from([
661 ("type", setting.category.interactions()),
662 ("threshold", setting.threshold.interactions()),
663 ])
664 }))
665}
666
667#[non_exhaustive]
670#[derive(Clone, Debug, Default, PartialEq, Serialize)]
671pub struct InteractionsOptions {
672 #[serde(skip_serializing_if = "Option::is_none")]
675 pub agent: Option<String>,
676 #[serde(skip_serializing_if = "Option::is_none")]
678 pub agent_config: Option<AgentConfig>,
679 #[serde(skip_serializing_if = "Option::is_none")]
681 pub background: Option<bool>,
682 #[serde(skip_serializing_if = "Option::is_none")]
684 pub previous_interaction_id: Option<String>,
685 #[serde(
687 skip_serializing_if = "Vec::is_empty",
688 serialize_with = "interactions_safety"
689 )]
690 pub safety_settings: Vec<SafetySetting>,
691 #[serde(skip_serializing_if = "is_default")]
694 pub generation_config: InteractionsGenerationConfig,
695}
696
697impl InteractionsOptions {
698 pub fn new() -> Self {
700 Self::default()
701 }
702
703 pub fn agent(mut self, agent: impl Into<String>) -> Self {
705 self.agent = Some(agent.into());
706 self
707 }
708
709 pub fn agent_config(mut self, config: AgentConfig) -> Self {
711 self.agent_config = Some(config);
712 self
713 }
714
715 pub fn background(mut self, background: bool) -> Self {
717 self.background = Some(background);
718 self
719 }
720
721 pub fn previous_interaction_id(mut self, id: impl Into<String>) -> Self {
723 self.previous_interaction_id = Some(id.into());
724 self
725 }
726
727 pub fn safety_setting(mut self, category: HarmCategory, threshold: HarmBlockThreshold) -> Self {
729 self.safety_settings
730 .push(SafetySetting::new(category, threshold));
731 self
732 }
733
734 pub fn thinking_summaries(mut self, summaries: ThinkingSummaries) -> Self {
736 self.generation_config.thinking_summaries = Some(summaries);
737 self
738 }
739
740 pub fn speech_config(mut self, config: InteractionSpeechConfig) -> Self {
742 self.generation_config.speech_config = Some(config);
743 self
744 }
745}
746
747#[non_exhaustive]
749#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
750pub struct InteractionsGenerationConfig {
751 #[serde(skip_serializing_if = "Option::is_none")]
753 pub thinking_summaries: Option<ThinkingSummaries>,
754 #[serde(skip_serializing_if = "Option::is_none")]
756 pub speech_config: Option<InteractionSpeechConfig>,
757}
758
759#[non_exhaustive]
761#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
762#[serde(rename_all = "snake_case")]
763pub enum ThinkingSummaries {
764 Auto,
766 None,
768}
769
770#[non_exhaustive]
772#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
773#[serde(tag = "type", rename_all = "kebab-case")]
774pub enum AgentConfig {
775 Dynamic,
777 DeepResearch(DeepResearchConfig),
779}
780
781#[non_exhaustive]
784#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
785pub struct DeepResearchConfig {
786 #[serde(skip_serializing_if = "Option::is_none")]
788 pub thinking_summaries: Option<ThinkingSummaries>,
789 #[serde(skip_serializing_if = "Option::is_none")]
791 pub visualization: Option<Visualization>,
792 #[serde(skip_serializing_if = "Option::is_none")]
795 pub collaborative_planning: Option<bool>,
796}
797
798impl DeepResearchConfig {
799 pub fn new() -> Self {
801 Self::default()
802 }
803
804 pub fn thinking_summaries(mut self, summaries: ThinkingSummaries) -> Self {
806 self.thinking_summaries = Some(summaries);
807 self
808 }
809
810 pub fn visualization(mut self, visualization: Visualization) -> Self {
812 self.visualization = Some(visualization);
813 self
814 }
815
816 pub fn collaborative_planning(mut self, plan: bool) -> Self {
818 self.collaborative_planning = Some(plan);
819 self
820 }
821}
822
823impl From<DeepResearchConfig> for AgentConfig {
824 fn from(config: DeepResearchConfig) -> Self {
825 Self::DeepResearch(config)
826 }
827}
828
829#[non_exhaustive]
831#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
832#[serde(rename_all = "snake_case")]
833pub enum Visualization {
834 Auto,
836 Off,
838}
839
840#[non_exhaustive]
845#[derive(Clone, Debug, Default, PartialEq)]
846pub struct GeminiExtras {
847 pub model_version: Option<String>,
849 pub response_id: Option<String>,
851 pub service_tier: Option<String>,
854 pub prompt_tokens_details: Option<Vec<ModalityTokenCount>>,
856 pub cache_tokens_details: Option<Vec<ModalityTokenCount>>,
858 pub candidates_tokens_details: Option<Vec<ModalityTokenCount>>,
860 pub tool_use_prompt_tokens_details: Option<Vec<ModalityTokenCount>>,
862 pub prompt_feedback: Option<PromptFeedback>,
864 pub safety_ratings: Option<Vec<SafetyRating>>,
866 pub finish_message: Option<String>,
868 pub citation_metadata: Option<CitationMetadata>,
870 pub grounding_metadata: Option<GroundingMetadata>,
872 pub url_context_metadata: Option<UrlContextMetadata>,
874 pub avg_logprobs: Option<f64>,
876 pub logprobs_result: Option<LogprobsResult>,
878 pub id: Option<String>,
880 pub status: Option<InteractionStatus>,
882 pub created: Option<String>,
884 pub updated: Option<String>,
886 pub input_tokens_by_modality: Option<Vec<ModalityTokens>>,
888 pub output_tokens_by_modality: Option<Vec<ModalityTokens>>,
890 pub cached_tokens_by_modality: Option<Vec<ModalityTokens>>,
892 pub grounding_tool_count: Option<Vec<GroundingToolCount>>,
894}
895
896impl ReplyExtras for GeminiExtras {
897 fn from_reply(api: &Api, raw: &Value) -> Result<Self, serde_json::Error> {
898 match api.as_str() {
899 GENERATE_CONTENT => {
900 Ok(crate::json_utils::from_value::<GenerateContentReply>(raw)?.into())
901 }
902 INTERACTIONS => Ok(crate::json_utils::from_value::<InteractionReply>(raw)?.into()),
903 other => Err(serde_json::Error::custom(format!(
904 "the Gemini API returns no `{other}` reply"
905 ))),
906 }
907 }
908}
909
910#[derive(Deserialize)]
912#[serde(rename_all = "camelCase")]
913struct GenerateContentReply {
914 model_version: Option<String>,
915 response_id: Option<String>,
916 usage_metadata: Option<UsageDetails>,
917 prompt_feedback: Option<PromptFeedback>,
918 #[serde(default)]
919 candidates: Vec<CandidateDetails>,
920}
921
922#[derive(Deserialize)]
923#[serde(rename_all = "camelCase")]
924struct UsageDetails {
925 service_tier: Option<String>,
926 prompt_tokens_details: Option<Vec<ModalityTokenCount>>,
927 cache_tokens_details: Option<Vec<ModalityTokenCount>>,
928 candidates_tokens_details: Option<Vec<ModalityTokenCount>>,
929 tool_use_prompt_tokens_details: Option<Vec<ModalityTokenCount>>,
930}
931
932#[derive(Deserialize)]
933#[serde(rename_all = "camelCase")]
934struct CandidateDetails {
935 safety_ratings: Option<Vec<SafetyRating>>,
936 finish_message: Option<String>,
937 citation_metadata: Option<CitationMetadata>,
938 grounding_metadata: Option<GroundingMetadata>,
939 url_context_metadata: Option<UrlContextMetadata>,
940 avg_logprobs: Option<f64>,
941 logprobs_result: Option<LogprobsResult>,
942}
943
944impl From<GenerateContentReply> for GeminiExtras {
945 fn from(reply: GenerateContentReply) -> Self {
946 let mut extras = Self {
947 model_version: reply.model_version,
948 response_id: reply.response_id,
949 prompt_feedback: reply.prompt_feedback,
950 ..Self::default()
951 };
952 if let Some(usage) = reply.usage_metadata {
953 extras.service_tier = usage.service_tier;
954 extras.prompt_tokens_details = listed(usage.prompt_tokens_details);
955 extras.cache_tokens_details = listed(usage.cache_tokens_details);
956 extras.candidates_tokens_details = listed(usage.candidates_tokens_details);
957 extras.tool_use_prompt_tokens_details = listed(usage.tool_use_prompt_tokens_details);
958 }
959 if let Some(candidate) = reply.candidates.into_iter().next() {
960 extras.safety_ratings = listed(candidate.safety_ratings);
961 extras.finish_message = candidate.finish_message;
962 extras.citation_metadata = candidate.citation_metadata;
963 extras.grounding_metadata = candidate.grounding_metadata;
964 extras.url_context_metadata = candidate.url_context_metadata;
965 extras.avg_logprobs = candidate.avg_logprobs;
966 extras.logprobs_result = candidate.logprobs_result;
967 }
968 extras
969 }
970}
971
972fn listed<T>(items: Option<Vec<T>>) -> Option<Vec<T>> {
975 items.filter(|items| !items.is_empty())
976}
977
978#[derive(Deserialize)]
980struct InteractionReply {
981 id: Option<String>,
982 status: Option<InteractionStatus>,
983 service_tier: Option<String>,
984 created: Option<String>,
985 updated: Option<String>,
986 usage: Option<InteractionUsage>,
987}
988
989#[derive(Deserialize)]
990struct InteractionUsage {
991 input_tokens_by_modality: Option<Vec<ModalityTokens>>,
992 output_tokens_by_modality: Option<Vec<ModalityTokens>>,
993 cached_tokens_by_modality: Option<Vec<ModalityTokens>>,
994 grounding_tool_count: Option<Vec<GroundingToolCount>>,
995}
996
997impl From<InteractionReply> for GeminiExtras {
998 fn from(reply: InteractionReply) -> Self {
999 let mut extras = Self {
1000 id: reply.id,
1001 status: reply.status,
1002 service_tier: reply.service_tier,
1003 created: reply.created,
1004 updated: reply.updated,
1005 ..Self::default()
1006 };
1007 if let Some(usage) = reply.usage {
1008 extras.input_tokens_by_modality = listed(usage.input_tokens_by_modality);
1009 extras.output_tokens_by_modality = listed(usage.output_tokens_by_modality);
1010 extras.cached_tokens_by_modality = listed(usage.cached_tokens_by_modality);
1011 extras.grounding_tool_count = listed(usage.grounding_tool_count);
1012 }
1013 extras
1014 }
1015}
1016
1017#[non_exhaustive]
1019#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1020#[serde(rename_all = "camelCase")]
1021pub struct ModalityTokenCount {
1022 pub modality: Option<String>,
1024 pub token_count: Option<u64>,
1026}
1027
1028#[non_exhaustive]
1030#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1031#[serde(rename_all = "camelCase")]
1032pub struct PromptFeedback {
1033 pub block_reason: Option<String>,
1035 pub safety_ratings: Option<Vec<SafetyRating>>,
1037}
1038
1039#[non_exhaustive]
1041#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1042#[serde(rename_all = "camelCase")]
1043pub struct SafetyRating {
1044 pub category: Option<String>,
1046 pub probability: Option<String>,
1048 pub blocked: Option<bool>,
1050}
1051
1052#[non_exhaustive]
1054#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1055#[serde(rename_all = "camelCase")]
1056pub struct CitationMetadata {
1057 pub citation_sources: Option<Vec<CitationSource>>,
1059}
1060
1061#[non_exhaustive]
1063#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1064#[serde(rename_all = "camelCase")]
1065pub struct CitationSource {
1066 pub start_index: Option<u64>,
1068 pub end_index: Option<u64>,
1070 pub uri: Option<String>,
1072 pub license: Option<String>,
1074}
1075
1076#[non_exhaustive]
1078#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
1079#[serde(rename_all = "camelCase")]
1080pub struct GroundingMetadata {
1081 pub web_search_queries: Option<Vec<String>>,
1083 pub search_entry_point: Option<SearchEntryPoint>,
1085 pub grounding_chunks: Option<Vec<GroundingChunk>>,
1087 pub grounding_supports: Option<Vec<GroundingSupport>>,
1089}
1090
1091#[non_exhaustive]
1093#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1094#[serde(rename_all = "camelCase")]
1095pub struct SearchEntryPoint {
1096 pub rendered_content: Option<String>,
1098}
1099
1100#[non_exhaustive]
1102#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1103#[serde(rename_all = "camelCase")]
1104pub struct GroundingChunk {
1105 pub web: Option<WebChunk>,
1107 pub retrieved_context: Option<RetrievedContext>,
1109}
1110
1111#[non_exhaustive]
1113#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1114pub struct WebChunk {
1115 pub uri: Option<String>,
1117 pub title: Option<String>,
1119}
1120
1121#[non_exhaustive]
1123#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1124pub struct RetrievedContext {
1125 pub uri: Option<String>,
1127 pub title: Option<String>,
1129 pub text: Option<String>,
1131}
1132
1133#[non_exhaustive]
1135#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
1136#[serde(rename_all = "camelCase")]
1137pub struct GroundingSupport {
1138 pub segment: Option<Segment>,
1140 pub grounding_chunk_indices: Option<Vec<u32>>,
1142 pub confidence_scores: Option<Vec<f64>>,
1144}
1145
1146#[non_exhaustive]
1148#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1149#[serde(rename_all = "camelCase")]
1150pub struct Segment {
1151 pub part_index: Option<u32>,
1153 pub start_index: Option<u64>,
1155 pub end_index: Option<u64>,
1157 pub text: Option<String>,
1159}
1160
1161#[non_exhaustive]
1163#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1164#[serde(rename_all = "camelCase")]
1165pub struct UrlContextMetadata {
1166 pub url_metadata: Option<Vec<UrlMetadata>>,
1168}
1169
1170#[non_exhaustive]
1172#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1173#[serde(rename_all = "camelCase")]
1174pub struct UrlMetadata {
1175 pub retrieved_url: Option<String>,
1177 pub url_retrieval_status: Option<String>,
1179}
1180
1181#[non_exhaustive]
1183#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
1184#[serde(rename_all = "camelCase")]
1185pub struct LogprobsResult {
1186 pub top_candidates: Option<Vec<TopCandidates>>,
1188 pub chosen_candidates: Option<Vec<LogprobsCandidate>>,
1190 pub log_probability_sum: Option<f64>,
1192}
1193
1194#[non_exhaustive]
1196#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
1197pub struct TopCandidates {
1198 pub candidates: Option<Vec<LogprobsCandidate>>,
1200}
1201
1202#[non_exhaustive]
1204#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
1205#[serde(rename_all = "camelCase")]
1206pub struct LogprobsCandidate {
1207 pub token: Option<String>,
1209 pub token_id: Option<i64>,
1211 pub log_probability: Option<f64>,
1213}
1214
1215#[non_exhaustive]
1217#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
1218#[serde(rename_all = "snake_case")]
1219pub enum InteractionStatus {
1220 InProgress,
1222 RequiresAction,
1224 Incomplete,
1226 BudgetExceeded,
1228 Completed,
1230 Failed,
1232 Cancelled,
1234 Queued,
1236 #[serde(untagged)]
1238 Unknown(String),
1239}
1240
1241#[non_exhaustive]
1243#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1244pub struct ModalityTokens {
1245 pub modality: Option<String>,
1247 pub tokens: Option<u64>,
1249}
1250
1251#[non_exhaustive]
1253#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize)]
1254pub struct GroundingToolCount {
1255 #[serde(rename = "type")]
1257 pub kind: Option<String>,
1258 pub count: Option<u64>,
1260 pub search_query_count: Option<u64>,
1262}
1263
1264#[cfg(test)]
1265mod tests;