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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Tests to validate that examples use the API correctly

use api_gemini::{ client::Client, models::* };
use serde_json::json;

/// Create client for integration tests - REQUIRES real API key
/// Fails immediately if no valid API key is found
fn create_integration_client() -> Client
{
  Client::new().unwrap_or_else( |err| {
    panic!( 
    "\n❌ INTEGRATION TEST FAILURE: No valid API key found!\n\
    \n🔑 Required: Set GEMINI_API_KEY environment variable or create secret/gemini_api_key file\n\
    \n📋 This integration test validates API structure with real API calls\n\
    \n🚫 No API key available from any source\n\
    \n💡 For unit tests only (no API), run: cargo test --no-default-features\n\
  \nOriginal error : {err:?}" 
    );
  })
}

#[ test ]
fn test_chat_example_structure()
{
  // Verify the chat example uses correct request structure
  let request = GenerateContentRequest
  {
    contents: vec!
    [
    Content
    {
      parts: vec!
      [
      Part
      {
        text: Some( "Hello! Can you explain what artificial intelligence is in simple terms?".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    }
    ],
    generation_config: Some( GenerationConfig
    {
      temperature: Some( 0.7 ),
      top_k: Some( 40 ),
      top_p: Some( 0.95 ),
      candidate_count: Some( 1 ),
      max_output_tokens: Some( 1024 ),
      stop_sequences: None,
    }),
    safety_settings: Some( vec!
    [
    SafetySetting
    {
      category: "HARM_CATEGORY_HARASSMENT".to_string(),
      threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
    },
    SafetySetting
    {
      category: "HARM_CATEGORY_HATE_SPEECH".to_string(),
      threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
    }
    ]),
    tools: None,
    tool_config: None,
    system_instruction: None,
    cached_content: None,
  };

  // Verify serialization works
  let json = serde_json::to_string( &request ).unwrap();
  assert!( json.contains( "contents" ) );
  assert!( json.contains( "generationConfig" ) );
  assert!( json.contains( "safetySettings" ) );
}

#[ test ]
fn test_multi_turn_conversation_structure()
{
  // Verify multi-turn conversation structure
  let conversation =
  [
  Content
  {
    role: "user".to_string(),
    parts: vec!
    [
    Part
    {
      text: Some( "What is the capital of France?".to_string() ),
      ..Default::default()
    }
    ],
  },
  Content
  {
    role: "model".to_string(),
    parts: vec!
    [
    Part
    {
      text: Some( "The capital of France is Paris.".to_string() ),
      ..Default::default()
    }
    ],
  },
  Content
  {
    role: "user".to_string(),
    parts: vec!
    [
    Part
    {
      text: Some( "What's the population?".to_string() ),
      ..Default::default()
    }
    ],
  },
  ];

  assert_eq!( conversation.len(), 3 );
  assert_eq!( conversation[ 0 ].role, "user" );
  assert_eq!( conversation[ 1 ].role, "model" );
  assert_eq!( conversation[ 2 ].role, "user" );
}

#[ test ]
fn test_embeddings_example_structure()
{
  // Verify embeddings request structure
  let embed_request = EmbedContentRequest
  {
    content: Content
    {
      role: "user".to_string(),
      parts: vec!
      [
      Part
      {
        text: Some( "The quick brown fox".to_string() ),
        ..Default::default()
      }
      ],
    },
    task_type: Some( "RETRIEVAL_DOCUMENT".to_string() ),
    title: None,
    output_dimensionality: None,
  };

  let json = serde_json::to_string( &embed_request ).unwrap();
  assert!( json.contains( "content" ) );
  assert!( json.contains( "taskType" ) );
  assert!( json.contains( "RETRIEVAL_DOCUMENT" ) );
}

#[ test ]
fn test_function_calling_structure()
{
  // Verify function calling structure
  let tools = vec!
  [
  Tool
  {
    function_declarations: Some( vec!
    [
    FunctionDeclaration
    {
      name: "get_weather".to_string(),
      description: "Get the current weather in a given location".to_string(),
      parameters: Some( json!
      ({
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city name"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"],
            "description": "The temperature unit to use"
          }
        },
        "required": ["location"]
      })),
    }
    ]),
    code_execution: None,
    google_search_retrieval: None,
    code_execution_tool: None,
  }
  ];

  let json = serde_json::to_string( &tools ).unwrap();
  assert!( json.contains( "functionDeclarations" ) );
  assert!( json.contains( "get_weather" ) );
  assert!( json.contains( "parameters" ) );
}

#[ test ]
fn test_multimodal_structure()
{
  // Verify multimodal request with image
  use base64::Engine;

  let test_image_data = vec![ 0x89, 0x50, 0x4E, 0x47 ]; // PNG header
  let base64_image = base64::engine::general_purpose::STANDARD.encode( &test_image_data );

  let request = GenerateContentRequest
  {
    contents: vec!
    [
    Content
    {
      parts: vec!
      [
      Part
      {
        text: Some( "What's in this image?".to_string() ),
        ..Default::default()
      },
      Part
      {
        inline_data: Some( Blob
        {
          mime_type: "image/png".to_string(),
          data: base64_image.clone(),
        }),
        ..Default::default()
      },
      ],
      role: "user".to_string(),
    }
    ],
    ..Default::default()
  };

  assert_eq!( request.contents[ 0 ].parts.len(), 2 );
  assert!( request.contents[ 0 ].parts[ 1 ].inline_data.is_some() );

  let blob = request.contents[ 0 ].parts[ 1 ].inline_data.as_ref().unwrap();
  assert_eq!( blob.mime_type, "image/png" );
  assert_eq!( blob.data, base64_image );
}

#[ test ]
fn test_safety_settings_structure()
{
  // Verify safety settings
  let safety_settings = vec!
  [
  SafetySetting
  {
    category: "HARM_CATEGORY_HARASSMENT".to_string(),
    threshold: "BLOCK_LOW_AND_ABOVE".to_string(),
  },
  SafetySetting
  {
    category: "HARM_CATEGORY_HATE_SPEECH".to_string(),
    threshold: "BLOCK_ONLY_HIGH".to_string(),
  },
  ];

  let json = serde_json::to_string( &safety_settings ).unwrap();
  assert!( json.contains( "HARM_CATEGORY_HARASSMENT" ) );
  assert!( json.contains( "BLOCK_LOW_AND_ABOVE" ) );
  assert!( json.contains( "HARM_CATEGORY_HATE_SPEECH" ) );
  assert!( json.contains( "BLOCK_ONLY_HIGH" ) );
}

#[ test ]
fn test_error_handling_client_builder()
{
  // Test that error handling example's client builder pattern works
  let result = Client::builder()
  .api_key( "test-key".to_string() )
  .build();

  assert!( result.is_ok() );

  // Test empty API key error
  let result = Client::builder()
  .api_key( String::new() )
  .build();

  assert!( result.is_err() );
  match result.unwrap_err()
  {
    api_gemini ::error::Error::AuthenticationError( msg ) =>
    {
      assert_eq!( msg, "API key cannot be empty" );
    },
    _ => panic!( "Expected AuthenticationError" ),
  }
}

#[ test ]
fn test_model_list_response()
{
  // Verify model list response structure
  let models = ListModelsResponse
  {
    models: vec!
    [
    Model
    {
      name: "models/gemini-flash-latest".to_string(),
      display_name: Some( "Gemini 1.5 Pro Latest".to_string() ),
      description: Some( "Our most capable model".to_string() ),
      input_token_limit: Some( 1_048_576 ),
      output_token_limit: Some( 8192 ),
      supported_generation_methods: Some( vec!
      [
      "generateContent".to_string(),
      "embedContent".to_string(),
      ]),
      temperature: Some( 1.0 ),
      top_p: Some( 0.95 ),
      top_k: Some( 64 ),
      version: Some( "001".to_string() ),
    }
    ],
    next_page_token: None,
  };

  assert_eq!( models.models.len(), 1 );
  assert_eq!( models.models[ 0 ].name, "models/gemini-flash-latest" );
  assert!( models.models[ 0 ].supported_generation_methods.as_ref().unwrap().contains( &"generateContent".to_string() ) );
}

// ==============================================================================
// INTEGRATION TESTS - Real API validation of structures
// ==============================================================================

#[ tokio::test ]
async fn integration_test_chat_example_real_api()
{
  let client = create_integration_client();
  
  // Test the exact structure from the unit test but with real API
  let request = GenerateContentRequest
  {
    contents: vec!
    [
    Content
    {
      parts: vec!
      [
      Part
      {
        text: Some( "Hello! Can you explain what artificial intelligence is in one sentence?".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    }
    ],
    generation_config: Some( GenerationConfig
    {
      temperature: Some( 0.7 ),
      top_k: Some( 40 ),
      top_p: Some( 0.95 ),
      candidate_count: Some( 1 ),
      max_output_tokens: Some( 600 ), // Increased to avoid truncation
      stop_sequences: None,
    }),
    safety_settings: Some( vec!
    [
    SafetySetting
    {
      category: "HARM_CATEGORY_HARASSMENT".to_string(),
      threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
    },
    SafetySetting
    {
      category: "HARM_CATEGORY_HATE_SPEECH".to_string(),
      threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
    }
    ]),
    tools: None,
    tool_config: None,
    system_instruction: None,
    cached_content: None,
  };

  let response = client.models().by_name( "gemini-flash-latest" )
  .generate_content( &request )
  .await
  .expect( "Chat example structure should work with real API" );

  // Validate response structure
  assert!( !response.candidates.is_empty(), "Real API should return candidates" );
  assert!( response.candidates[ 0 ].content.parts[ 0 ].text.is_some(), "Real API should return text" );
  
  let response_text = response.candidates[ 0 ].content.parts[ 0 ].text.as_ref().unwrap();
  assert!( !response_text.is_empty(), "Real API should return non-empty response" );
  assert!( response_text.to_lowercase().contains( "artificial" ) || response_text.to_lowercase().contains( "ai" ), 
"Response should relate to AI: {response_text}" );
}

#[ tokio::test ]
async fn integration_test_embeddings_example_real_api()
{
  let client = create_integration_client();
  
  // Test the exact structure from the unit test but with real API
  let embed_request = EmbedContentRequest
  {
    content: Content
    {
      role: "user".to_string(),
      parts: vec!
      [
      Part
      {
        text: Some( "The quick brown fox jumps over the lazy dog".to_string() ),
        ..Default::default()
      }
      ],
    },
    task_type: Some( "RETRIEVAL_DOCUMENT".to_string() ),
    title: None,
    output_dimensionality: None,
  };

  let response = client.models().by_name( "text-embedding-004" )
  .embed_content( &embed_request )
  .await
  .expect( "Embeddings example structure should work with real API" );

  // Validate response structure
  assert!( response.embedding.values.len() > 100, "Real API should return meaningful embedding vector" );
  
  // Verify embedding values are reasonable
  let embedding_sum: f32 = response.embedding.values.iter().sum();
  assert!( embedding_sum.abs() > 0.001, "Embedding should have non-zero values" );
}

#[ tokio::test ]
async fn integration_test_function_calling_example_real_api()
{
  let client = create_integration_client();
  
  // Test the exact structure from the unit test but with real API
  let tools = vec!
  [
  Tool
  {
    function_declarations: Some( vec!
    [
    FunctionDeclaration
    {
      name: "get_weather".to_string(),
      description: "Get the current weather in a given location".to_string(),
      parameters: Some( json!
      ({
        "type": "object",
        "properties": {
          "location": {
            "type": "string",
            "description": "The city name"
          },
          "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"],
            "description": "The temperature unit to use"
          }
        },
        "required": ["location"]
      })),
    }
    ]),
    code_execution: None,
    google_search_retrieval: None,
    code_execution_tool: None,
  }
  ];

  let request = GenerateContentRequest
  {
    contents: vec!
    [
    Content
    {
      parts: vec!
      [
      Part
      {
        text: Some( "What's the weather like in Paris? Please use the get_weather function.".to_string() ),
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    }
    ],
    tools: Some( tools ),
    ..Default::default()
  };

  let response = client.models().by_name( "gemini-flash-latest" )
  .generate_content( &request )
  .await
  .expect( "Function calling example structure should work with real API" );

  // Validate response structure
  assert!( !response.candidates.is_empty(), "Real API should return candidates" );
  
  // Should either call the function or explain why it can't
  let has_function_call = response.candidates[ 0 ].content.parts.iter()
  .any( |part| part.function_call.is_some() );
  let has_text_response = response.candidates[ 0 ].content.parts.iter()
  .any( |part| part.text.is_some() );
  
  assert!( has_function_call || has_text_response, 
  "Real API should either call function or provide text response" );
}

#[ tokio::test ]
async fn integration_test_multimodal_example_real_api()
{
  let client = create_integration_client();
  
  // Test multimodal structure without actual image data - focus on API structure validation
  // This tests the multimodal capability by using text-only content but with multimodal structure
  let request = GenerateContentRequest
  {
    contents: vec!
    [
    Content
    {
      parts: vec!
      [
      Part
      {
        text: Some( "Describe a simple image: a red circle on a white background.".to_string() ),
        ..Default::default()
      },
      Part
      {
        text: Some( "[Image description: A red circle on a white background]".to_string() ),
        ..Default::default()
      },
      ],
      role: "user".to_string(),
    }
    ],
    generation_config: Some( GenerationConfig
    {
      max_output_tokens: Some( 600 ),
      ..Default::default()
    }),
    ..Default::default()
  };

  let response = client.models().by_name( "gemini-flash-latest" )
  .generate_content( &request )
  .await
  .expect( "Multimodal example structure should work with real API" );

  // Validate multimodal response structure - the key is that the API accepts multiple Parts
  assert!( !response.candidates.is_empty(), "Real API should return candidates for multimodal structure" );
  assert!( response.candidates[ 0 ].content.parts[ 0 ].text.is_some(), "Real API should return text" );
  
  let response_text = response.candidates[ 0 ].content.parts[ 0 ].text.as_ref().unwrap();
  assert!( !response_text.is_empty(), "Real API should return non-empty response" );
  assert!( response_text.len() > 10, "Response should be substantive for multimodal input" );
}

#[ tokio::test ]
async fn integration_test_safety_settings_example_real_api()
{
  let client = create_integration_client();
  
  // Test safety settings with a request that might trigger content filtering
  let request = GenerateContentRequest
  {
    contents: vec!
    [
    Content
    {
      parts: vec!
      [
      Part
      {
        text: Some( "Write a story about friendship and cooperation.".to_string() ), // Safe content
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    }
    ],
    safety_settings: Some( vec!
    [
    SafetySetting
    {
      category: "HARM_CATEGORY_HARASSMENT".to_string(),
      threshold: "BLOCK_LOW_AND_ABOVE".to_string(),
    },
    SafetySetting
    {
      category: "HARM_CATEGORY_HATE_SPEECH".to_string(),
      threshold: "BLOCK_ONLY_HIGH".to_string(),
    },
    ]),
    generation_config: Some( GenerationConfig
    {
      max_output_tokens: Some( 600 ),
      ..Default::default()
    }),
    ..Default::default()
  };

  let response = client.models().by_name( "gemini-flash-latest" )
  .generate_content( &request )
  .await
  .expect( "Safety settings example structure should work with real API" );

  // Validate response structure
  assert!( !response.candidates.is_empty(), "Real API should return candidates with safe content" );
  assert!( response.candidates[ 0 ].content.parts[ 0 ].text.is_some(), "Real API should return text" );
  
  let response_text = response.candidates[ 0 ].content.parts[ 0 ].text.as_ref().unwrap();
  assert!( !response_text.is_empty(), "Real API should return non-empty response" );
  // Verify the model responded appropriately to the safe content request
  // The response should be substantive (not just an error or refusal)
assert!( response_text.len() > 20, "Response should be substantive for safe content : {response_text}" );
  
  // Accept any positive, creative response that doesn't indicate content blocking
  let response_lower = response_text.to_lowercase();
  let contains_story_elements = response_lower.contains( "friend" ) || 
  response_lower.contains( "story" ) ||
  response_lower.contains( "cooperation" ) ||
  response_lower.contains( "together" ) ||
  response_lower.contains( "help" ) ||
  response_lower.contains( "kind" );
                               
  // If it doesn't contain expected elements, ensure it's at least a creative narrative
  if !contains_story_elements
  {
    // Should be a narrative (contains narrative elements)
    let is_narrative = response_lower.contains( "once" ) ||
    response_lower.contains( "there" ) ||
    response_lower.contains( "was" ) ||
    response_lower.contains( "said" ) ||
    response_text.split( '.' ).count() > 2; // Multiple sentences
  assert!( is_narrative, "Response should either relate to friendship/cooperation or be a creative narrative : {response_text}" );
  }
}

#[ tokio::test ]
async fn integration_test_model_list_example_real_api()
{
  let client = create_integration_client();
  
  // Test model listing with real API
  let models_response = client.models()
  .list()
  .await
  .expect( "Model list should work with real API" );

  // Validate response structure matches our unit test expectations
  assert!( !models_response.models.is_empty(), "Real API should return available models" );
  
  // Find a gemini model
  let gemini_models: Vec< _ > = models_response.models.iter()
  .filter( |model| model.name.contains( "gemini" ) )
  .collect();
  
  assert!( !gemini_models.is_empty(), "Real API should include Gemini models" );
  
  // Validate model structure
  let first_model = &gemini_models[ 0 ];
  assert!( !first_model.name.is_empty(), "Model should have name" );
  assert!( first_model.display_name.is_some(), "Model should have display name" );
  assert!( first_model.supported_generation_methods.is_some(), "Model should have supported methods" );
  
  let methods = first_model.supported_generation_methods.as_ref().unwrap();
  assert!( methods.contains( &"generateContent".to_string() ) || methods.contains( &"embedContent".to_string() ),
  "Model should support at least one generation method" );
}