Files
gemini-rs/examples/text-from-text-streaming.rs
2024-11-27 15:11:01 +00:00

47 lines
1.3 KiB
Rust

use gemini_rs::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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 = "Tell me the story of the genesis of the universe as a bedtime story.";
let request = GenerateContentRequest::builder()
.add_content(
Content::builder()
.role(Role::User)
.add_part(Part::Text(prompt.to_string()))
.build(),
)
.build();
let queue = gemini.stream_generate_content(&request, "gemini-pro").await;
while let Some(response) = queue.pop().await {
match response {
Ok(result) => {
let text = result
.candidates
.iter()
.filter_map(|c| c.get_text())
.collect::<String>();
print!("{}", text);
}
Err(error) => {
println!("{error}");
}
}
}
Ok(())
}