api_gemini 0.5.0

Gemini's API for accessing large language models (LLMs).
Documentation
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Interactive chat with server-side cached content and token management.
//!
//! This example demonstrates advanced Gemini API features:
//! - Interactive chat loop with conversation history
//! - Server-side cached content for context efficiency
//! - Token counting for cache optimization
//! - Real streaming responses with cached context
//! - Cache lifecycle management (create/update/delete)
//! - Cost optimization through intelligent caching
//!
//! ## Features Demonstrated
//!
//! 1. **Server-side Caching**: Creates cached content to store conversation context
//! 2. **Token Management**: Counts tokens to optimize cache usage and costs
//! 3. **Interactive Chat**: Real-time conversation with the AI
//! 4. **Streaming Responses**: Live streaming of AI responses (with streaming feature)
//! 5. **Cache Optimization**: Automatically manages cache size and updates
//! 6. **Cost Efficiency**: Reduces token usage through smart caching
//!
//! ## Usage
//!
//! ```bash
//! # With real streaming (recommended)
//! cargo run --example gemini_chat_cached_interactive --features streaming
//!
//! # With all features
//! cargo run --example gemini_chat_cached_interactive --features full
//!
//! # Basic version without streaming
//! cargo run --example gemini_chat_cached_interactive
//! ```
//!
//! ## Commands
//!
//! - Type messages normally for conversation
//! - `!tokens` - Show current token usage
//! - `!cache info` - Show cache information
//! - `!cache clear` - Clear and recreate cache
//! - `!help` - Show available commands
//! - `quit`, `exit`, `bye` - End conversation and cleanup
//!
//! ## Requirements
//!
//! - Valid GEMINI_API_KEY environment variable
//! - Interactive terminal (not for automated testing)

#[ cfg( feature = "streaming" ) ]
use futures::StreamExt;
use api_gemini::{
  client ::Client,
  models ::*,
  error ::Error,
};
use std::io::{self, Write};
use std::time::{SystemTime, UNIX_EPOCH};

/// Configuration for the cached chat session
struct ChatConfig
{
  /// Maximum tokens to keep in cache before optimization
  max_cache_tokens: i32,
  /// Maximum conversation turns to keep in memory
  max_conversation_turns: usize,
  /// Cache TTL in seconds
  cache_ttl_seconds: u32,
  /// Model to use for the conversation
  model_name: String,
}

impl Default for ChatConfig
{
  fn default() -> Self
  {
    Self {
      max_cache_tokens: 8000,
      max_conversation_turns: 20,
      cache_ttl_seconds: 3600, // 1 hour
      model_name: "gemini-1.5-flash-latest".to_string(),
    }
  }
}

/// Manages the cached conversation state
struct CachedChatSession
{
  client: Client,
  config: ChatConfig,
  conversation_history: Vec< Content >,
  cache_id: Option< String >,
  total_tokens_used: i32,
  cached_tokens: i32,
}

impl CachedChatSession
{
  /// Create a new cached chat session
  async fn new(client: Client) -> Result< Self, Error > 
  {
    let config = ChatConfig::default();

    Ok(Self {
      client,
      config,
      conversation_history: Vec::new(),
      cache_id: None,
      total_tokens_used: 0,
      cached_tokens: 0,
    })
  }

  /// Initialize the conversation with a system prompt and create initial cache
  async fn initialize(&mut self) -> Result< (), Error > 
  {
    println!("🔄 Initializing cached conversation session...");

    // Create initial system context
    let system_content = Content {
      role: "user".to_string(),
      parts : vec![Part {
        text: Some(
        "You are a helpful AI assistant. This is the beginning of our conversation. \
        Please be concise but informative in your responses. You can discuss any topic \
        the user is interested in.".to_string()
        ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      }],
    };

    self.conversation_history.push(system_content.clone());

    // Create cached content for the initial context
    self.create_cache().await?;

    println!("✅ Cache initialized successfully!");
    Ok(())
  }

  /// Create server-side cached content
  async fn create_cache(&mut self) -> Result< (), Error > 
  {
    let timestamp = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs();

    let cache_request = CreateCachedContentRequest {
      model: self.config.model_name.clone(),
      contents: self.conversation_history.clone(),
    ttl : Some(format!("{}s", self.config.cache_ttl_seconds)),
      expire_time: None,
    display_name : Some(format!("Interactive Chat Session {}", timestamp)),
      system_instruction : Some(Content {
        role: "system".to_string(),
        parts : vec![Part {
          text: Some(
          "You are engaging in an interactive conversation. Maintain context \
          from previous messages and provide helpful, accurate responses.".to_string()
          ),
          inline_data: None,
          function_call: None,
          function_response: None,
          ..Default::default()
        }],
      }),
      tools: None,
      tool_config: None,
    };

    let response = self.client.cached_content().create(&cache_request).await?;
    self.cache_id = Some(response.name.clone());

    // Count tokens in the cached content
    self.update_token_counts().await?;

println!("📦 Created cache : {} (ID: {})",
    response.display_name.unwrap_or_else(|| "Unnamed".to_string()),
    response.name
    );

    Ok(())
  }

  /// Update cached content with new conversation history
  async fn update_cache(&mut self) -> Result< (), Error > 
  {
    if let Some(cache_id) = &self.cache_id
    {
      let update_request = UpdateCachedContentRequest {
      ttl : Some(format!("{}s", self.config.cache_ttl_seconds)),
        expire_time: None,
      };

      let _response = self.client.cached_content().update(cache_id, &update_request).await?;
      self.update_token_counts().await?;

      println!("🔄 Cache updated successfully");
    }
    Ok(())
  }

  /// Count tokens in current conversation and update cached token count
  async fn update_token_counts(&mut self) -> Result< (), Error > 
  {
    let count_request = CountTokensRequest {
      contents: self.conversation_history.clone(),
      generate_content_request: None,
    };

    let count_response = self.client
    .models()
    .count_tokens(&self.config.model_name, &count_request)
    .await?;

    self.total_tokens_used = count_response.total_tokens;
    self.cached_tokens = count_response.cached_content_token_count.unwrap_or(0);

    Ok(())
  }

  /// Add a user message to the conversation
  fn add_user_message(&mut self, message: String)
  {
    let content = Content {
      role: "user".to_string(),
      parts : vec![Part {
        text: Some(message),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      }],
    };
    self.conversation_history.push(content);
  }

  /// Add an AI response to the conversation
  fn add_ai_response(&mut self, response: Content)
  {
    self.conversation_history.push(response);
  }

  /// Generate AI response using cached content
  async fn generate_response(&mut self) -> Result< String, Error > 
  {
    let request = GenerateContentRequest {
      contents: self.conversation_history.clone(),
      generation_config : Some(GenerationConfig {
        temperature: Some(0.7),
        max_output_tokens: Some(1024),
        top_p: Some(0.9),
        top_k: Some(40),
        candidate_count: Some(1),
        stop_sequences: None,
      }),
      safety_settings: None,
      tools: None,
      tool_config: None,
      system_instruction: None,
      cached_content: self.cache_id.clone(),
    };

    #[ cfg( feature = "streaming" ) ]
    {
      self.generate_streaming_response(request).await
    }

    #[ cfg( not( feature = "streaming" ) ) ]
    {
      self.generate_regular_response(request).await
    }
  }

  /// Generate streaming response
  #[ cfg( feature = "streaming" ) ]
  async fn generate_streaming_response(&mut self, request: GenerateContentRequest) -> Result< String, Error > 
  {
    let stream = self.client
    .models()
    .by_name(&self.config.model_name)
    .generate_content_stream(&request)
    .await?;

    let mut full_response = String::new();
    let mut response_content: Option< Content > = None;

    futures ::pin_mut!(stream);

    while let Some(chunk) = stream.next().await
    {
      match chunk
      {
        Ok(streaming_response) => {
          if let Some(candidates) = streaming_response.candidates
          {
            if let Some(candidate) = candidates.first()
            {
              if let Some(part) = candidate.content.parts.first()
              {
                if let Some(text) = &part.text
                {
                print!("{}", text);
                  io ::stdout().flush().unwrap();
                  full_response.push_str(text);
                }
              }

              if response_content.is_none()
              {
                response_content = Some(candidate.content.clone());
              }
            }
          }
        }
        Err(e) => {
          return Err(e);
        }
      }
    }

    // Add response to conversation history
    if let Some(content) = response_content
    {
      self.add_ai_response(content);
    } else if !full_response.is_empty()
    {
      let content = Content {
        role: "model".to_string(),
        parts : vec![Part {
          text: Some(full_response.clone()),
          inline_data: None,
          function_call: None,
          function_response: None,
          ..Default::default()
        }],
      };
      self.add_ai_response(content);
    }

    Ok(full_response)
  }

  /// Generate regular (non-streaming) response
  #[ cfg( not( feature = "streaming" ) ) ]
  async fn generate_regular_response(&mut self, request: GenerateContentRequest) -> Result< String, Error > 
  {
    let response = self.client
    .models()
    .by_name(&self.config.model_name)
    .generate_content(&request)
    .await?;

    if let Some(candidate) = response.candidates.first()
    {
      if let Some(part) = candidate.content.parts.first()
      {
        if let Some(text) = &part.text
        {
          // Simulate streaming by printing words with delays
          let words: Vec< &str > = text.split_whitespace().collect();
          for (i, word) in words.iter().enumerate()
          {
          print!("{}", word);
            if i < words.len() - 1
            {
              print!(" ");
            }
            io ::stdout().flush().unwrap();
            tokio ::time::sleep(tokio::time::Duration::from_millis(80)).await;
          }

          self.add_ai_response(candidate.content.clone());
          return Ok(text.clone());
        }
      }
    }

    Err(Error::ApiError("No response generated".to_string()))
  }

  /// Check if cache optimization is needed
  async fn should_optimize_cache(&mut self) -> Result< bool, Error > 
  {
    self.update_token_counts().await?;

    Ok(self.total_tokens_used > self.config.max_cache_tokens ||
    self.conversation_history.len() > self.config.max_conversation_turns)
  }

  /// Optimize cache by removing older messages and updating cache
  async fn optimize_cache(&mut self) -> Result< (), Error > 
  {
  println!("\n🔄 Optimizing cache (current tokens : {})...", self.total_tokens_used);

    // Keep system message and recent conversation
    let keep_count = self.config.max_conversation_turns / 2;
    if self.conversation_history.len() > keep_count + 1
    {
      let mut optimized_history = vec![self.conversation_history[0].clone()]; // Keep system message

      // Keep the most recent messages
      let start_index = self.conversation_history.len().saturating_sub(keep_count);
      optimized_history.extend_from_slice(&self.conversation_history[start_index..]);

      self.conversation_history = optimized_history;
    }

    // Recreate cache with optimized history
    self.delete_cache().await?;
    self.create_cache().await?;

  println!("✅ Cache optimized! New token count : {}", self.total_tokens_used);
    Ok(())
  }

  /// Delete the current cache
  async fn delete_cache(&mut self) -> Result< (), Error > 
  {
    if let Some(cache_id) = &self.cache_id
    {
      self.client.cached_content().delete(cache_id).await?;
      self.cache_id = None;
      println!("🗑️ Cache deleted");
    }
    Ok(())
  }

  /// Show token usage information
  async fn show_token_info(&mut self) -> Result< (), Error > 
  {
    self.update_token_counts().await?;

    println!("\n📊 Token Usage Information:");
  println!("  Total tokens in conversation : {}", self.total_tokens_used);
  println!("  Cached tokens : {}", self.cached_tokens);
  println!("  Conversation turns : {}", self.conversation_history.len() - 1); // Exclude system message
  println!("  Cache ID: {}", self.cache_id.as_deref().unwrap_or("None"));
  println!("  Max cache tokens (before optimization): {}", self.config.max_cache_tokens);
    println!();

    Ok(())
  }

  /// Show cache information
  async fn show_cache_info(&mut self) -> Result< (), Error > 
  {
    if let Some(cache_id) = &self.cache_id
    {
      match self.client.cached_content().get(cache_id).await
      {
        Ok(cache_info) => {
          println!("\n📦 Cache Information:");
        println!("  Cache ID: {}", cache_info.name);
        println!("  Display Name : {}", cache_info.display_name.unwrap_or_else(|| "Unnamed".to_string()));
        println!("  Model : {}", cache_info.model);
        println!("  Expire Time : {}", cache_info.expire_time.as_deref().unwrap_or("No expiration"));
        println!("  Create Time : {}", cache_info.create_time.unwrap_or_else(|| "Unknown".to_string()));
        println!("  Update Time : {}", cache_info.update_time.unwrap_or_else(|| "Unknown".to_string()));
          println!();
        }
        Err(e) => {
        println!("❌ Error retrieving cache info : {}", e);
        }
      }
    } else {
      println!("❌ No active cache");
    }
    Ok(())
  }

  /// Clear cache and recreate
  async fn clear_cache(&mut self) -> Result< (), Error > 
  {
    println!("🔄 Clearing and recreating cache...");
    self.delete_cache().await?;

    // Reset conversation but keep system message
    if !self.conversation_history.is_empty()
    {
      self.conversation_history = vec![self.conversation_history[0].clone()];
    }

    self.create_cache().await?;
    println!("✅ Cache cleared and recreated!");
    Ok(())
  }

  /// Show help information
  fn show_help(&self)
  {
    println!("\n📖 Available Commands:");
    println!("  !tokens           - Show current token usage");
    println!("  !cache info       - Show cache information");
    println!("  !cache clear      - Clear and recreate cache");
    println!("  !help             - Show this help message");
    println!("  quit/exit/bye     - End conversation");
    println!("\n💡 Tips:");
  println!("  - Cache automatically optimizes when reaching {} tokens", self.config.max_cache_tokens);
    println!("  - Cached content reduces token costs for repeated context");
    println!("  - Use !tokens to monitor your usage");
    println!();
  }

  /// Cleanup resources
  async fn cleanup(&mut self) -> Result< (), Error > 
  {
    if let Some(cache_id) = &self.cache_id
    {
      println!("🧹 Cleaning up cache...");
      self.client.cached_content().delete(cache_id).await?;
      println!("✅ Cache deleted successfully");
    }
    Ok(())
  }
}

#[ tokio::main ]
#[ allow( clippy::too_many_lines ) ]
async fn main() -> Result<(), Box< dyn core::error::Error > > 
{
  // Initialize client
  let client = match Client::new()
  {
    Ok(client) => client,
    Err(e) => {
    eprintln!("❌ Failed to create client : {}", e);
      eprintln!("💡 Make sure GEMINI_API_KEY environment variable is set");
      return Ok(());
    }
  };

  println!("🤖 Interactive Cached Chat with Gemini");
  println!("=====================================");
  println!("This demo showcases server-side caching with token management.");
  println!("Type '!help' for commands or start chatting!\n");

  // Initialize chat session
  let mut session = CachedChatSession::new(client).await?;

  // Initialize cache
  if let Err(e) = session.initialize().await
  {
  eprintln!("❌ Failed to initialize session : {}", e);
    return Ok(());
  }

  // Main chat loop
  loop
  {
    // Get user input
    print!("\n👤 You: ");
    io ::stdout().flush()?;

    let mut input = String::new();
    match io::stdin().read_line(&mut input)
    {
      Ok(0) => {
        println!("\n❌ No input available. Use this example in interactive terminal only.");
        break;
      }
    Ok(_) => {}
      Err(e) => {
      println!("\n❌ Error reading input : {}", e);
        break;
      }
    }

    let user_message = input.trim().to_string();

    // Handle empty input
    if user_message.is_empty()
    {
      continue;
    }

    // Handle exit commands
    if matches!(user_message.to_lowercase().as_str(), "quit" | "exit" | "bye")
    {
      println!("\n👋 Goodbye! Thanks for chatting!");
      break;
    }

    // Handle special commands
    match user_message.as_str()
    {
      "!help" => {
        session.show_help();
        continue;
      }
      "!tokens" => {
        if let Err(e) = session.show_token_info().await
        {
        println!("❌ Error showing token info : {}", e);
        }
        continue;
      }
      "!cache info" => {
        if let Err(e) = session.show_cache_info().await
        {
        println!("❌ Error showing cache info : {}", e);
        }
        continue;
      }
      "!cache clear" => {
        if let Err(e) = session.clear_cache().await
        {
        println!("❌ Error clearing cache : {}", e);
        }
        continue;
      }
    _ => {}
    }

    // Add user message to conversation
    session.add_user_message(user_message);

    // Check if cache optimization is needed
    if let Ok(true) = session.should_optimize_cache().await
    {
      if let Err(e) = session.optimize_cache().await
      {
      println!("⚠️ Cache optimization failed : {}", e);
      }
    }

    // Generate AI response
    print!("\n🤖 AI: ");
    io ::stdout().flush()?;

    match session.generate_response().await
    {
      Ok(_response) => {
        println!("\n");

        // Update cache after successful response
        if let Err(e) = session.update_cache().await
        {
        println!("⚠️ Cache update failed : {}", e);
        }
      }
      Err(e) => {
      println!("\n❌ Error generating response : {}", e);
        println!("💡 Please try again or type 'quit' to exit.");
      }
    }
  }

  // Cleanup
  if let Err(e) = session.cleanup().await
  {
  eprintln!("⚠️ Cleanup error : {}", e);
  }

  Ok(())
}