[PAID] Gemini Extension to interact with the Gemini-pro model from Google

Gemini

The Gemini extension for AI2 allows you to interact with the Google Gemini-Pro and Gemini-Pro-Vesion models and these models that Bard is based on to generate text and control a stream of text generation.

Features of the Gemini extension for AI2 :

  • Generate text using the Google Gemini API.

  • Stream generated text in real time.

  • Generate text with images.

  • Encode images to Base64 with the -w0 option.

  • Handle errors gracefully.

Benefits of using the Gemini extension:

  • Easy to use: The Gemini extension provides a simple and easy-to-use interface for interacting with the Gemini API.

  • Powerful: The Gemini API is a powerful tool for generating text, and the Gemini extension makes it easy to use this API in your AI2 apps.

  • Versatile: The Gemini extension can be used for a variety of tasks, such as generating customer support responses, writing creative content, and translating languages.

  • Extensible: The Gemini extension is extensible, and new features can be added in the future.

Here are some specific examples of how the Gemini extension can be used:

  • Create a chatbot that can respond to user queries in a natural and engaging way.

  • Generate marketing copy for your products or services.

  • Write blog posts and articles on a variety of topics.

  • Translate languages in real time.

  • Create interactive stories and games.

The possibilities are endless!

Blocks


Explanation

Generating Content

To generate content using Gemini, you can use the GenerateGeminiContent block. This block takes two arguments:

  • modelName (String): The name of the Gemini model to use (e.g., "gemini-1.5-flash") check this docs.

  • apiKey (String): Your Google API key.

  • contents: A list of dictionaries, where each dictionary represents a content item. Each content item can have ,
    So the JSON input for contents will be like this

[
        {"role":"user",
        "parts":[{
          "text": "Write the first line of a story about a magic backpack."}]},
        {"role": "model",
         "parts":[{
           "text": "In the bustling city of Meadow brook, lived a young girl named Sophie. She was a bright and curious soul with an imaginative mind."}]},
        {"role": "user",
         "parts":[{
           "text": "Can you set it in a quiet village in 1600s France?"}]},
      ]

Blocks example:
old Block: for explanation

the following keys:
* role: A string representing the role of the content item in the conversation.
* parts: A list of dictionaries, where each dictionary represents a part of the content item. Each part can have the following keys:
* text: A string representing the text of the part.

The GenerateGeminiContent block will generate content using the specified parameters and return the result in the RespondedToGemini event.

The RespondedToGemini event will be triggered with the following parameters:

  • apiResponse: The raw API response from Gemini.
  • textParts: A list of strings representing the generated text parts.
  • role: The role of the generated content.
  • finishReason: The reason why the generation was finished.
  • index: The index of the generated content.
  • safetyRatings: A list of dictionaries representing the safety ratings of the generated content. Each dictionary will have the following keys:
    • category: The category of the safety rating.
    • probability: The probability of the safety rating.

Streaming Content

To generate content in a stream, you can use the StreamGenerateGeminiContent block. This block takes two arguments:

  • modelName (String): The name of the Gemini model to use (e.g., "gemini-1.5-flash") check this docs.

  • apiKey (String): Your Google API key.

  • contents: A list of dictionaries, where each dictionary represents a content item. Each content item can have the following keys:

    • role: A string representing the role of the content item in the conversation.
    • parts: A list of dictionaries, where each dictionary represents a part of the content item. Each part can have the following keys:
      • text: A string representing the text of the part. so the JSON input for contents will be like this
[
        {"role":"user",
         "parts":[{
           "text": "Write the first line of a story about a magic backpack."}]},
        {"role": "model",
         "parts":[{
           "text": "In the bustling city of Meadow brook, lived a young girl named Sophie. She was a bright and curious soul with an imaginative mind."}]},
        {"role": "user",
         "parts":[{
           "text": "Can you set it in a quiet village in 1600s France?"}]},
      ]

Blocks examble
old Block: for explanation

  • api key: Your Google Cloud API key.

The StreamGenerateGeminiContent block will open a stream of content generation using the specified parameters. The generated content will be returned in the GotGeminiStream event.

Screenshot 2023-12-16 235605

The GotGeminiStream event will be triggered with the following parameter:

  • text: A string representing the generated text.

Screenshot 2023-12-16 235605

You can manually stop the stream using the StopStream block. The StoppedStream event will be triggered when the stream is stopped.

Screenshot 2023-12-16 235605

You can also check if the stream is currently running using the IsStreaming block.



Generating Content with Images

To generate content using images, you can use the StreamGenerateGeminiVisionContent block. This block takes two arguments:

  • contents: A list of dictionaries, where each dictionary represents a content item. Each content item can have the following keys:
    • role: A string representing the role of the content item in the conversation.
    • parts: A list of dictionaries, where each dictionary represents a part of the content item. Each part can have the following keys:
      • text: A string representing the text of the part.
      • inlineData: A dictionary representing inline data, such as an image. The inlineData dictionary can have the following keys:
        • mimeType: The MIME type of the inline data.
        • data: The base64-encoded data of the inline data.
          So the JSON input for contents will be like this
[
       {
         "text": "Describe what the people are doing in this image:\n"
       },
       {
         "inlineData": {
           "mimeType": "image/png",
           "data": "'$(base64 -w0 image0.jpeg)'"
         }
       },
       {
         "text": " "
       }
     ]
   }
 ]

Blocks example :
old Block: for explanation

  • api key: Your Google Cloud API key.

Screenshot 2023-12-16 235605

The StreamGenerateGeminiVisionContent block will open a stream of content generation using the specified parameters. The generated content will be returned in the GotGeminiStream event.



StreamGenerateGeminiFileContentFromBase64
This function sends a streaming request to the Google Gemini API to generate content based on the provided files and text.

Parameters:

  • apiKey (String): Your Google API key.

  • modelName (String): The name of the Gemini model to use (e.g., "gemini-1.5-flash") check this docs.

  • fileBase64List (YailList): A list of strings containing the Base64 encoded data of the files.

  • mimeTypeList (YailList): A list of strings containing the MIME types of the files in fileBase64List. The order of MIME types must correspond to the order of files.

  • additionalText (String): Any additional text to include in the request.




GetGeminiModelNames

This function retrieves a list of available Gemini model names from the Google Gemini API.

Parameters:

  • apiKey (String): Your Google API key.

Events:

  • GotGeminiModelNames(List modelNames): This event is triggered when the API request is successful and the list of model names is retrieved. The modelNames parameter contains the list of model names as strings.



7

  • ErrorOccurred(String message, String component): This event is triggered if an error occurs during the API request.



Encoding Images to Base64

The EncodeImageToBase64 block can be used to encode an image file to Base64 with the -w0 option, which removes all line breaks from the encoded string. This can be useful for sending images to the Gemini API.

The EncodeImageToBase64 block takes one argument:

  • imagePath: The path to the image file.

The EncodeImageToBase64 block will return the base64-encoded image as a string.


Error Handling

Screenshot 2023-12-16 235605

The ErrorOccurred event will be triggered if an error occurs while using the Gemini extension. The event will be triggered with the following parameters:

  • message: A string describing the error.
  • component: The name of the component that caused the error.

Examples

Here is an example of how to use the Gemini extension to generate text:

contents = [{"role": "user", "parts": [{"text": "Hello, Gemini!"}]}]
api_key = "YOUR_API_KEY"
GenerateGeminiContent(contents, api_key)

Bocks:



Here is an example of how to use the Gemini extension to generate text in a stream:

contents = [{"role": "user", "parts": [{"text": "Hello, Gemini!"}]}]
api_key = "YOUR_API_KEY"
StreamGenerateGeminiContent(contents, api_key)

Bocks:

Here is an example of how to use the Gemini extension to generate text with images:


contents = [
  {
    "role": "user",
    "parts": [
      {"text": "Here is an image of a cat:"},
      {"inlineData": {"mimeType": "image/jpeg", "data": base64_image}}
    ]
  }
]

api_key = "YOUR_API_KEY"

Bocks:

Here is an example of how to use the Gemini extension to generate text with images in FreeForm Prompt:

you can use this extension to convert the TextBox component to FreeForm layout :


contents = [
             {
      "parts": [
        {
          "text": "Describe what the people are doing in this image:\n"
        },
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": "'$(base64 -w0 image0.jpeg)'"
          }
        },
        {
          "text": "\nand what is the relation between this is mage to \n"
        },
        {
          "inlineData": {
            "mimeType": "image/webp",
            "data": "'$(base64 -w0 image1.webp)'"
          }
        }
      ]
    }
  ]

api_key = "YOUR_API_KEY"

Bocks:

Freeform preview example:

Screenshot 2023-12-18 004254

PaLM_2 blocks palm


blocks(3)

component_event(1)

component_event

PaLM 2
PaLM 2 is a large language model (LLM) developed by Google that can perform various tasks involving natural language understanding and generation, such as reasoning, coding, mathematics, and multilingual translation. It is an improved version of PaLM, which was released in 2022. PaLM 2 is based on three main innovations: compute-optimal scaling, improved dataset mixture, and updated model architecture and objective. PaLM 2 is also used in other generative AI tools, such as the PaLM API and Bard


Applications that use this extension :

videos preview:

Aix_file:

Price: $5.99

PayPal payment URL: Purchase Gemini.aix , After payment you will be directed to the download URL so you do not have to contact me to get the extension file however you can contact me in case of any problems

Have Inquiries?

For any queries regarding the Gemini extension, feel free to reach out at PM

Note :

*You can try Gemini and get your API key from here

1 Like

PaLM 2 NewBlocks added

1 Like

Hi, I just paid for gemini.aix but it sent me chatGPT.aix

2 Likes

@Long_Cao I have sent you the file of Gemini. aix and I thank you for reporting this issue
and for ChatGpt.aix it's yours cause this is my fault not yours

1 Like

I just made payment via "Gemini.aix".

I paid for Gemini.aix today, but I was not directed to the website to download the file.

Taifun

I sent a private message and also tried via chat, but I still couldn't speak to "Cavaleiro_Negro".

I think "Black_Knight" is the extension author, not the one you said.
Oh wait you traslated the author's name, you obviously won't be able to send a message to them! Send it to "Black_Knight" (and do not translate the name please)...

Sorry for this error, the author of the extension is "Black_Knight". Thanks!

1 Like

You're welcome.

I would like help in contacting "Black_Knight". I already sent a private message, tried via chat, but I was unsuccessful. I purchased the "Gemini.aix" extension, but after payment I was not directed to download the file.

@Black_Knight also gets messages if you post something here in this thread
just be patient

Taifun

Thank you very much for your help, I will wait for the answer

Firstly I am very sorry for my very late reply and for this bad situation I was very busy with my military missions at the last of those days and you

You can Contact me again here and I will solve your problem

Send me the URL to download the extension and everything will be resolved.

Thank you very much for sending me the file. If I have any questions about the application, I will post them here.

1 Like

You are welcome man !

Ok you can ask if you want any thing about it

New update for the Extension to meet the latest updates of the Gemini API .

Here's a summary of the updates made to the Gemini.aix compared to the initial version.

1. Model Selection:

  • The GenerateGeminiContent, StreamGenerateGeminiContent, and functions now all accept a modelName parameter, allowing the user to specify which Gemini model to use for the request. This provides flexibility in choosing the appropriate model for different tasks.

2. StreamGenerateGeminiFileContentFromBase64 Function:

  • New Function: A new function called StreamGenerateGeminiFileContentFromBase64 has been added.



  • Base64 File Input: This function accepts a list of Base64 encoded files (fileBase64List) and a corresponding list of MIME types (mimeTypeList).

  • Generic File Handling: It handles various file types (not just images) by using the MIME type information.

  • Streaming Response: It uses streaming to receive the response from the Gemini API and triggers the GotGeminiStream event for each chunk of text received.

3. GetGeminiModelNames Function:

  • New Function: A new function called GetGeminiModelNames has been added.




  • Retrieving Model Names: It retrieves a list of available Gemini model names from the API and triggers the GotGeminiModelNames event with the list.

4. GetFilePathFromDataURI Function:

  • New Function: A new function called GetFilePathFromDataURI has been added.

  • Data URI to File Path: It converts a Data URI (representing a file) to a local file path. It handles content://, file://, and data:// URI schemes.

5. getMimeType Function:

  • New Function: A new function called getMimeType has been added.

  • Get MIME Type: It takes a file path as input and returns the MIME type of the file using Files.probeContentType(path).

6. Code Cleanup and Improvements:

  • Removed Redundant Parameter: The contents parameter in the StreamGenerateGeminiVisionContentFromPathsAndText function was removed as it became unnecessary after adding separate parameters for images and text.

  • Error Handling: The code now includes more robust error handling, using try-catch blocks and triggering the ErrorOccurred event when necessary.

Overall, the updated code is more versatile, efficient, and user-friendly:

  • More Features: It provides functions to retrieve model names, handle various file types, and work with Data URIs.

  • Flexibility: Users can now choose specific Gemini models and send different file types to the API.

  • Efficiency: Streaming responses allow for better handling of large data.

  • Improved Usability: The code is more organized and includes better documentation and error handling.

These updates enhance the functionality and make the extension more useful for a wider range of applications within App Inventor.

If any one interested of this competition AI event from google