1use reqwest::Url;
2use serde::{Deserialize, Serialize, de};
3use time::OffsetDateTime;
4
5use crate::{
6 Content, Modality, Part,
7 safety::{SafetyRating, SafetySetting},
8};
9
10#[derive(Debug, Clone, Serialize, PartialEq)]
15#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
16#[non_exhaustive]
17pub enum FinishReason {
18 FinishReasonUnspecified,
20 Stop,
22 MaxTokens,
24 Safety,
26 Recitation,
28 Language,
30 Other,
32 Blocklist,
34 ProhibitedContent,
36 Spii,
38 MalformedFunctionCall,
40 ModelArmor,
42 ImageSafety,
44 UnexpectedToolCall,
46 TooManyToolCalls,
48 MissingThoughtSignature,
50 MalformedResponse,
52 Continuation,
55}
56
57impl FinishReason {
58 fn from_wire_str(value: &str) -> Self {
59 match value {
60 "FINISH_REASON_UNSPECIFIED" => Self::FinishReasonUnspecified,
61 "STOP" => Self::Stop,
62 "MAX_TOKENS" => Self::MaxTokens,
63 "SAFETY" => Self::Safety,
64 "RECITATION" => Self::Recitation,
65 "LANGUAGE" => Self::Language,
66 "OTHER" => Self::Other,
67 "BLOCKLIST" => Self::Blocklist,
68 "PROHIBITED_CONTENT" => Self::ProhibitedContent,
69 "SPII" => Self::Spii,
70 "MALFORMED_FUNCTION_CALL" => Self::MalformedFunctionCall,
71 "MODEL_ARMOR" => Self::ModelArmor,
72 "IMAGE_SAFETY" => Self::ImageSafety,
73 "UNEXPECTED_TOOL_CALL" => Self::UnexpectedToolCall,
74 "TOO_MANY_TOOL_CALLS" => Self::TooManyToolCalls,
75 "MISSING_THOUGHT_SIGNATURE" => Self::MissingThoughtSignature,
76 "MALFORMED_RESPONSE" => Self::MalformedResponse,
77 "CONTINUATION" => Self::Continuation,
78 _ => Self::Other,
79 }
80 }
81
82 fn from_wire_number(value: i64) -> Self {
83 match value {
84 0 => Self::FinishReasonUnspecified,
85 1 => Self::Stop,
86 2 => Self::MaxTokens,
87 3 => Self::Safety,
88 4 => Self::Recitation,
89 5 => Self::Other,
90 6 => Self::Blocklist,
91 7 => Self::ProhibitedContent,
92 8 => Self::Spii,
93 9 => Self::MalformedFunctionCall,
94 10 => Self::ModelArmor,
95 _ => Self::Other,
96 }
97 }
98}
99
100impl<'de> Deserialize<'de> for FinishReason {
101 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
102 where
103 D: serde::Deserializer<'de>,
104 {
105 let value = serde_json::Value::deserialize(deserializer)?;
106 match value {
107 serde_json::Value::String(s) => Ok(Self::from_wire_str(&s)),
108 serde_json::Value::Number(n) => {
109 n.as_i64().map(Self::from_wire_number).ok_or_else(|| {
110 de::Error::custom("finishReason must be an integer-compatible number")
111 })
112 }
113 _ => Err(de::Error::custom("finishReason must be a string or integer")),
114 }
115 }
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
120#[serde(rename_all = "camelCase")]
121pub struct CitationMetadata {
122 #[serde(default)]
124 pub citation_sources: Vec<CitationSource>,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
129#[serde(rename_all = "camelCase")]
130pub struct CitationSource {
131 pub uri: Option<String>,
133 pub title: Option<String>,
135 pub start_index: Option<i32>,
137 pub end_index: Option<i32>,
139 pub license: Option<String>,
141 #[serde(default, with = "time::serde::rfc3339::option")]
143 pub publication_date: Option<OffsetDateTime>,
144}
145
146#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
148#[serde(rename_all = "camelCase")]
149pub struct Candidate {
150 #[serde(default)]
152 pub content: Content,
153 #[serde(skip_serializing_if = "Option::is_none")]
155 pub safety_ratings: Option<Vec<SafetyRating>>,
156 #[serde(skip_serializing_if = "Option::is_none")]
158 pub citation_metadata: Option<CitationMetadata>,
159 #[serde(skip_serializing_if = "Option::is_none")]
161 pub grounding_metadata: Option<GroundingMetadata>,
162 #[serde(skip_serializing_if = "Option::is_none")]
164 pub finish_reason: Option<FinishReason>,
165 #[serde(skip_serializing_if = "Option::is_none")]
167 pub index: Option<i32>,
168 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub logprobs_result: Option<LogprobsResult>,
172 #[serde(default, skip_serializing_if = "Option::is_none")]
174 pub avg_logprobs: Option<f64>,
175}
176
177#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
193#[serde(rename_all = "camelCase")]
194pub struct LogprobsResult {
195 #[serde(default, skip_serializing_if = "Vec::is_empty")]
197 pub top_candidates: Vec<TopLogprobsCandidates>,
198 #[serde(default, skip_serializing_if = "Vec::is_empty")]
200 pub chosen_candidates: Vec<LogprobsCandidate>,
201 #[serde(default, skip_serializing_if = "Option::is_none")]
203 pub log_probability_sum: Option<f64>,
204}
205
206#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
208#[serde(rename_all = "camelCase")]
209pub struct TopLogprobsCandidates {
210 #[serde(default, skip_serializing_if = "Vec::is_empty")]
212 pub candidates: Vec<LogprobsCandidate>,
213}
214
215#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
217#[serde(rename_all = "camelCase")]
218pub struct LogprobsCandidate {
219 #[serde(default, skip_serializing_if = "Option::is_none")]
221 pub token: Option<String>,
222 #[serde(default, skip_serializing_if = "Option::is_none")]
224 pub token_id: Option<i32>,
225 #[serde(default, skip_serializing_if = "Option::is_none")]
227 pub log_probability: Option<f64>,
228}
229
230#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
251#[serde(rename_all = "camelCase", try_from = "serde_json::Value")]
252pub struct UsageMetadata {
253 #[serde(skip_serializing_if = "Option::is_none")]
255 pub prompt_token_count: Option<i32>,
256 #[serde(skip_serializing_if = "Option::is_none")]
258 pub candidates_token_count: Option<i32>,
259 #[serde(skip_serializing_if = "Option::is_none")]
261 pub total_token_count: Option<i32>,
262 #[serde(skip_serializing_if = "Option::is_none")]
264 pub thoughts_token_count: Option<i32>,
265 #[serde(skip_serializing_if = "Option::is_none")]
267 pub prompt_tokens_details: Option<Vec<PromptTokenDetails>>,
268 #[serde(skip_serializing_if = "Option::is_none")]
270 pub cached_content_token_count: Option<i32>,
271 #[serde(skip_serializing_if = "Option::is_none")]
273 pub cache_tokens_details: Option<Vec<PromptTokenDetails>>,
274 #[serde(skip)]
279 pub raw: Option<serde_json::Value>,
280}
281
282#[derive(Deserialize)]
283#[serde(rename_all = "camelCase")]
284struct UsageMetadataWire {
285 #[serde(default)]
286 prompt_token_count: Option<i32>,
287 #[serde(default)]
288 candidates_token_count: Option<i32>,
289 #[serde(default)]
290 total_token_count: Option<i32>,
291 #[serde(default)]
292 thoughts_token_count: Option<i32>,
293 #[serde(default)]
294 prompt_tokens_details: Option<Vec<PromptTokenDetails>>,
295 #[serde(default)]
296 cached_content_token_count: Option<i32>,
297 #[serde(default)]
298 cache_tokens_details: Option<Vec<PromptTokenDetails>>,
299}
300
301impl TryFrom<serde_json::Value> for UsageMetadata {
302 type Error = serde_json::Error;
303
304 fn try_from(raw: serde_json::Value) -> Result<Self, Self::Error> {
305 let wire = UsageMetadataWire::deserialize(&raw)?;
306 Ok(Self {
307 prompt_token_count: wire.prompt_token_count,
308 candidates_token_count: wire.candidates_token_count,
309 total_token_count: wire.total_token_count,
310 thoughts_token_count: wire.thoughts_token_count,
311 prompt_tokens_details: wire.prompt_tokens_details,
312 cached_content_token_count: wire.cached_content_token_count,
313 cache_tokens_details: wire.cache_tokens_details,
314 raw: Some(raw),
315 })
316 }
317}
318
319#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
321#[serde(rename_all = "camelCase")]
322pub struct PromptTokenDetails {
323 #[serde(default = "unspecified_modality")]
325 pub modality: Modality,
326 #[serde(default, deserialize_with = "adk_core::nullable_count")]
328 pub token_count: i32,
329}
330
331fn unspecified_modality() -> Modality {
332 Modality::ModalityUnspecified
333}
334
335#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
337#[serde(rename_all = "camelCase")]
338pub struct GroundingMetadata {
339 #[serde(skip_serializing_if = "Option::is_none")]
341 pub grounding_chunks: Option<Vec<GroundingChunk>>,
342 #[serde(skip_serializing_if = "Option::is_none")]
344 pub grounding_supports: Option<Vec<GroundingSupport>>,
345 #[serde(skip_serializing_if = "Option::is_none")]
347 pub web_search_queries: Option<Vec<String>>,
348 #[serde(skip_serializing_if = "Option::is_none")]
350 pub search_entry_point: Option<SearchEntryPoint>,
351 #[serde(skip_serializing_if = "Option::is_none")]
353 pub google_maps_widget_context_token: Option<String>,
354}
355
356#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
358#[serde(rename_all = "camelCase")]
359pub struct SearchEntryPoint {
360 #[serde(skip_serializing_if = "Option::is_none")]
362 pub rendered_content: Option<String>,
363 #[serde(skip_serializing_if = "Option::is_none")]
365 pub sdk_blob: Option<String>,
366}
367
368#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
370#[serde(rename_all = "camelCase")]
371pub struct GroundingChunk {
372 #[serde(skip_serializing_if = "Option::is_none")]
374 pub maps: Option<MapsGroundingChunk>,
375 #[serde(skip_serializing_if = "Option::is_none")]
377 pub web: Option<WebGroundingChunk>,
378}
379
380#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
382#[serde(rename_all = "camelCase")]
383pub struct MapsGroundingChunk {
384 #[serde(default)]
386 pub uri: Option<Url>,
387 #[serde(default)]
389 pub title: Option<String>,
390 #[serde(skip_serializing_if = "Option::is_none")]
392 pub place_id: Option<String>,
393}
394
395#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
397#[serde(rename_all = "camelCase")]
398pub struct WebGroundingChunk {
399 #[serde(default)]
401 pub uri: Option<Url>,
402 #[serde(default)]
404 pub title: Option<String>,
405}
406
407#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
409#[serde(rename_all = "camelCase")]
410pub struct GroundingSupport {
411 pub segment: GroundingSegment,
413 pub grounding_chunk_indices: Vec<u32>,
415}
416
417#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
419#[serde(rename_all = "camelCase")]
420pub struct GroundingSegment {
421 #[serde(default, skip_serializing_if = "Option::is_none")]
426 pub part_index: Option<u32>,
427 #[serde(default)]
429 pub start_index: Option<u32>,
430 #[serde(default)]
432 pub end_index: Option<u32>,
433 #[serde(default)]
435 pub text: Option<String>,
436}
437
438#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
440#[serde(rename_all = "camelCase")]
441pub struct GenerationResponse {
442 #[serde(default, skip_serializing_if = "Vec::is_empty")]
444 pub candidates: Vec<Candidate>,
445 #[serde(skip_serializing_if = "Option::is_none")]
447 pub prompt_feedback: Option<PromptFeedback>,
448 #[serde(skip_serializing_if = "Option::is_none")]
450 pub usage_metadata: Option<UsageMetadata>,
451 #[serde(skip_serializing_if = "Option::is_none")]
453 pub model_version: Option<String>,
454 #[serde(skip_serializing_if = "Option::is_none")]
456 pub response_id: Option<String>,
457}
458
459#[derive(Debug, Clone, Serialize, PartialEq)]
461#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
462pub enum BlockReason {
463 BlockReasonUnspecified,
465 Safety,
467 Other,
469 Blocklist,
471 ProhibitedContent,
473 ModelArmor,
475 Jailbreak,
477 ImageSafety,
479}
480
481impl BlockReason {
482 fn from_wire_str(value: &str) -> Self {
483 match value {
484 "BLOCK_REASON_UNSPECIFIED" | "BLOCKED_REASON_UNSPECIFIED" => {
485 Self::BlockReasonUnspecified
486 }
487 "SAFETY" => Self::Safety,
488 "OTHER" => Self::Other,
489 "BLOCKLIST" => Self::Blocklist,
490 "PROHIBITED_CONTENT" => Self::ProhibitedContent,
491 "MODEL_ARMOR" => Self::ModelArmor,
492 "JAILBREAK" => Self::Jailbreak,
493 "IMAGE_SAFETY" => Self::ImageSafety,
494 _ => Self::Other,
495 }
496 }
497
498 fn from_wire_number(value: i64) -> Self {
499 match value {
500 0 => Self::BlockReasonUnspecified,
501 1 => Self::Safety,
502 2 => Self::Other,
503 3 => Self::Blocklist,
504 4 => Self::ProhibitedContent,
505 5 => Self::ModelArmor,
506 6 => Self::Jailbreak,
507 7 => Self::ImageSafety,
508 _ => Self::Other,
509 }
510 }
511}
512
513impl<'de> Deserialize<'de> for BlockReason {
514 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
515 where
516 D: serde::Deserializer<'de>,
517 {
518 let value = serde_json::Value::deserialize(deserializer)?;
519 match value {
520 serde_json::Value::String(s) => Ok(Self::from_wire_str(&s)),
521 serde_json::Value::Number(n) => {
522 n.as_i64().map(Self::from_wire_number).ok_or_else(|| {
523 de::Error::custom("blockReason must be an integer-compatible number")
524 })
525 }
526 _ => Err(de::Error::custom("blockReason must be a string or integer")),
527 }
528 }
529}
530
531#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
533#[serde(rename_all = "camelCase")]
534pub struct PromptFeedback {
535 #[serde(default, skip_serializing_if = "Vec::is_empty")]
537 pub safety_ratings: Vec<SafetyRating>,
538 #[serde(skip_serializing_if = "Option::is_none")]
540 pub block_reason: Option<BlockReason>,
541}
542
543impl GenerationResponse {
544 pub fn text(&self) -> String {
553 self.candidates
554 .first()
555 .and_then(|c| c.content.parts.as_ref())
556 .map(|parts| {
557 parts
558 .iter()
559 .filter_map(|p| match p {
560 Part::Text { text, thought, thought_signature: _ }
561 if !thought.unwrap_or(false) =>
562 {
563 Some(text.as_str())
564 }
565 _ => None,
566 })
567 .collect::<String>()
568 })
569 .unwrap_or_default()
570 }
571
572 pub fn function_calls(&self) -> Vec<&crate::tools::FunctionCall> {
574 self.candidates
575 .iter()
576 .flat_map(|c| {
577 c.content
578 .parts
579 .as_ref()
580 .map(|parts| {
581 parts
582 .iter()
583 .filter_map(|p| match p {
584 Part::FunctionCall { function_call, thought_signature: _ } => {
585 Some(function_call)
586 }
587 _ => None,
588 })
589 .collect::<Vec<_>>()
590 })
591 .unwrap_or_default()
592 })
593 .collect()
594 }
595
596 pub fn function_calls_with_thoughts(
598 &self,
599 ) -> Vec<(&crate::tools::FunctionCall, Option<&String>)> {
600 self.candidates
601 .iter()
602 .flat_map(|c| {
603 c.content
604 .parts
605 .as_ref()
606 .map(|parts| {
607 parts
608 .iter()
609 .filter_map(|p| match p {
610 Part::FunctionCall { function_call, thought_signature } => {
611 Some((function_call, thought_signature.as_ref()))
612 }
613 _ => None,
614 })
615 .collect::<Vec<_>>()
616 })
617 .unwrap_or_default()
618 })
619 .collect()
620 }
621
622 pub fn thoughts(&self) -> Vec<String> {
624 self.candidates
625 .iter()
626 .flat_map(|c| {
627 c.content
628 .parts
629 .as_ref()
630 .map(|parts| {
631 parts
632 .iter()
633 .filter_map(|p| match p {
634 Part::Text { text, thought: Some(true), thought_signature: _ } => {
635 Some(text.clone())
636 }
637 _ => None,
638 })
639 .collect::<Vec<_>>()
640 })
641 .unwrap_or_default()
642 })
643 .collect()
644 }
645
646 pub fn all_text(&self) -> Vec<(String, bool)> {
648 self.candidates
649 .iter()
650 .flat_map(|c| {
651 c.content
652 .parts
653 .as_ref()
654 .map(|parts| {
655 parts
656 .iter()
657 .filter_map(|p| match p {
658 Part::Text { text, thought, thought_signature: _ } => {
659 Some((text.clone(), thought.unwrap_or(false)))
660 }
661 _ => None,
662 })
663 .collect::<Vec<_>>()
664 })
665 .unwrap_or_default()
666 })
667 .collect()
668 }
669
670 pub fn text_with_thoughts(&self) -> Vec<(String, bool, Option<&String>)> {
672 self.candidates
673 .iter()
674 .flat_map(|c| {
675 c.content
676 .parts
677 .as_ref()
678 .map(|parts| {
679 parts
680 .iter()
681 .filter_map(|p| match p {
682 Part::Text { text, thought, thought_signature } => Some((
683 text.clone(),
684 thought.unwrap_or(false),
685 thought_signature.as_ref(),
686 )),
687 _ => None,
688 })
689 .collect::<Vec<_>>()
690 })
691 .unwrap_or_default()
692 })
693 .collect()
694 }
695}
696
697#[derive(Debug, Clone, Serialize, Deserialize)]
699#[serde(rename_all = "camelCase")]
700pub struct GenerateContentRequest {
701 pub contents: Vec<Content>,
703 #[serde(skip_serializing_if = "Option::is_none")]
705 pub generation_config: Option<GenerationConfig>,
706 #[serde(skip_serializing_if = "Option::is_none")]
708 pub safety_settings: Option<Vec<SafetySetting>>,
709 #[serde(skip_serializing_if = "Option::is_none")]
711 pub tools: Option<Vec<crate::tools::Tool>>,
712 #[serde(skip_serializing_if = "Option::is_none")]
714 pub tool_config: Option<crate::tools::ToolConfig>,
715 #[serde(skip_serializing_if = "Option::is_none")]
717 pub system_instruction: Option<Content>,
718 #[serde(skip_serializing_if = "Option::is_none")]
720 pub cached_content: Option<String>,
721}
722
723impl GenerateContentRequest {
724 pub fn strip_vertex_unsupported_fields(&mut self) {
736 if let Some(tc) = &mut self.tool_config {
737 tc.include_server_side_tool_invocations = None;
738 }
739 }
740}
741
742#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
771#[serde(rename_all = "lowercase")]
772#[non_exhaustive]
773pub enum ThinkingLevel {
774 Minimal,
777 Low,
779 Medium,
782 High,
785 XHigh,
788}
789
790impl From<adk_core::Effort> for ThinkingLevel {
803 fn from(effort: adk_core::Effort) -> Self {
804 match effort {
805 adk_core::Effort::Low => Self::Low,
806 adk_core::Effort::Medium => Self::Medium,
807 adk_core::Effort::High => Self::High,
808 }
809 }
810}
811
812#[derive(Debug, Clone, Serialize, Deserialize)]
814#[serde(rename_all = "camelCase")]
815pub struct ThinkingConfig {
816 #[serde(skip_serializing_if = "Option::is_none")]
829 pub thinking_budget: Option<i32>,
830
831 #[serde(skip_serializing_if = "Option::is_none")]
836 pub include_thoughts: Option<bool>,
837
838 #[serde(skip_serializing_if = "Option::is_none")]
843 pub thinking_level: Option<ThinkingLevel>,
844}
845
846impl ThinkingConfig {
847 pub fn validate(&self) -> Result<(), String> {
852 if self.thinking_budget.is_some() && self.thinking_level.is_some() {
853 return Err(
854 "thinking_budget and thinking_level are mutually exclusive; use one or the other"
855 .to_string(),
856 );
857 }
858 Ok(())
859 }
860
861 pub fn new() -> Self {
863 Self { thinking_budget: None, include_thoughts: None, thinking_level: None }
864 }
865
866 pub fn with_thinking_budget(mut self, budget: i32) -> Self {
868 self.thinking_budget = Some(budget);
869 self
870 }
871
872 pub fn with_dynamic_thinking(mut self) -> Self {
874 self.thinking_budget = Some(-1);
875 self
876 }
877
878 pub fn with_thoughts_included(mut self, include: bool) -> Self {
880 self.include_thoughts = Some(include);
881 self
882 }
883
884 pub fn with_thinking_level(mut self, level: ThinkingLevel) -> Self {
889 self.thinking_level = Some(level);
890 self
891 }
892
893 pub fn dynamic_thinking() -> Self {
895 Self { thinking_budget: Some(-1), include_thoughts: Some(true), thinking_level: None }
896 }
897}
898
899impl Default for ThinkingConfig {
900 fn default() -> Self {
901 Self::new()
902 }
903}
904
905#[derive(Debug, Default, Clone, Serialize, Deserialize)]
907#[serde(rename_all = "camelCase")]
908pub struct GenerationConfig {
909 #[serde(skip_serializing_if = "Option::is_none")]
914 pub temperature: Option<f32>,
915
916 #[serde(skip_serializing_if = "Option::is_none")]
922 pub top_p: Option<f32>,
923
924 #[serde(skip_serializing_if = "Option::is_none")]
929 pub top_k: Option<i32>,
930
931 #[serde(skip_serializing_if = "Option::is_none")]
935 pub max_output_tokens: Option<i32>,
936
937 #[serde(skip_serializing_if = "Option::is_none")]
941 pub candidate_count: Option<i32>,
942
943 #[serde(skip_serializing_if = "Option::is_none")]
947 pub stop_sequences: Option<Vec<String>>,
948
949 #[serde(default, skip_serializing_if = "Option::is_none")]
951 pub seed: Option<i64>,
952
953 #[serde(default, skip_serializing_if = "Option::is_none")]
957 pub presence_penalty: Option<f32>,
958
959 #[serde(default, skip_serializing_if = "Option::is_none")]
963 pub frequency_penalty: Option<f32>,
964
965 #[serde(default, skip_serializing_if = "Option::is_none")]
971 pub response_logprobs: Option<bool>,
972
973 #[serde(default, skip_serializing_if = "Option::is_none")]
976 pub logprobs: Option<i32>,
977
978 #[serde(skip_serializing_if = "Option::is_none")]
982 pub response_mime_type: Option<String>,
983 #[serde(skip_serializing_if = "Option::is_none")]
987 pub response_schema: Option<serde_json::Value>,
988
989 #[serde(skip_serializing_if = "Option::is_none")]
991 pub response_modalities: Option<Vec<String>>,
992
993 #[serde(skip_serializing_if = "Option::is_none")]
995 pub speech_config: Option<SpeechConfig>,
996
997 #[serde(skip_serializing_if = "Option::is_none")]
1001 pub thinking_config: Option<ThinkingConfig>,
1002
1003 #[serde(default, skip_serializing_if = "Option::is_none")]
1006 pub media_resolution: Option<MediaResolution>,
1007}
1008
1009#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1024#[non_exhaustive]
1025pub enum MediaResolution {
1026 #[serde(rename = "MEDIA_RESOLUTION_UNSPECIFIED")]
1028 Unspecified,
1029 #[serde(rename = "MEDIA_RESOLUTION_LOW")]
1031 Low,
1032 #[serde(rename = "MEDIA_RESOLUTION_MEDIUM")]
1034 Medium,
1035 #[serde(rename = "MEDIA_RESOLUTION_HIGH")]
1037 High,
1038}
1039
1040impl GenerationConfig {
1041 pub fn validate(&self) -> Result<(), String> {
1052 if let Some(t) = self.temperature
1053 && !(0.0..=2.0).contains(&t)
1054 {
1055 return Err("temperature must be between 0.0 and 2.0".to_string());
1056 }
1057 if let Some(p) = self.top_p
1058 && !(0.0..=1.0).contains(&p)
1059 {
1060 return Err("top_p must be between 0.0 and 1.0".to_string());
1061 }
1062 if let Some(k) = self.top_k
1063 && k <= 0
1064 {
1065 return Err("top_k must be positive".to_string());
1066 }
1067 if let Some(m) = self.max_output_tokens
1068 && m <= 0
1069 {
1070 return Err("max_output_tokens must be positive".to_string());
1071 }
1072 if let Some(ref tc) = self.thinking_config {
1073 tc.validate()?;
1074 }
1075 Ok(())
1076 }
1077}
1078
1079#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1081#[serde(rename_all = "camelCase")]
1082pub struct SpeechConfig {
1083 #[serde(skip_serializing_if = "Option::is_none")]
1085 pub voice_config: Option<VoiceConfig>,
1086 #[serde(skip_serializing_if = "Option::is_none")]
1088 pub multi_speaker_voice_config: Option<MultiSpeakerVoiceConfig>,
1089}
1090
1091#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1093#[serde(rename_all = "camelCase")]
1094pub struct VoiceConfig {
1095 #[serde(skip_serializing_if = "Option::is_none")]
1097 pub prebuilt_voice_config: Option<PrebuiltVoiceConfig>,
1098}
1099
1100#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1102#[serde(rename_all = "camelCase")]
1103pub struct PrebuiltVoiceConfig {
1104 pub voice_name: String,
1106}
1107
1108#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1110#[serde(rename_all = "camelCase")]
1111pub struct MultiSpeakerVoiceConfig {
1112 pub speaker_voice_configs: Vec<SpeakerVoiceConfig>,
1114}
1115
1116#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
1118#[serde(rename_all = "camelCase")]
1119pub struct SpeakerVoiceConfig {
1120 pub speaker: String,
1122 pub voice_config: VoiceConfig,
1124}
1125
1126impl SpeechConfig {
1127 pub fn single_voice(voice_name: impl Into<String>) -> Self {
1129 Self {
1130 voice_config: Some(VoiceConfig {
1131 prebuilt_voice_config: Some(PrebuiltVoiceConfig { voice_name: voice_name.into() }),
1132 }),
1133 multi_speaker_voice_config: None,
1134 }
1135 }
1136
1137 pub fn multi_speaker(speakers: Vec<SpeakerVoiceConfig>) -> Self {
1139 Self {
1140 voice_config: None,
1141 multi_speaker_voice_config: Some(MultiSpeakerVoiceConfig {
1142 speaker_voice_configs: speakers,
1143 }),
1144 }
1145 }
1146}
1147
1148impl SpeakerVoiceConfig {
1149 pub fn new(speaker: impl Into<String>, voice_name: impl Into<String>) -> Self {
1151 Self {
1152 speaker: speaker.into(),
1153 voice_config: VoiceConfig {
1154 prebuilt_voice_config: Some(PrebuiltVoiceConfig { voice_name: voice_name.into() }),
1155 },
1156 }
1157 }
1158}
1159
1160#[cfg(test)]
1161mod effort_tests {
1162 use super::ThinkingLevel;
1163
1164 #[test]
1166 fn every_harness_effort_maps_to_its_namesake() {
1167 for (core, level) in [
1168 (adk_core::Effort::Low, ThinkingLevel::Low),
1169 (adk_core::Effort::Medium, ThinkingLevel::Medium),
1170 (adk_core::Effort::High, ThinkingLevel::High),
1171 ] {
1172 assert_eq!(ThinkingLevel::from(core), level);
1173 }
1174 }
1175}