From 8b94651a847fb3237a57ec8a6852df5338edd190 Mon Sep 17 00:00:00 2001 From: Andre Bandarra Date: Wed, 17 Apr 2024 13:32:54 +0100 Subject: [PATCH] Makes candidate content optional --- src/types/generate_content.rs | 62 +++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/types/generate_content.rs b/src/types/generate_content.rs index ebfa1d6..527065d 100644 --- a/src/types/generate_content.rs +++ b/src/types/generate_content.rs @@ -44,7 +44,7 @@ pub struct GenerationConfig { #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Candidate { - pub content: Content, + pub content: Option, pub citation_metadata: Option, pub safety_ratings: Vec, pub finish_reason: Option, @@ -52,7 +52,10 @@ pub struct Candidate { impl Candidate { pub fn get_text(&self) -> Option { - self.content.get_text() + match &self.content { + Some(content) => content.get_text(), + None => None, + } } } @@ -197,4 +200,59 @@ mod tests { }"#; serde_json::from_str::(input).unwrap(); } + + #[test] + fn parses_candidates_without_content() { + let input = r#"{ + "candidates": [ + { + "finishReason": "RECITATION", + "safetyRatings": [ + { + "category": "HARM_CATEGORY_HATE_SPEECH", + "probability": "NEGLIGIBLE", + "probabilityScore": 0.08021325, + "severity": "HARM_SEVERITY_NEGLIGIBLE", + "severityScore": 0.0721122 + }, + { + "category": "HARM_CATEGORY_DANGEROUS_CONTENT", + "probability": "NEGLIGIBLE", + "probabilityScore": 0.19360436, + "severity": "HARM_SEVERITY_NEGLIGIBLE", + "severityScore": 0.1066906 + }, + { + "category": "HARM_CATEGORY_HARASSMENT", + "probability": "NEGLIGIBLE", + "probabilityScore": 0.07751766, + "severity": "HARM_SEVERITY_NEGLIGIBLE", + "severityScore": 0.040769264 + }, + { + "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", + "probability": "NEGLIGIBLE", + "probabilityScore": 0.030792166, + "severity": "HARM_SEVERITY_NEGLIGIBLE", + "severityScore": 0.04138472 + } + ], + "citationMetadata": { + "citations": [ + { + "startIndex": 1108, + "endIndex": 1250, + "uri": "https://chrome.google.com/webstore/detail/autocontrol-shortcut-mana/lkaihdpfpifdlgoapbfocpmekbokmcfd?hl=zh-TW" + } + ] + } + } + ], + "usageMetadata": { + "promptTokenCount": 577, + "totalTokenCount": 577 + } + }"#; + serde_json::from_str::(input).unwrap(); + } }