Chatbot with Gemini

Firebase

Dec 18, 2023

Introduction

Powered by Gemini, Google’s state-of-art Large Language Model (LLM), this extension allows to easily deploy a chatbot using Gemini’s extensive language models, with messages stored and managed in Cloud Firestore.

The Chatbot with Gemini extension provides an out-of-the-box solution for communicating with and engaging in conversations with Google AI.

Google AI

Google DeepMind has released its newest Large Language Model (LLM) called Gemini. This solution includes a new range of features for allowing developers to converse and interact with AI easily.

Gemini offers a range of features and capabilities including:

A Multimodal approach

Gemini provides a multimodal approach, allowing it to understand and process various forms of information. This also allows users to combine different types of content such as text, code, audio, images, and video to generate AI responses.

Deliberate Reasoning

Gemini surpasses previous models in its ability to engage in deliberate reasoning. This means that the language model is not only capable of processing and understanding information but also of applying logical reasoning to solve problems and make decisions.

Human-quality Text Generation

Gemini generates human-quality text, which means that it can create realistic and engaging narratives, write different kinds of creative content, and even translate languages.

Model Sizes

This extension uses the Gemini Pro and Gemini Pro Vision models for handling requests.

Google AI API

This Firebase extension uses the Google AI API (for developers) and Vertex AI to communicate with the Gemini model.

Getting Started with the Firestore GenAI Chatbot extension

Before installing, ensure you have a Firebase project set up. If it’s your first time, here’s how to get started:

Once your project is prepared, you can integrate the Chatbot with Gemini extension into your application.

Extension Installation

Installing the “Chatbot with Gemini” extension can be done via the Firebase Console or using the Firebase CLI.

Option 1: Firebase Console

Find your way to the Firebase Extensions catalog, and locate the “Chatbot with Gemini” extension. Click “Install in Firebase Console” and complete the installation process by configuring your extension’s parameters.

Chatbot with Gemini install button

Option 2: Firebase CLI

For those who prefer using command-line tools, the Firebase CLI offers a straightforward installation command:

firebase ext:install googlecloud/firestore-genai-chatbot --project=projectId-or-alias

Configure Parameters

Configuring the “Chatbot with Gemini” extension involves several parameters to help you customize it. Here’s a breakdown of each parameter:

To quickly get started with this extension, there is one required fields which must be provided.

Required Configuration

Cloud Functions location
The location field should represent the closest location to where your project is located. In this example we are going to select Iowa (us-central1). See this guide for more information on cloud function locations.

Default Parameters

Once you finish filling in the required fields you can hit install. To give you more customization options, there are a number of optional fields which you can either configure or leave to default. These values include:

Gemini API Provider
Since Google AI requires an API key, you have the option to choose Vertex AI instead, which authenticates requests using application default credentials.

Generative model
This parameter allows to select the Gemini mode. It should be set to the model name, which is the second segment of the model code (for example, models/gemini-pro should be chosen as gemini-pro).

The list of supported models for Google AI can be found here.

For Vertex AI, only the Gemini family of models and PaLM models are currently supported and the list can be found here.

Collection Path
This field represents the location where the extension will listen to updates in your Firestore database.

In this example, we only need a single collection name, such as chat, but you can also point to a sub-collection using wildcards.

For more advanced configuration, you could specify users/{uid}/discussions/{discussionId}/messages as an example. This allows you to customize chats for each authenticated user individually.

Prompt Field
This Firestore document field will contain the query from the user when adding a document in Firestore.

Response Field
This Firestore document field will contain the response from Gemini when adding a document in Firestore.

Optional Parameters

API key (A required field for Google AI)
If you selected Google AI as the Gemini API provider, you will need to provide a Google AI API key. This can be obtained through Google AI Studio.

To generate a key:

  1. Select Get API key.
  2. Select Create API key in new project or Create API key in existing project.
  3. Select the newly generated key and choose Copy.

Navigate back to your extension configuration and enter the value before selecting Create Secret.

This will store your secret securely in the Google Secret Manager.

Order Field (optional)
This extension will automatically load previous documents as context, ensuring that the conversation continues as documents are added to the collection. Select createTime for this value.

Context (optional)
Contextual preamble for the generative AI model. A string giving context for the discussion.

Temperature (optional)
This parameter controls the randomness of the output, with values ranging from 0 to 1, inclusive. A value approaching 1 will yield more diverse responses, whereas a value nearing 0 tends to generate less unexpected responses from the model.

Candidate Count (optional)
The Gemini AI has the ability to return multiple responses, allowing users to select which is the most appropriate. In this instance we would like one response. Keep this value as 1

Enable per document overrides
When adding a Firestore document, it is possible to override configuration for the Gemini when making a request. To keep the default settings, ensure this value is set to No.

How it works

After installation and configuration, the extension will listen for any new documents that are added to the configured collection. To see it in action:

  1. Add a new document: By including a prompt field with text, the AI will be prompted for a response.
  2. Tracking status: While a response from the AI is generated, the extension will update the current Firestore document with a status field. This field will include PROCESSING, COMPLETED and ERRORED responses.
  3. Successful responses: When the AI has returned a successful response. A new field will be added containing a response.
  4. Maintaining context: Existing documents will contain a startTime field, this is generated by the extension. When you add a new document, the existing document will be sent along with the request as context. This allows the AI to generate a response, based on previous conversations.
  5. Regenerating a response:: Updating the status field to anything over than “COMPLETED” will regenerate a response.

Chatbot with Gemini Firebase extensions flow chart

Steps to connect to the Chatbot with Gemini Extension

Before you begin, ensure that the Firebase SDK is initialized and you can access Firestore.

const firebaseConfig = {
  // Your Firebase configuration keys
};

firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();

Next, we can simply add a new document to the configured collection to start the conversation.

/** Add a new Firestore document */
await addDoc(collection(db, 'collection-name'), {
  prompt: 'This is an example',
});

Listen for any changes to the collection to display the conversation.

/** Create a Firebase query */
const collectionRef = collection(db, 'collection-name');
const q = query(collectionRef, orderBy('status.startTime'));

/** Listen for any changes **/
onSnapshot(q, (snapshot) => {
  snapshot.docs.forEach((change) => {
    /** Get prompt and response */
    const { comment, output } = change.data();

    /** Update the UI status */
    console.log(output);

    /** Update the UI prompt */
    console.log(comment);

    /** Update the UI AI status */
    console.log(response);
  });
});

Conclusion

Using the Chatbot with Gemini extension, you can converse with Google Gemini API by simply creating a Firestore document. The generative AI model’s responses are highly configurable, allowing various customization options for developers.