Drops the serde_eventsource dependency

serde_eventsource is taking a while to update to reqwest 13.*. This
PR implements handling SSE in the library code.
This commit is contained in:
2026-01-29 20:04:33 +00:00
parent 941614e550
commit ab8e4ede60
7 changed files with 428 additions and 327 deletions

View File

@@ -0,0 +1,32 @@
use std::{env, error::Error};
use google_genai::prelude::{Content, GeminiClient, GenerateContentRequest, Role};
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt().init();
let _ = dotenvy::dotenv();
let api_key = env::var("GEMINI_API_KEY")?;
let gemini_client = GeminiClient::new(api_key);
let prompt = vec![
Content::builder()
.role(Role::User)
.add_text_part("What is the airspeed of an unladen swallow?")
.build(),
];
let request = GenerateContentRequest::builder().contents(prompt).build();
let mut response_stream = gemini_client
.stream_generate_content(&request, "gemini-3-pro-preview")
.await?;
while let Some(content) = response_stream.next().await {
match content {
Ok(response) => println!("Response: {:?}", response.candidates[0].get_text().unwrap()),
Err(err) => eprintln!("Error: {}", err),
}
}
Ok(())
}