Last updated:
0 purchases
gemini ai
Gemini API Flutter Plugin #
Flutter plugin of Gemini AI for Flutter Developers that uses native Android and iOS libraries.
My personal website: cihanurtekin.com
NOTE: This plugin is not a simple package that just makes web calls. It is a plugin that includes Gemini AI's native Android and iOS libraries.
Usage #
To experience Gemini online: Google Maker Suite
Generative Model #
A facilitator for a given multimodal model (eg; Gemini).
import 'package:gemini_ai/model/generative_model.dart';
GenerativeModel generativeModel = GenerativeModel(
modelName: "gemini-pro",
apiKey: "YOUR_API_KEY_HERE",
generationConfig: _generationConfig,
safetySettings: _safetySettings,
);
copied to clipboard
modelName (String): Name of the model in the backend
apiKey (String): Authentication key for interacting with the backend
generationConfig (GenerationConfig?): Configuration parameters to use for content generation
safetySettings (List<SafetySetting>): The safety bounds to use during alongside prompts during content generation
Generation Config #
Configuration parameters to use for content generation.
import 'package:gemini_ai/model/generation_config.dart';
GenerationConfig _generationConfig = GenerationConfig(
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 2048,
);
copied to clipboard
temperature (double?): The degree of randomness in token selection, typically between 0 and 1
topK (int?): The sum of probabilities to collect to during token selection
topP (double?): How many tokens to select amongst the highest probabilities
candidateCount (int?): The max unique responses to return
maxOutputTokens (int?): The max tokens to generate per response
stopSequences (List<String>?): A list of strings to stop generation on occurrence of
Safety Settings #
A configuration for a BlockThreshold of some HarmCategory allowed and blocked in responses.
import 'package:gemini_ai/model/safety_setting.dart';
import 'package:gemini_ai/enum/block_threshold.dart';
import 'package:gemini_ai/enum/harm_category.dart';
List<SafetySetting> _safetySettings = [
SafetySetting(
HarmCategory.harassment,
BlockThreshold.mediumAndAbove,
),
SafetySetting(
HarmCategory.hateSpeech,
BlockThreshold.mediumAndAbove,
),
SafetySetting(
HarmCategory.sexuallyExplicit,
BlockThreshold.mediumAndAbove,
),
SafetySetting(
HarmCategory.dangerousContent,
BlockThreshold.mediumAndAbove,
),
];
copied to clipboard
HarmCategory (enum) : Category for a given harm rating.
unknown : A new and not yet supported value.
harassment : Harassment content.
hateSpeech : Hate speech and content.
sexuallyExplicit : Sexually explicit content.
dangerousContent : Dangerous content.
BlockThreshold (enum) : Represents the threshold for some HarmCategory that is allowed and blocked by SafetySettings.
unspecified : The threshold was not specified.
lowAndAbove : Content with negligible harm is allowed.
mediumAndAbove : Content with negligible to low harm is allowed.
onlyHigh : Content with negligible to medium harm is allowed.
none : All content is allowed regardless of harm.
Generate content #
Get response from Gemini AI with only one line of code using the generateContent method. For a full example, you can visit the example tab.
GeminiAi : The class used to call Gemini functions and properties
generateContent : Generates a response from the backend with the provided text represented Content.
prompt : The text to be converted into a single piece of Content to send to the model.
Returns a response String after some delay (async).
String? content = await _gemini.generateContent(
GeminiConfig.generativeModel,
"Your message to Gemini",
);
copied to clipboard
Text and Image Input (Multimodal) #
Gemini provides a multimodal model (gemini-pro-vision), so you can input both text and images. Make sure to review the image requirements for prompts.
When the prompt input includes both text and images, use the gemini-pro-vision model with generateContent to generate text output.
images : List of images in File type. You can add as many images as you want to your prompt.
GenerativeModel generativeModel = GenerativeModel(
modelName: "gemini-pro-vision",
apiKey: "YOUR_API_KEY_HERE",
generationConfig: _generationConfig,
safetySettings: _safetySettings,
);
copied to clipboard
String? content = await _gemini.generateContent(
GeminiConfig.generativeModel,
"What's different between these pictures?",
images: [
imageFile1,
imageFile2,
],
);
copied to clipboard
Chat #
Using Gemini, you can build freeform conversations across multiple turns. The SDK simplifies the process by managing the state of the conversation, so unlike with generateContent, you don't have to store the conversation history yourself.
To build a multi-turn conversation (like chat), use the gemini-pro model, and initialize the chat by calling startChat() and send a new user message, which will also append the message and the response to the chat history.
There are two possible options for role associated with the content in a conversation:
user : the role which provides the prompts.
model : the role which provides the responses.
In the example below, Gemini will answer our question "How many paws are in my house?" without any problems. Because we sent the past conversation to Gemini using startChat() method, so Gemini knows how many dogs there are in our house.
String? content = await _gemini.startChat(
GeminiConfig.generativeModel,
"How many paws are in my house?",
history: [
ChatMessage(Role.user, ["Hello, I have 2 dogs in my house."]),
ChatMessage(Role.model, ["Great to meet you. What would you like to know?"]),
],
);
copied to clipboard
If we had used generateContent() method, Gemini would give an answer like this since it does not know the number of dogs in our house: "I do not have access to information about your house or the number of paws in it."
Stream #
By default, the model returns a response after completing the entire generation process. You can achieve faster interactions by not waiting for the entire result, and instead use streaming to handle partial results.
_gemini.generateContentStream(
GeminiConfig.generativeModel,
_inputController.text.trim(),
).listen((content) {
print(content);
});
copied to clipboard
_gemini.startChatStream(
GeminiConfig.generativeModel,
"How many paws are in my house?",
history: [
ChatMessage(Role.user, ["Hello, I have 2 dogs in my house."]),
ChatMessage(Role.model, ["Great to meet you. What would you like to know?"]),
],
).listen((content) {
print(content);
});
copied to clipboard
License #
Gemini AI Flutter Plugin is licensed under the BSD-3-Clause License.
My personal website: cihanurtekin.com
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.