0

I am currently working on a project where I need to create an AI chatbot using Google’s GEMINI. I have chosen Node.js as my development environment.

While I have some experience with Node.js, this is my first time working with Google GEMINI and I am not sure where to start. I have looked through the GEMINI documentation but I am still unclear about how to integrate it with Node.js to create a chatbot.

Could anyone provide a step-by-step guide or point me to some resources that could help me get started with creating a chatbot using Google GEMINI and Node.js? Any code snippets, tutorials, or advice would be greatly appreciated.

1 Answer 1

0

Chat is also called multi-turn conversation, because the user and the ai take turns responding to messages.

There is an example already which shows how to store the chat history and hold on a conversation with the AI.

Build multi-turn conversations (chat)

const { GoogleGenerativeAI } = require("@google/generative-ai");

// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);

async function run() {
  // For text-only input, use the gemini-pro model
  const model = genAI.getGenerativeModel({ model: "gemini-pro"});

  const chat = model.startChat({
    history: [
      {
        role: "user",
        parts: [{ text: "Hello, I have 2 dogs in my house." }],
      },
      {
        role: "model",
        parts: [{ text: "Great to meet you. What would you like to know?" }],
      },
    ],
    generationConfig: {
      maxOutputTokens: 100,
    },
  });

  const msg = "How many paws are in my house?";

  const result = await chat.sendMessage(msg);
  const response = await result.response;
  const text = response.text();
  console.log(text);
}

run();
Sign up to request clarification or add additional context in comments.

2 Comments

You just copy/pasted the code from another page. You cannot put that into an html file. Please elaborate.
Its node.js not client side javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.