google_cloud_ai_generativelanguage_v1beta 0.1.2
google_cloud_ai_generativelanguage_v1beta: ^0.1.2 copied to clipboard
The Google Cloud client library for the Generative Language API.
Generative Language API #
The Google Cloud client library for the Generative Language API.
Tip
Flutter applications should use Firebase AI Logic.
The Generative Language API is meant for Dart command-line, cloud, and server applications. For mobile and web applications, see instead Firebase AI Logic, which provides client-side access to both the Gemini Developer API and Vertex AI.
Note
This package is currently experimental and published under the labs.dart.dev pub publisher in order to solicit feedback.
For packages in the labs.dart.dev publisher we generally plan to either graduate the package into a supported publisher (dart.dev, tools.dart.dev) after a period of feedback and iteration, or discontinue the package. These packages have a much higher expected rate of API and breaking changes.
Your feedback is valuable and will help us evolve this package. For general feedback, suggestions, and comments, please file an issue in the bug tracker.
What's this? #
The Google Cloud client library for the Generative Language API.
The Gemini API allows developers to build generative AI applications using Gemini models. Gemini is our most capable model, built from the ground up to be multimodal. It can generalize and seamlessly understand, operate across, and combine different types of information including language, images, audio, video, and code. You can use the Gemini API for use cases like reasoning across text and images, content generation, dialogue agents, summarization and classification systems, and more.
Quickstart #
This quickstart shows you how to install the package and make your first Gemini API request.
Before you begin #
You need a Gemini API key. If you don't already have one, you can get it for free in Google AI Studio (step-by-step instructions).
Installing the package into your application #
Tip
You can create a skeleton application by running the terminal command: dart create myapp
Run the terminal command:
dart pub add google_cloud_ai_generativelanguage_v1beta
Make your first request #
Here is an example that uses the generateContent method to send a request to the Gemini API using the Gemini 2.5 Flash model.
If you set your API key as the environment variable GEMINI_API_KEY or
GOOGLE_API_KEY, the API key will be picked up automatically by the client
when using the Gemini API libraries. Otherwise you will need to pass your
API key as an argument when initializing the client.
Note
Models names must be prefixed with the string "models/". For example,
"models/gemini-2.5-pro".
import 'package:google_cloud_ai_generativelanguage_v1beta/generativelanguage.dart';
void main() async {
// Pass your API key here if the GEMINI_API_KEY environment variable is not set.
// See https://ai.google.dev/gemini-api/docs/api-key
final service = GenerativeService.fromApiKey();
final request = GenerateContentRequest(
model: 'models/gemini-2.5-flash',
contents: [
Content(parts: [Part(text: "Explain how AI works in a few words")]),
],
);
final result = await service.generateContent(request);
final parts = result.candidates?[0].content?.parts;
if (parts == null) {
print('<No textual response>');
} else {
print(parts.map((p) => p.text ?? '').join(''));
}
service.close();
}