Skip to content

Commit 847f518

Browse files
authored
Merge pull request #12 from VintageWander/main
Add in types for url context and google search
2 parents d12ee05 + d99d245 commit 847f518

1 file changed

Lines changed: 159 additions & 8 deletions

File tree

src/types.rs

Lines changed: 159 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::{BTreeSet, HashMap};
22

33
use serde::{Deserialize, Serialize};
44
use serde_json::Value;
@@ -44,6 +44,11 @@ pub enum Tool {
4444
google_search: serde_json::Value,
4545
},
4646

47+
UrlContext {
48+
#[serde(rename = "url_context")]
49+
url_context: serde_json::Value,
50+
},
51+
4752
/* NOTE: Used by v2 models if they have the code execution built in */
4853
CodeExecution {
4954
code_execution: serde_json::Value,
@@ -274,6 +279,8 @@ pub struct UsageMetadata {
274279
candidates_tokens_details: Vec<ModalityTokenCount>,
275280
#[serde(default)]
276281
tool_use_prompt_tokens_details: Vec<ModalityTokenCount>,
282+
#[serde(skip_serializing_if = "Option::is_none")]
283+
pub traffic_type: Option<TrafficType>,
277284
}
278285

279286
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -283,6 +290,20 @@ pub struct ModalityTokenCount {
283290
token_count: u32,
284291
}
285292

293+
/// Request traffic type. Indicates whether the request consumes Pay-As-You-Go or
294+
/// Provisioned Throughput quota.
295+
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq)]
296+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
297+
pub enum TrafficType {
298+
/// Unspecified request traffic type.
299+
#[default]
300+
TrafficTypeUnspecified,
301+
/// Type for Pay-As-You-Go traffic.
302+
OnDemand,
303+
/// Type for Provisioned Throughput traffic.
304+
ProvisionedThroughput,
305+
}
306+
286307
/// Content Part modality
287308
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq)]
288309
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
@@ -338,10 +359,9 @@ pub struct Candidate {
338359
/// answer. This field is populated for `GenerateAnswer` calls.
339360
#[serde(default)]
340361
pub grounding_attributions: Vec<GroundingAttribution>,
341-
// TODO
342-
// /// Grounding metadata for the candidate. This field is populated for
343-
// /// `GenerateContent` calls.
344-
// pub grounding_metadata: Option<GroundingMetadata>,
362+
/// Grounding metadata for the candidate. This field is populated for
363+
/// `GenerateContent` calls.
364+
pub grounding_metadata: Option<GroundingMetadata>,
345365
/// Average log probability score of the candidate.
346366
pub avg_logprobs: Option<f32>,
347367
// TODO
@@ -350,9 +370,8 @@ pub struct Candidate {
350370
// TODO
351371
// /// Metadata related to url context retrieval tool.
352372
// pub url_retrieval_metadata: Option<UrlRetrievalMetadata>,
353-
// TODO
354-
// /// Metadata related to url context retrieval tool.
355-
// pub url_context_metadata: Option<UrlContextMetadata>,
373+
/// Metadata related to url context retrieval tool.
374+
pub url_context_metadata: Option<UrlContextMetadata>,
356375
/// Index of the candidate in the list of response candidates.
357376
pub index: Option<u32>,
358377
}
@@ -409,6 +428,7 @@ pub struct SemanticRetrieverChunk {
409428
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
410429
#[serde(rename_all = "camelCase")]
411430
pub struct CitationMetadata {
431+
#[serde(default)]
412432
pub citation_sources: Vec<CitationSource>,
413433
}
414434

@@ -502,6 +522,137 @@ pub enum HarmCategory {
502522
CivicIntegrity,
503523
}
504524

525+
/// Metadata returned to client when grounding is enabled.
526+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
527+
#[serde(rename_all = "camelCase")]
528+
pub struct GroundingMetadata {
529+
/// List of supporting references retrieved from specified grounding source.
530+
#[serde(default)]
531+
pub grounding_chunks: Vec<GroundingChunk>,
532+
/// List of grounding support.
533+
#[serde(default)]
534+
pub grounding_supports: Vec<GroundingSupport>,
535+
/// Web search queries for the following-up web search.
536+
#[serde(default)]
537+
pub web_search_queries: Vec<String>,
538+
/// Optional. Google search entry for the following-up web searches.
539+
#[serde(skip_serializing_if = "Option::is_none")]
540+
pub search_entry_point: Option<SearchEntryPoint>,
541+
/// Metadata related to retrieval in the grounding flow.
542+
#[serde(skip_serializing_if = "Option::is_none")]
543+
pub retrieval_metadata: Option<RetrievalMetadata>,
544+
}
545+
546+
/// Grounding chunk.
547+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
548+
#[serde(rename_all = "camelCase")]
549+
pub enum GroundingChunk {
550+
/// Grounding chunk from the web.
551+
Web(Web),
552+
}
553+
554+
/// Chunk from the web
555+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
556+
#[serde(rename_all = "camelCase")]
557+
pub struct Web {
558+
/// URI reference of the chunk
559+
pub uri: String,
560+
/// Title of the chunk
561+
pub title: String,
562+
}
563+
564+
/// Grounding support.
565+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
566+
#[serde(rename_all = "camelCase")]
567+
pub struct GroundingSupport {
568+
/// A list of indices (into 'grounding_chunk') specifying the citations associated with the claim.
569+
/// For instance [1,3,4] means that grounding_chunk[1], grounding_chunk[3], grounding_chunk[4] are the
570+
/// retrieved content attributed to the claim.
571+
#[serde(default)]
572+
pub grounding_chunk_indices: BTreeSet<u32>,
573+
/// Confidence score of the support references. Ranges from 0 to 1. 1 is the most confident.
574+
/// This list must have the same size as the groundingChunkIndices.
575+
#[serde(default)]
576+
pub confidence_scores: Vec<f64>,
577+
/// Segment of the content this support belongs to.
578+
pub segment: Segment,
579+
}
580+
581+
/// Segment of the content.
582+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
583+
#[serde(rename_all = "camelCase")]
584+
pub struct Segment {
585+
/// Output only. The index of a Part object within its parent Content object.
586+
#[serde(skip_serializing_if = "Option::is_none")]
587+
pub part_index: Option<u32>,
588+
/// Output only. Start index in the given Part, measured in bytes. Offset from the start of the Part, inclusive, starting at zero.
589+
#[serde(skip_serializing_if = "Option::is_none")]
590+
pub start_index: Option<u32>,
591+
/// Output only. End index in the given Part, measured in bytes. Offset from the start of the Part, exclusive, starting at zero.
592+
#[serde(skip_serializing_if = "Option::is_none")]
593+
pub end_index: Option<u32>,
594+
/// Output only. The text corresponding to the segment from the response.
595+
#[serde(skip_serializing_if = "Option::is_none")]
596+
pub text: Option<String>,
597+
}
598+
599+
/// Google search entry point.
600+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
601+
#[serde(rename_all = "camelCase")]
602+
pub struct SearchEntryPoint {
603+
/// Optional. Web content snippet that can be embedded in a web page or an app webview.
604+
#[serde(skip_serializing_if = "Option::is_none")]
605+
pub rendered_content: Option<String>,
606+
/// Optional. Base64 encoded JSON representing array of <search term, search url> tuple.
607+
/// A base64-encoded string.
608+
#[serde(skip_serializing_if = "Option::is_none")]
609+
pub sdk_blob: Option<String>,
610+
}
611+
612+
/// Retrieval metadata for grounding
613+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
614+
#[serde(rename_all = "camelCase")]
615+
pub struct RetrievalMetadata {
616+
/// Optional. Score indicating how likely information from google search could help answer the prompt.
617+
/// The score is in the range [0, 1], where 0 is the least likely and 1 is the most likely.
618+
/// This score is only populated when google search grounding and dynamic retrieval is enabled.
619+
/// It will be compared to the threshold to determine whether to trigger google search.
620+
#[serde(skip_serializing_if = "Option::is_none")]
621+
pub google_search_dynamic_retrieval_score: Option<f64>,
622+
}
623+
624+
/// Metadata related to url context retrieval tool.
625+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
626+
#[serde(rename_all = "camelCase")]
627+
pub struct UrlContextMetadata {
628+
/// List of url context.
629+
#[serde(default)]
630+
pub url_metadata: Vec<UrlMetadata>,
631+
}
632+
633+
/// Context of the a single url retrieval.
634+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
635+
#[serde(rename_all = "camelCase")]
636+
pub struct UrlMetadata {
637+
/// Retrieved url by the tool.
638+
pub retrieved_url: String,
639+
/// Status of the url retrieval.
640+
pub url_retrieval_status: UrlRetrievalStatus,
641+
}
642+
643+
/// Status of the url retrieval.
644+
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
645+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
646+
pub enum UrlRetrievalStatus {
647+
/// Default value. This value is unused.
648+
#[default]
649+
UrlRetrievalStatusUnspecified,
650+
/// Url retrieval is successful.
651+
Success,
652+
/// Url retrieval is failed due to error.
653+
Error,
654+
}
655+
505656
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
506657
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
507658
pub enum FinishReason {

0 commit comments

Comments
 (0)