Your own customized chatbot with Google Gemini API

In this article, we are going to explore how to set up your own customized chatbot on a NodeJS application. 

Since the advent of Artificial Intelligence (AI), various companies have developed their own personalized chatbots to enhance communication with their users. These powerful tools have enabled businesses to significantly reduce costs associated with customer service and support. By providing immediate responses and handling many inquiries simultaneously, chatbots enhance the user experience. They create a more efficient and engaging interaction, allowing users to receive instant support and information. Additionally, chatbots operate in a controlled environment, ensuring consistent and accurate communication while minimizing human error. Moreover, the data collected through these interactions is invaluable, as it is used to train and refine the company’s AI engines. This continuous learning process helps improve the chatbot’s performance, making it increasingly effective in understanding and addressing user needs. Consequently, chatbots not only streamline operations but also contribute to the ongoing advancement of AI technologies within the company. 

Build your tech team faster 

Scale with senior nearshore experts in your time zone.

Google Gemini: A comprehensive introduction  

On December 13th, 2023, Google introduced Gemini to the world, a powerful, advanced, and versatile AI model that marks a significant leap in artificial intelligence capabilities. Since February 2024, the release of Gemini 1.5 Pro took things to the next level, enabling developers to create contexts with an unprecedented 1 million tokens. 

Gemini’s capabilities are remarkable, as it can understand and process information across various formats, including text, code, audio, image, and video. This multimodal approach makes it incredibly adaptable, allowing it to handle a wide range of tasks with ease. Whether it’s generating detailed text responses, interpreting code snippets, analyzing audio signals, recognizing images, or even processing videos, Gemini excels in all these areas, making it an invaluable tool for developers across diverse industries. 

Key features of Gemini include: 

  1. Multimodality:

Gemini can seamlessly switch between different types of data, making it exceptionally versatile and capable of performing a multitude of tasks. This feature allows for more integrated and cohesive AI solutions that can operate in various contexts without compromising performance. 

  1. Efficiency:

Designed for optimal performance, Gemini can run efficiently on a range of platforms, from powerful data centers to smaller, more constrained devices. This ensures that developers can deploy Gemini in various environments, from large-scale enterprise applications to mobile and edge devices. 

  1. Scalability:

Gemini is built to handle tasks of varying complexity, from simple queries to highly intricate problems. Its scalable nature means it can be employed for both routine tasks and demanding projects, making it a robust solution for diverse AI needs. 

  1. Safety:

Google has implemented robust safety measures to ensure that Gemini is used responsibly. These measures include rigorous testing, ethical guidelines, and continuous monitoring to prevent misuse and ensure that the AI operates within safe and ethical boundaries. 

Google Gemini API integration into a NodeJS app 

Now that you know a little bit about Google Gemini, let’s start by showing how to integrate this tool into a NodeJS application. 

Initial setup 

  1. Install the @google/generative-ai library.

Open your terminal on the root folder of your project and run: 

				
					Your own customized chatbot with Google Gemini API 
				
			
  1. Go to the Google AI Studio website to get your Gemini API key. 
  2. Initialize the model:
    				
    					const { GoogleGenerativeAI } = require("@google/generative-ai"); 
     
    const genAI = new GoogleGenerativeAI(process.env.API_KEY); 
     
    const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash"}); 
    				
    			
  3. Run a prompt: 
				
					const prompt = "Does this look store-bought or homemade?"; 
const image = { 
  inlineData: { 
    data: Buffer.from(fs.readFileSync("cookie.png")).toString("base64"), 
    mimeType: "image/png", 
  }, 
}; 
 
const result = await model.generateContent([prompt, image]); 
console.log(result.response.text()); 
				
			

Once everything is set up, and you’ve tested the connection, we can move on to personalizing the chatbot. 

Model configuration 

  1. Choose the model variant that suits your needs.

Gemini offers different models that are optimized for specific use cases. Here’s a brief overview of Gemini variants that are available: 

For additional information, you can check the documentation here.
  1. Configure text generation.

Every prompt you send to the model includes parameters that influence how the model generates responses. You can adjust these parameters using GenerationConfig. If you don’t customize the parameters, the model will use its default settings, which may differ depending on the specific model. You can get additional information about this here.

Let’s add this to our code:

				
					
const model = genAI.getGenerativeModel({
  model: "gemini-1.5-flash"
  generationConfig: { maxOutputTokens: 2000, temperature: 0.9 }
});

				
			

The temperature parameter controls the randomness of the output. Use higher values for more creative responses, and lower values for more deterministic responses. Values can range from [0.0, 2.0].

The maxOutputTokens parameter sets the maximum number of tokens to include in a candidate.

  1. Configure the safety settings.

The Gemini API offers safety settings that you can modify during the prototyping phase to assess whether your application needs more or less restrictive safety configurations. These settings can be adjusted across four filter categories to control the restriction or allowance of specific types of content. You can get more information on this here.

Let’s add this to our code:

				
					const model = genAI.getGenerativeModel({
  model: "gemini-1.5-flash"
  generationConfig: { maxOutputTokens: 2000, temperature: 0.9 },
  safetySettings: [
      {
        category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
    ],
});

				
			
  1. Configure the system instructions

The system instructions allow you to set the behavior of the model based on your own needs by giving it additional context. These instructions let the model know how to respond, you can set a persona (“you are a rocket scientist”) or tell it what kind of voice to use (“talk like a pirate”). You can get more information on this here.

Let’s add this to our code:

				
					const model = genAI.getGenerativeModel({
  model: "gemini-1.5-flash"
  generationConfig: { maxOutputTokens: 2000, temperature: 0.9 },
  safetySettings: [
      {
        category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
    ],
  systemInstruction: {
“You are a friendly and empathic AI assistant, here to answer questions concisely. Your name is Chatty”.
    }
});

				
			

This is the most important section to personalize your chatbot. You can get creative and let your model know exactly how you want it to behave when responding. You can also use the Gemini app to improve your system instructions and customize the model to fit your specific needs.

  1. Create a function to get a response based on user input.

Let’s add this to our code:

				
					const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const generateResponse = async (prompt) => {
  const model = genAI.getGenerativeModel({
    model: "gemini-1.5-flash"
    generationConfig: { maxOutputTokens: 2000, temperature: 0.9 },
    safetySettings: [
      {
        category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
      {
        category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
      },
    ],
    systemInstruction: {
“You are a friendly and empathic AI assistant, here to answer questions concisely. Your name is Chatty”.
    }
  });
  try {
    const result = await model.generateContent(prompt);
    const response = result.response;
    return response.text();
  } catch (error) {
    return error
  }
}
export { generateResponse }

				
			

Now you can just import the generateResponse function in the wanted route and start interacting with the model.

Build your
tech team
faster
Scale with senior nearshore experts in your time zone.

Tags

NEWSLETTER
Get tech insights
in your inbox

Related

Access Elite
Software Developers
from Argentina

Get in touch
for expert solutions


«Outsourcing is too risky
and unreliable»


«Outsourcing is too risky
and unreliable»


«Outsourcing is too risky
and unreliable»

Get tech insights in your inbox

Get exclusive news and updates.