googleai_dart 1.1.0 copy "googleai_dart: ^1.1.0" to clipboard
googleai_dart: ^1.1.0 copied to clipboard

Unofficial Dart client for the Google AI Gemini Developer API and Vertex AI Gemini API with unified interface.

example/example.dart

// ignore_for_file: avoid_print

/// A simple example demonstrating basic usage of the googleai_dart package.
///
/// This example shows how to:
/// - Initialize the Google AI client with an API key
/// - Generate content using the Gemini API
/// - Handle the response and print results
///
/// To run this example:
/// 1. Get an API key from https://ai.google.dev/gemini-api/docs/api-key
/// 2. Replace 'YOUR_API_KEY' with your actual key
/// 3. Run: dart run example/example.dart
library;

import 'package:googleai_dart/googleai_dart.dart';

void main() async {
  // Initialize the Google AI client
  final client = GoogleAIClient(
    config: const GoogleAIConfig(authProvider: ApiKeyProvider('YOUR_API_KEY')),
  );

  try {
    // Generate content using Gemini
    final response = await client.models.generateContent(
      model: 'gemini-2.5-flash',
      request: const GenerateContentRequest(
        contents: [
          Content(
            parts: [TextPart('Explain quantum computing in simple terms')],
            role: 'user',
          ),
        ],
      ),
    );

    // Print the generated text
    for (final part in response.candidates?.firstOrNull?.content?.parts ?? []) {
      if (part is TextPart) {
        print('Response: ${part.text}');
      }
    }

    // Print token usage
    if (response.usageMetadata != null) {
      print('\nTokens used: ${response.usageMetadata!.totalTokenCount}');
    }
  } catch (e) {
    print('Error: $e');
  } finally {
    // Clean up resources
    client.close();
  }
}
16
likes
160
points
382
downloads

Publisher

verified publisherlangchaindart.dev

Weekly Downloads

Unofficial Dart client for the Google AI Gemini Developer API and Vertex AI Gemini API with unified interface.

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#nlp #gen-ai #llms #googleai #gemini

Documentation

Documentation
API reference

Funding

Consider supporting this project:

github.com

License

MIT (license)

Dependencies

http, logging

More

Packages that depend on googleai_dart