-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcache_basic.rs
More file actions
122 lines (103 loc) · 4.46 KB
/
cache_basic.rs
File metadata and controls
122 lines (103 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use display_error_chain::DisplayErrorChain;
use gemini_rust::{Gemini, Model};
use std::process::ExitCode;
use std::time::Duration;
use tracing::{error, info, warn};
// Include the story text at compile time
const GRIEF_EATER_STORY: &str = include_str!("../test_data/grief_eater.txt");
#[tokio::main]
async fn main() -> ExitCode {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::level_filters::LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();
match do_main().await {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
let error_chain = DisplayErrorChain::new(e.as_ref());
tracing::error!(error.debug = ?e, error.chained = %error_chain, "execution failed");
ExitCode::FAILURE
}
}
}
async fn do_main() -> Result<(), Box<dyn std::error::Error>> {
let api_key =
std::env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable must be set");
let client = Gemini::with_model(api_key, Model::Gemini25FlashLite)?;
info!("creating cached content with full story text");
// Create cached content with the full story for analysis
let cache = client
.create_cache()
.with_display_name("Grief Eater Story Analysis Cache")?
.with_system_instruction("You are a literary analyst specialized in horror and supernatural fiction. Analyze stories for themes, character development, narrative techniques, and psychological elements.")
.with_user_message("Please read and analyze this story:")
.with_user_message(GRIEF_EATER_STORY)
.with_ttl(Duration::from_secs(3600)) // Cache for 1 hour
.execute()
.await?;
info!(cache_name = cache.name(), "cache created successfully");
// Demonstrate cache retrieval to show token count
info!("retrieving cache information");
let cached_content = cache.get().await?;
info!(
cache_name = cached_content.name,
display_name = cached_content.display_name.as_ref().unwrap_or(&"N/A".to_string()),
model = %cached_content.model,
create_time = %cached_content.create_time,
update_time = %cached_content.update_time,
total_tokens = cached_content.usage_metadata.total_token_count,
"cache details retrieved"
);
if let Some(expire_time) = cached_content.expiration.expire_time {
info!(expire_time = %expire_time, "cache expiration time");
}
// Ask several analytical questions using the cached content
info!("asking question 1: main theme");
let response1 = client
.generate_content()
.with_cached_content(&cache)
.with_user_message("What is the central theme of this story? How does the protagonist's relationship with grief evolve?")
.execute()
.await?;
info!(response = response1.text(), "question 1 response received");
info!("asking question 2: narrative technique");
let response2 = client
.generate_content()
.with_cached_content(&cache)
.with_user_message("Analyze the narrative technique. How does the author use the protagonist's childhood nightmare as a literary device?")
.execute()
.await?;
info!(response = response2.text(), "question 2 response received");
info!("asking question 3: character arc");
let response3 = client
.generate_content()
.with_cached_content(&cache)
.with_user_message("Describe the protagonist's character arc. What does he learn about grief by the end of the story?")
.execute()
.await?;
info!(response = response3.text(), "question 3 response received");
info!("asking question 4: symbolism");
let response4 = client
.generate_content()
.with_cached_content(&cache)
.with_user_message("What symbolic meaning does the grandfather's pocket knife hold in the story's resolution?")
.execute()
.await?;
info!(response = response4.text(), "question 4 response received");
// Clean up by deleting the cache
info!("cleaning up cache");
match cache.delete().await {
Ok(_) => info!("cache deleted successfully"),
Err((cache, error)) => {
error!(error = %error, "failed to delete cache");
warn!(
cache_name = cache.name(),
"cache handle returned for potential retry"
);
}
}
Ok(())
}