pub struct GeminiEmbedding { /* private fields */ }Expand description
Client for generating embeddings using Gemini embedding models.
§Example
use gemini_client_api::gemini::embed::GeminiEmbedding;
use gemini_client_api::gemini::types::embedding::TaskType;
let embedder = GeminiEmbedding::new("YOUR_API_KEY", "gemini-embedding-001")
.set_task_type(TaskType::RetrievalDocument);
let response = embedder.embed_text("Hello, world!").await.unwrap();
println!("Embedding dimension: {}", response.embedding().values().len());Implementations§
Source§impl GeminiEmbedding
impl GeminiEmbedding
Sourcepub fn new(api_key: impl Into<String>, model: impl Into<String>) -> Self
pub fn new(api_key: impl Into<String>, model: impl Into<String>) -> Self
Creates a new GeminiEmbedding client.
§Arguments
api_key- Your Gemini API key. Get one from Google AI studio.model- The embedding model to use (e.g.,"gemini-embedding-001"). See embedding models.
Examples found in repository?
examples/embedding.rs (line 10)
6async fn main() {
7 // 1. Create the Gemini Embedding client
8 // Get your API key from https://aistudio.google.com/app/apikey
9 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
10 let embedder = GeminiEmbedding::new(api_key, "gemini-embedding-001")
11 .set_task_type(TaskType::RetrievalDocument)
12 // Optional: reduce dimension for Matryoshka Representation Learning
13 .set_output_dimensionality(256);
14
15 // 2. Generate embedding for a single text
16 let prompt = "Rust is a blazing fast and memory-efficient systems programming language.";
17 let response = embedder.embed_text(prompt).await.unwrap();
18
19 // 3. Print the embedding information
20 let embedding = response.embedding();
21 println!("Embedding generated for: {:?}", prompt);
22 println!("Total Dimensions: {}", embedding.dimension());
23 println!(
24 "First 5 values: {:?}",
25 &embedding.values()[..usize::min(embedding.dimension(), 5)]
26 );
27}Sourcepub fn new_with_client(
api_key: impl Into<String>,
model: impl Into<String>,
client: Client,
) -> Self
pub fn new_with_client( api_key: impl Into<String>, model: impl Into<String>, client: Client, ) -> Self
Creates a new GeminiEmbedding client with a custom reqwest::Client.
§Arguments
api_key- Your Gemini API key.model- The embedding model to use.client- A customreqwest::Clientfor making requests.
Sourcepub fn set_task_type(self, task_type: TaskType) -> Self
pub fn set_task_type(self, task_type: TaskType) -> Self
Sets the task type for the embedding.
The task type helps the model produce better embeddings tailored for the specific use case.
Examples found in repository?
examples/embedding.rs (line 11)
6async fn main() {
7 // 1. Create the Gemini Embedding client
8 // Get your API key from https://aistudio.google.com/app/apikey
9 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
10 let embedder = GeminiEmbedding::new(api_key, "gemini-embedding-001")
11 .set_task_type(TaskType::RetrievalDocument)
12 // Optional: reduce dimension for Matryoshka Representation Learning
13 .set_output_dimensionality(256);
14
15 // 2. Generate embedding for a single text
16 let prompt = "Rust is a blazing fast and memory-efficient systems programming language.";
17 let response = embedder.embed_text(prompt).await.unwrap();
18
19 // 3. Print the embedding information
20 let embedding = response.embedding();
21 println!("Embedding generated for: {:?}", prompt);
22 println!("Total Dimensions: {}", embedding.dimension());
23 println!(
24 "First 5 values: {:?}",
25 &embedding.values()[..usize::min(embedding.dimension(), 5)]
26 );
27}Sourcepub fn set_output_dimensionality(self, output_dimensionality: u32) -> Self
pub fn set_output_dimensionality(self, output_dimensionality: u32) -> Self
Sets the output dimensionality for the embedding.
Allows reducing the embedding dimension for storage/performance optimization via Matryoshka Representation Learning.
Examples found in repository?
examples/embedding.rs (line 13)
6async fn main() {
7 // 1. Create the Gemini Embedding client
8 // Get your API key from https://aistudio.google.com/app/apikey
9 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
10 let embedder = GeminiEmbedding::new(api_key, "gemini-embedding-001")
11 .set_task_type(TaskType::RetrievalDocument)
12 // Optional: reduce dimension for Matryoshka Representation Learning
13 .set_output_dimensionality(256);
14
15 // 2. Generate embedding for a single text
16 let prompt = "Rust is a blazing fast and memory-efficient systems programming language.";
17 let response = embedder.embed_text(prompt).await.unwrap();
18
19 // 3. Print the embedding information
20 let embedding = response.embedding();
21 println!("Embedding generated for: {:?}", prompt);
22 println!("Total Dimensions: {}", embedding.dimension());
23 println!(
24 "First 5 values: {:?}",
25 &embedding.values()[..usize::min(embedding.dimension(), 5)]
26 );
27}pub fn set_api_key(self, api_key: impl Into<String>) -> Self
pub fn set_model(self, model: impl Into<String>) -> Self
Sourcepub fn set_config(self, config: EmbedContentConfig) -> Self
pub fn set_config(self, config: EmbedContentConfig) -> Self
Sets the full embedding configuration, replacing any previously set config.
Sourcepub async fn embed(
&self,
content: Vec<Part>,
) -> Result<EmbedContentResponse, GeminiResponseError>
pub async fn embed( &self, content: Vec<Part>, ) -> Result<EmbedContentResponse, GeminiResponseError>
Sourcepub async fn embed_text(
&self,
text: impl Into<String>,
) -> Result<EmbedContentResponse, GeminiResponseError>
pub async fn embed_text( &self, text: impl Into<String>, ) -> Result<EmbedContentResponse, GeminiResponseError>
Convenience method to generate an embedding for a single text string.
§Arguments
text- The text to embed.
§Example
let embedder = GeminiEmbedding::new("YOUR_API_KEY", "gemini-embedding-001");
let response = embedder.embed_text("What is the meaning of life?").await.unwrap();
println!("Got {} dimensions", response.embedding().values().len());Examples found in repository?
examples/embedding.rs (line 17)
6async fn main() {
7 // 1. Create the Gemini Embedding client
8 // Get your API key from https://aistudio.google.com/app/apikey
9 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
10 let embedder = GeminiEmbedding::new(api_key, "gemini-embedding-001")
11 .set_task_type(TaskType::RetrievalDocument)
12 // Optional: reduce dimension for Matryoshka Representation Learning
13 .set_output_dimensionality(256);
14
15 // 2. Generate embedding for a single text
16 let prompt = "Rust is a blazing fast and memory-efficient systems programming language.";
17 let response = embedder.embed_text(prompt).await.unwrap();
18
19 // 3. Print the embedding information
20 let embedding = response.embedding();
21 println!("Embedding generated for: {:?}", prompt);
22 println!("Total Dimensions: {}", embedding.dimension());
23 println!(
24 "First 5 values: {:?}",
25 &embedding.values()[..usize::min(embedding.dimension(), 5)]
26 );
27}Trait Implementations§
Source§impl Clone for GeminiEmbedding
impl Clone for GeminiEmbedding
Source§fn clone(&self) -> GeminiEmbedding
fn clone(&self) -> GeminiEmbedding
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for GeminiEmbedding
impl !UnwindSafe for GeminiEmbedding
impl Freeze for GeminiEmbedding
impl Send for GeminiEmbedding
impl Sync for GeminiEmbedding
impl Unpin for GeminiEmbedding
impl UnsafeUnpin for GeminiEmbedding
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more