Skip to main content

context_caching/
context_caching.rs

1use 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)); //Faking big context for example
15    session.ask(InlineData::from_url("https://bitmesra.ac.in/UploadedDocuments/admingo/files/221225_List%20of%20Holiday_2026_26.pdf").await.unwrap());
16
17    let cached_content_req = CachedContentBuilder::new("gemini-2.5-flash")
18        .display_name("Simulated Large Doc")
19        .contents(
20            session
21                .get_history()
22                .into_iter()
23                .map(|e| e.to_owned())
24                .collect(),
25        )
26        .ttl(Duration::from_secs(300))
27        .build()
28        .unwrap();
29
30    println!("Creating cache...");
31    match ai.create_cache(&cached_content_req).await {
32        Ok(cache) => {
33            println!("Cache created: {}", cache.name().as_ref().unwrap());
34
35            // 2. Use the cache in a request
36            let mut session = Session::new(10);
37            let prompt = "Summarize the cached document.";
38            println!("User: {}", prompt);
39
40            // Create a new client instance that uses the cache
41            let ai_with_cache = ai
42                .clone()
43                .set_cached_content(cache.name().as_ref().unwrap());
44
45            match ai_with_cache.ask(session.ask(prompt)).await {
46                Ok(response) => {
47                    println!("\nGemini: {}", response.get_chat().get_text_no_think(""));
48                }
49                Err(e) => eprintln!("Error asking Gemini: {:?}", e),
50            }
51
52            // 3. List caches
53            println!("\nListing caches...");
54            match ai.list_caches().await {
55                Ok(list) => {
56                    if let Some(caches) = list.cached_contents() {
57                        for c in caches {
58                            println!("- {}", c.name().as_ref().unwrap_or(&"Unknown".to_string()));
59                        }
60                    } else {
61                        println!("No caches found.");
62                    }
63                }
64                Err(e) => eprintln!("Error listing caches: {:?}", e),
65            }
66
67            // 4. Delete the cache
68            println!("\nDeleting cache...");
69            match ai.delete_cache(cache.name().as_ref().unwrap()).await {
70                Ok(_) => println!("Cache deleted."),
71                Err(e) => eprintln!("Error deleting cache: {:?}", e),
72            }
73        }
74        Err(e) => {
75            eprintln!("Failed to create cache: {:?}", e);
76        }
77    }
78}