google_generative_ai 0.4.1 google_generative_ai: ^0.4.1 copied to clipboard
The Google AI Dart SDK enables developers to use Google's state-of-the-art generative AI models (like Gemini).
The Google AI Dart SDK enables developers to use Google's state-of-the-art generative AI models (like Gemini) to build AI-powered features and applications. This SDK supports use cases like:
- Generate text from text-only input
- Generate text from text-and-images input (multimodal)
- Build multi-turn conversations (chat)
- Embedding
Caution
Using the Google AI SDK for Dart (Flutter) to call the Google AI Gemini API directly from your app is recommended for prototyping only. If you plan to enable billing, we strongly recommend that you use the SDK to call the Google AI Gemini API only server-side to keep your API key safe. You risk potentially exposing your API key to malicious actors if you embed your API key directly in your mobile or web app or fetch it remotely at runtime.
Getting started #
Get an API key #
Using the Google AI Dart SDK requires an API key; see https://ai.google.dev/tutorials/setup for how to create one.
Add the package to your project #
Add a dependency on the package:google_generative_ai
package like so:
dart pub add google_generative_ai
or:
flutter pub add google_generative_ai
Additionally, import:
import 'package:google_generative_ai/google_generative_ai.dart';
Use the API #
import 'package:google_generative_ai/google_generative_ai.dart';
const apiKey = ...;
void main() async {
final model = GenerativeModel(
model: 'gemini-1.5-flash-latest',
apiKey: apiKey,
);
final prompt = 'Write a story about a magic backpack.';
final content = [Content.text(prompt)];
final response = await model.generateContent(content);
print(response.text);
};
See additional examples at samples/.
For detailed instructions, you can find a quickstart for the Google AI Dart SDK in the Google documentation: http://ai.google.dev/tutorials/dart_quickstart. This quickstart describes how to add your API key and the SDK to your app, initialize the model, and then call the API to access the model. It also describes some additional use cases and features, like streaming, embedding, counting tokens, and controlling responses.
Additional documentation #
You can find additional documentation for the Google AI SDKs and the Gemini model at ai.google.dev/docs.