Skip to Content

Getting started with Gemini API and Web Apps: What is the Correct Way to Start Multi-Turn Chat with Google AI JavaScript SDK?

Learn the proper syntax to initiate a multi-turn chat conversation using the Google AI JavaScript SDK. Find out which method and parameters to use for seamless chat integration.

Table of Contents

Question

Which of these is the correct way to start a multi-turn chat conversation using the Google AI Javascript SDK?

A. model.startChat({history: …})
B. model.generateContent(history)
C. chat.sendMessage(…)
D. model.beginChat()

Answer

A. model.startChat({history: …})

Explanation

To break this down:

  • The chat is initiated using the startChat method on the model object
  • startChat takes an options object as a parameter
  • The options object must contain a history property
  • The history property is used to pass in any prior conversation history to provide context

So a basic example would look like:

const model = new Model({...});

const history = [
{user: "Hello!"},
{assistant: "Hi there! How can I assist you today?"},
{user: "I have a question about..."}
];

model.startChat({history});

The other options mentioned are incorrect:

  • generateContent is used for single-shot text generation, not multi-turn chat
  • There is no sendMessage method on the chat object
  • beginChat is not a valid method on the model object

So in summary, to properly start a multi-turn conversation, use model.startChat and pass the conversation history in the history option. This will allow the model to understand the context of the conversation when generating responses.

Getting started with the Gemini API and Web Apps certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Getting started with the Gemini API and Web Apps quiz and earn Getting started with the Gemini API and Web Apps certification.