pub struct Client {
pub client: Client,
pub api_key: String,
pub model: String,
pub api_url: Url,
}Expand description
Gemini API client structure.
Fieldsยง
ยงclient: ClientReqwest client instance.
api_key: StringAPI key for authentication.
model: StringModel to be used.
api_url: UrlAPI URL for Gemini.
Implementationsยง
Sourceยงimpl Client
impl Client
Sourcepub fn new(api_key: &str, model: &str) -> Self
pub fn new(api_key: &str, model: &str) -> Self
Creates a new instance of the Gemini Client.
ยงArguments
api_key- A static string representing the API key for authentication.model- A static string representing the model to be used.
ยงReturns
A new instance of the Gemini Client.
ยงPanics
Panics if there is an issue parsing the Gemini API URL.
ยงExamples
use gems::Client;
let client = Client::new("your_api_key", "your_model");Sourcepub async fn generate_content(
&mut self,
text: &str,
) -> Result<String, Box<dyn Error>>
pub async fn generate_content( &mut self, text: &str, ) -> Result<String, Box<dyn Error>>
Generates content using the Gemini API.
ยงArguments
text- A static string representing the input text for content generation.
ยงReturns
A Result containing the generated content as a string or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let result = client.generate_content("input_text").await;
match result {
Ok(content) => println!("Generated Content: {}", content),
Err(err) => eprintln!("Error: {:?}", err),
}
}Sourcepub async fn stream_generate_content(
&mut self,
text: &str,
) -> Result<Response, Box<dyn Error>>
pub async fn stream_generate_content( &mut self, text: &str, ) -> Result<Response, Box<dyn Error>>
Streams generated content using the Gemini API and prints it with a delay effect.
ยงArguments
text- A static string representing the input text for content generation.suppress- A boolean to decide whether or not to print the content being generated
ยงReturns
A Result indicating success or a Box
ยงExamples
use futures_util::StreamExt;
use gems::Client;
use gems::utils::{
extract_text_from_partial_json, type_with_cursor_effect,
};
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let result = client.stream_generate_content("input_text").await;
let mut stream = result.expect("response").bytes_stream();
let delay = 5;
let mut message: String = Default::default();
while let Some(mut chunk) = stream.next().await {
if let Ok(parsed_json) = std::str::from_utf8(chunk.as_mut().unwrap()) {
if let Some(text_value) = extract_text_from_partial_json(parsed_json) {
let lines: Vec<&str> = text_value
.split("\\n")
.flat_map(|s| s.split('\n'))
.collect();
for line in lines {
message.push_str(&line.replace('\\', ""));
if !line.is_empty() {
type_with_cursor_effect(&line.replace('\\', ""), delay);
} else {
println!("\n");
}
}
}
} else {
eprintln!("Failed to parse chunk: {:?}", chunk.as_ref().unwrap());
}
}
}Sourcepub async fn count_tokens(&mut self, text: &str) -> Result<usize, Error>
pub async fn count_tokens(&mut self, text: &str) -> Result<usize, Error>
Counts the number of tokens in the provided text using the Gemini API.
ยงArguments
text- A static string representing the input text for token counting.
ยงReturns
A Result containing the count of tokens as usize or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let result = client.count_tokens("input_text").await;
match result {
Ok(count) => println!("Token Count: {}", count),
Err(err) => eprintln!("Error: {:?}", err),
}
}Sourcepub async fn get_model_info(&mut self) -> Result<ModelInfo, Error>
pub async fn get_model_info(&mut self) -> Result<ModelInfo, Error>
Retrieves information about the specified model using the Gemini API.
ยงReturns
A Result containing model information or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let result = client.get_model_info().await;
match result {
Ok(info) => println!("Model Info: {:?}", info),
Err(err) => eprintln!("Error: {:?}", err),
}
}Sourcepub async fn embed_content(
&mut self,
text: &str,
) -> Result<EmbedContentResponse, Error>
pub async fn embed_content( &mut self, text: &str, ) -> Result<EmbedContentResponse, Error>
Embeds content using the Gemini API.
ยงArguments
text- A static string representing the input text for content embedding.
ยงReturns
A Result containing the embedded content response or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let result = client.embed_content("input_text").await;
match result {
Ok(response) => println!("Embedded Content: {:?}", response),
Err(err) => eprintln!("Error: {:?}", err),
}
}Sourcepub async fn batch_embed_contents(
&mut self,
texts: Vec<&str>,
) -> Result<BatchEmbedContentsResponse, Error>
pub async fn batch_embed_contents( &mut self, texts: Vec<&str>, ) -> Result<BatchEmbedContentsResponse, Error>
Batch embeds multiple contents using the Gemini API.
ยงArguments
texts- A vector of static strings representing the input texts for batch embedding.
ยงReturns
A Result containing the batch embedded contents response or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let texts = vec!["text1", "text2", "text3"];
let result = client.batch_embed_contents(texts).await;
match result {
Ok(response) => println!("Batch Embedded Contents: {:?}", response),
Err(err) => eprintln!("Error: {:?}", err),
}
}Sourcepub async fn list_models(&self) -> Result<ModelsResponse, Error>
pub async fn list_models(&self) -> Result<ModelsResponse, Error>
Retrieves a list of available models from the Gemini API.
ยงReturns
A Result containing the list of models or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let client = Client::new("your_api_key", "your_model");
let result = client.list_models().await;
match result {
Ok(models) => println!("Available Models: {:?}", models),
Err(err) => eprintln!("Error: {:?}", err),
}
}Sourcepub async fn generate_content_with_image(
&mut self,
text: &str,
image_data: &str,
) -> Result<String, Box<dyn Error>>
pub async fn generate_content_with_image( &mut self, text: &str, image_data: &str, ) -> Result<String, Box<dyn Error>>
Generates content using the Gemini API with both text and image input.
ยงArguments
text- A static string representing the input text for content generation.image_data- A base64-encoded string representing the image data.
ยงReturns
A Result containing the generated content as a string or a reqwest::Error on failure.
ยงExamples
use gems::Client;
#[tokio::main]
async fn main() {
let mut client = Client::new("your_api_key", "your_model");
let result = client.generate_content_with_image("What is this picture?", "base64_encoded_image_data").await;
match result {
Ok(content) => println!("Generated Content: {}", content),
Err(err) => eprintln!("Error: {:?}", err),
}
}