gemini_live 0.1.0 copy "gemini_live: ^0.1.0" to clipboard
gemini_live: ^0.1.0 copied to clipboard

A Flutter package for using the experimental Gemini Live API, enabling real-time, multimodal conversations with Google's Gemini models.

Flutter Gemini Live #

pub version License Platform

[English] | [한국어]


  • A Flutter package for using the experimental Gemini Live API, enabling real-time, multimodal conversations with Google's Gemini models.
  • no Firebase / Firebase AI Logic dependency
  • support for the gemini-2.0-flash-live-001 model.
  • support Text, Audio response_modalities

https://github.com/user-attachments/assets/7d826f37-196e-4ddd-8828-df66db252e8e

✨ Features #

  • Real-time Communication: Establishes a WebSocket connection for low-latency, two-way interaction.
  • Multimodal Input: Send text, images, and audio in a single conversational turn.
  • Streaming Responses: Receive text responses from the model as they are being generated.
  • Easy-to-use Callbacks: Simple event-based handlers for onOpen, onMessage, onError, and onClose.
Demo 1: Chihuahua vs muffin Demo 2: Labradoodle vs fried chicken
실시간 대화 데모 멀티모달 입력 데모
Chihuahua vs muffin Labradoodle vs fried chicken

🏁 Getting Started #

Prerequisites #

You need a Google Gemini API key to use this package. You can get your key from Google AI Studio.

Installation #

Add the package to your pubspec.yaml file:

dependencies:
  gemini_live: ^0.1.0 # Use the latest version

or run this command(Recommend):

flutter pub add gemini_live

Install the package from your terminal:

flutter pub get

Now, import the package in your Dart code:

import 'package:gemini_live/gemini_live.dart';

🚀 Usage #

Here is a basic example of how to use the gemini_live package to start a session and send a message.

Security Note: Do not hardcode your API key. It is highly recommended to use a .env file with a package like flutter_dotenv to keep your credentials secure.

import 'package:gemini_live/gemini_live.dart';

// 1. Initialize Gemini with your API key
final genAI = GoogleGenAI(apiKey: 'YOUR_API_KEY_HERE');
LiveSession? session;

// 2. Connect to the Live API
Future<void> connect() async {
  try {
    session = await genAI.live.connect(
      model: 'gemini-2.0-flash-live-001',
      callbacks: LiveCallbacks(
        onOpen: () => print('✅ Connection opened'),
        onMessage: (LiveServerMessage message) {
          // 3. Handle incoming messages from the model
          if (message.text != null) {
            print('Received chunk: ${message.text}');
          }
          if (message.serverContent?.turnComplete ?? false) {
            print('✅ Turn complete!');
          }
        },
        onError: (e, s) => print('🚨 Error: $e'),
        onClose: (code, reason) => print('🚪 Connection closed'),
      ),
    );
  } catch (e) {
    print('Connection failed: $e');
  }
}

// 4. Send a message to the model
void sendMessage(String text) {
  session?.sendMessage(
    LiveClientMessage(
      clientContent: LiveClientContent(
        turns: [
          Content(
            role: "user",
            parts: [Part(text: text)],
          ),
        ],
        turnComplete: true,
      ),
    ),
  );
}

💬 Live Chat Demo #

This repository includes a comprehensive example application demonstrating the features of the gemini_live package.

Running the Demo App #

  1. Get an API Key: Make sure you have a Gemini API key from Google AI Studio.

  2. Set Up the Project:

    • Clone this repository.
    • Open the example/lib/main.dart file and insert your API key:
      // example/lib/main.dart
      const String geminiApiKey = 'YOUR_API_KEY_HERE';
      
    • Configure platform permissions for microphone and photo library access as needed.
    • Run flutter pub get in the example directory.
  3. Run the App:

    cd example
    flutter run
    

How to Use the App #

  1. Connect: The app will attempt to connect to the Gemini API automatically. If the connection fails, tap the "Reconnect" button.

  2. Send a Text Message:

    • Type your message in the text field at the bottom.
    • Tap the send (▶️) icon.
  3. Send a Message with an Image:

    • Tap the image (🖼️) icon to open your gallery.
    • Select an image. A preview will appear.
    • (Optional) Type a question about the image.
    • Tap the send (▶️) icon.
  4. Send a Voice Message:

    • Tap the microphone (🎤) icon. Recording will start, and the icon will change to a red stop (⏹️) icon.
    • Speak your message.
    • Tap the stop (⏹️) icon again to finish. The audio will be sent automatically.

🤝 Contributing #

Contributions of all kinds are welcome, including bug reports, feature requests, and pull requests! Please feel free to open an issue on the issue tracker.

  1. Fork this repository.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'Add some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request.

📜 License #

See the LICENSE file for more details.

3
likes
130
points
274
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for using the experimental Gemini Live API, enabling real-time, multimodal conversations with Google's Gemini models.

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, http, json_annotation, web_socket_client

More

Packages that depend on gemini_live