context_caching/
context_caching.rs1use gemini_client_api::gemini::ask::Gemini;
2use gemini_client_api::gemini::types::caching::CachedContentBuilder;
3use gemini_client_api::gemini::types::request::InlineData;
4use gemini_client_api::gemini::types::sessions::Session;
5use std::env;
6use std::time::Duration;
7
8#[tokio::main]
9async fn main() {
10 let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY must be set");
11 let ai = Gemini::new(api_key, "gemini-2.5-flash", None);
12 let mut session = Session::new(10);
13
14 session.ask("What is there in this pdf".repeat(200))
16 .ask(InlineData::from_url("https://bitmesra.ac.in/UploadedDocuments/admingo/files/221225_List%20of%20Holiday_2026_26.pdf").await.unwrap());
17
18 let cached_content_req = CachedContentBuilder::new("gemini-2.5-flash")
19 .display_name("Simulated Large Doc")
20 .contents(session.get_history_owned().into_iter().collect())
21 .ttl(Duration::from_secs(300))
22 .build()
23 .unwrap();
24
25 println!("Creating cache...");
26 match ai.create_cache(&cached_content_req).await {
27 Ok(cache) => {
28 println!("Cache created: {}", cache.name().as_ref().unwrap());
29
30 let mut session = Session::new(10);
32 let prompt = "Summarize the cached document.";
33 println!("User: {}", prompt);
34
35 let ai_with_cache = ai
37 .clone()
38 .set_cached_content(cache.name().as_ref().unwrap());
39
40 match ai_with_cache.ask(session.ask(prompt)).await {
41 Ok(response) => {
42 println!("\nGemini: {}", response.get_chat().get_text_no_think(""));
43 }
44 Err(e) => eprintln!("Error asking Gemini: {:?}", e),
45 }
46
47 println!("\nListing caches...");
49 match ai.list_caches().await {
50 Ok(list) => {
51 if let Some(caches) = list.cached_contents() {
52 for c in caches {
53 println!("- {}", c.name().as_ref().unwrap_or(&"Unknown".to_string()));
54 }
55 } else {
56 println!("No caches found.");
57 }
58 }
59 Err(e) => eprintln!("Error listing caches: {:?}", e),
60 }
61
62 println!("\nDeleting cache...");
64 match ai.delete_cache(cache.name().as_ref().unwrap()).await {
65 Ok(_) => println!("Cache deleted."),
66 Err(e) => eprintln!("Error deleting cache: {:?}", e),
67 }
68 }
69 Err(e) => {
70 eprintln!("Failed to create cache: {:?}", e);
71 }
72 }
73}