Building a Chatbot with Gemini AI Step by Step Guide

Posted By :Mohd Ubaish |26th February 2024
Building a Chatbot with Gemini AI Step by Step Guide
Prerequisites

 

Before we start, you'll need the following:

1. Basic understanding of JavaScript and Node.js.

2. Node.js installed on your machine.

3. Understand RESTful APIs.

4. Access to Gemini AI API credentials.

 

Step 1: Setting Up Your Node.js Project

 

To start, build a new Node.js project.Navigate to your project directory in your terminal and run the following command:

 

mkdir gemini-chatbot
cd gemini-chatbot
npm init -y

 

Step 2: Installing Dependencies
Install the necessary dependencies for your project. In addition to Axios, you'll need the @openai/gemini package for interacting with Gemini AI:

 

npm install axios @openai/gemini express dotenv

 

Step 3: Environment Configuration
Create a .env file in your project directory and add your Gemini API credentials:

 

GEMINI_API_KEY=YOUR_GEMINI_API_KEY

 

Replace YOUR_GEMINI_API_KEY with your actual Gemini API key.

 

Step 4: Writing the Chatbot Logic
Create a new file named geminiChatbot.js in your project directory and add the following code:

 

const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);

const GeminiChatbot = async (req,res) => {
     try {

        let { message, geminiChatHistory, conversationId } = req.body;


        // const history = chatHistory;
        const history = JSON.parse(geminiChatHistory);
        history.pop();
        const model = genAI.getGenerativeModel({ model: "gemini-pro" });
        const chat = model.startChat({
            history: history,
            settings: { temperature: 0 }
        });
        const format = TEXT_REPONSE_FORMAT;
        const result = await chat.sendMessage(message + format);
        const response = await result.response;
        const text = response.text();
        const data = SimplifyGeminiData(text);

       

        return res.status(200).json({
            success: true,
            data,
            conversationId
        })

    } catch (error) {
        console.log(error);
        return res.status(500).json({ success: false, message: error.message });
    }
};

 

 

Step 6: Testing Your Chatbot
You can now test your chatbot by sending messages to your application and observing the responses generated by Gemini AI. Make sure your Gemini API credentials are valid and that your application can communicate with the Gemini API endpoint.

 

Conclusion
Congratulations! You've successfully integrated Gemini AI into your Node.js application to create a chatbot. Happy Coding!


About Author

Mohd Ubaish

Mohd Ubaish is a highly skilled Backend Developer with expertise in a wide range of technologies, including React, Node.js, Express.js, MongoDB, and JavaScript.With a deep understanding of both front-end and back-end development, he is currently dedicated to the development of TripCongo Web Discovery.He is committed to creating a user-friendly experience on the frontend while ensuring the seamless functionality of the backend. By staying up-to-date with the latest industry trends and advancements, he strives to provide innovative solutions and optimize the performance of the application.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us