Last updated:
0 purchases
google generative language api
Generative Language API (PaLM) #
A Dart package that provides convenient access to the Generative Language API.
It allows you to interact with the API to generate messages, generate text, embed text, retrieve models,
list available models, and count message tokens.
This package is a third party wrapper around the REST API, it is not an official Google package. With that in mind,
I have tried to make the package as easy to use as possible, adapting the models and documentation to Dart as accurately
as possible. If you encounter any discrepancies, issues, or have suggestions, please file an issue on GitHub.
The PaLM API allows developers to build generative AI applications using the PaLM model.
Large Language Models (LLMs) are a powerful, versatile type of machine learning model that enables computers to
comprehend and generate natural language through a series of prompts.
The PaLM API is based on Google's next generation LLM, PaLM. It excels at a variety of different tasks like code
generation, reasoning, and writing. You can use the PaLM API to build generative AI applications for use cases like
content generation, dialogue agents, summarization and classification systems, and more.
Refer to https://developers.generativeai.google/api/rest/generativelanguage for more information.
Features #
Generate messages using specified models.
Generate text using specified models.
Embed text using specified models.
Retrieve details of a specific model.
List available models with pagination support.
Count the number of tokens in a message.
Getting Started #
Before you can use this package, you need to obtain an API key for the Generative Language API. Follow these steps:
Go to https://makersuite.google.com/app/home
Tap on "Create an API key"
Select either "Create API key in new project" or "Create API key in existing project" depending on your needs.
Copy the API key you generated and save it somewhere safe.
Never share your API key with anyone and never commit it to a public repository.
Please use caution and follow best practices when using API keys.
Once you have your API key, you can install the package by adding it to your pubspec.yaml file:
dependencies:
google_generative_language_api: ^latest
copied to clipboard
Then run flutter pub get in your terminal.
Usage #
Import the package into your Dart file:
import 'package:google_generative_language_api/google_generative_language_api.dart';
copied to clipboard
You can then use the various methods provided by the package like so:
Get model details #
void main() async {
final Model model = await GenerativeLanguageAPI.getModel(
modelName: modelName,
apiKey: 'PALM_API_KEY',
);
print('Model Name: ${model.name}');
print('Description: ${model.description}');
// Print other relevant model details
}
copied to clipboard
List all available models #
void main() async {
final ListModelResponse response =
await GenerativeLanguageAPI.listModels(apiKey: 'PALM_API_KEY');
print('Models:');
for (final Model model in response.models) {
print('Name: ${model.name}');
print('Description: ${model.description}');
// Print other relevant model details
}
if (response.nextPageToken != null) {
print('Next Page Token: ${response.nextPageToken}');
}
}
copied to clipboard
Generate messages #
void main() async {
const request = GenerateMessageRequest(
prompt: MessagePrompt(
messages: [
Message(author: '1', content: 'hi'),
],
),
);
final GeneratedMessage generatedMessage =
await GenerativeLanguageAPI.generateMessage(
modelName: 'models/chat-bison-001',
request: request,
apiKey: 'PALM_API_KEY',
);
print(generatedMessage.messages.map((message) => message.content).join('\n'));
print(generatedMessage.candidates.map((message) => message.content).join('\n'));
}
copied to clipboard
Generate text #
void main() async {
const request = GenerateTextRequest(
prompt: TextPrompt(text: 'Write a story about a magic backpack.'),
temperature: 1.0,
candidateCount: 2,
);
final GeneratedText text = await GenerativeLanguageAPI.generateText(
modelName: 'models/text-bison-001',
request: request,
apiKey: 'PALM_API_KEY',
);
print(text.candidates.map((candidate) => candidate.output).join('\n'));
}
copied to clipboard
Embed Text #
void main() async {
const request = EmbedTextRequest(text: 'say something nice!');
final EmbeddedText embed = await GenerativeLanguageAPI.embedText(
modelName: 'models/embedding-gecko-001',
request: request,
apiKey: 'PALM_API_KEY',
);
print(embed.embedding.values.join('\n'));
}
copied to clipboard
Count tokens in a message #
void main() async {
final int tokenCount = await GenerativeLanguageAPI.countMessageTokens(
modelName: modelName,
request: CountMessageTokensRequest(prompt: prompt),
apiKey: 'PALM_API_KEY',
);
print('Token Count: $tokenCount');
}
copied to clipboard
Additional Information #
For more information, you can refer to the Generative Language API documentation. This package is only
a third party wrapper around the REST API.
If you encounter any issues or have suggestions, please file an issue on GitHub.
Socials #
Website
Twitter
Discord
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.