Add support for the Google Search tool
- Gemini 2.0 and above use a google_search tool instead of a google_search_retrieval tool. This commit adds support for the new tool.
This commit is contained in:
@@ -29,8 +29,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
println!(
|
||||
"Request: {}",
|
||||
serde_json::to_string_pretty(&request).unwrap()
|
||||
);
|
||||
|
||||
let result = gemini
|
||||
.generate_content(&request, "gemini-1.0-pro-002")
|
||||
.generate_content(&request, "gemini-1.5-flash-002")
|
||||
.await?;
|
||||
|
||||
println!("Response: {:?}", result.candidates[0].get_text().unwrap());
|
||||
|
||||
44
examples/google-search.rs
Normal file
44
examples/google-search.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use gemini_rs::prelude::*;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
tracing_subscriber::fmt().init();
|
||||
let authentication_manager = gcp_auth::provider().await?;
|
||||
let api_endpoint = std::env::var("API_ENDPOINT")?;
|
||||
let project_id = std::env::var("PROJECT_ID")?;
|
||||
let location_id = std::env::var("LOCATION_ID")?;
|
||||
|
||||
let gemini = GeminiClient::new(
|
||||
authentication_manager,
|
||||
api_endpoint,
|
||||
project_id,
|
||||
location_id,
|
||||
);
|
||||
|
||||
let prompt = "What day is today?";
|
||||
|
||||
let request = GenerateContentRequest {
|
||||
contents: vec![Content {
|
||||
role: Some(Role::User),
|
||||
parts: Some(vec![Part::Text(prompt.to_string())]),
|
||||
}],
|
||||
tools: Some(vec![Tools {
|
||||
google_search: Some(GoogleSearch::default()),
|
||||
..Default::default()
|
||||
}]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
println!(
|
||||
"Request: {}",
|
||||
serde_json::to_string_pretty(&request).unwrap()
|
||||
);
|
||||
|
||||
let result = gemini
|
||||
.generate_content(&request, "gemini-2.0-flash-001")
|
||||
.await?;
|
||||
|
||||
println!("Response: {:?}", result.candidates[0].get_text().unwrap());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -71,15 +71,39 @@ impl GenerateContentRequestBuilder {
|
||||
pub struct Tools {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub function_declarations: Option<Vec<FunctionDeclaration>>,
|
||||
|
||||
#[serde(rename = "googleSearchRetrieval")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub google_search_retrieval: Option<GoogleSearchRetrieval>,
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub google_search: Option<GoogleSearch>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
pub struct GoogleSearch {}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DynamicRetrievalConfig {
|
||||
pub mode: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub dynamic_threshold: Option<f32>,
|
||||
}
|
||||
|
||||
impl Default for DynamicRetrievalConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mode: "MODE_DYNAMIC".to_string(),
|
||||
dynamic_threshold: Some(0.7),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct GoogleSearchRetrieval {
|
||||
pub disable_attribution: bool,
|
||||
pub dynamic_retrieval_config: DynamicRetrievalConfig,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
||||
|
||||
Reference in New Issue
Block a user