This sample demonstrates how to use the Gemini model to generate text stream interactively.

## Code sample

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Node.js API
reference documentation](https://docs.cloud.google.com/nodejs/docs/reference/aiplatform/latest).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    const {VertexAI} = require('https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/overview.html');

    /**
     * TODO(developer): Update these variables before running the sample.
     */
    async function createStreamChat(
      projectId = 'PROJECT_ID',
      location = 'us-central1',
      model = 'gemini-2.0-flash-001'
    ) {
      // Initialize Vertex with your Cloud project and location
      const vertexAI = new https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/vertexai.html({project: projectId, location: location});

      // Instantiate the model
      const generativeModel = vertexAI.https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/vertexai.html({
        model: model,
      });

      const chat = generativeModel.startChat({});
      const chatInput1 = 'How can I learn more about that?';

      console.log(`User: ${chatInput1}`);

      const result1 = await chat.sendMessageStream(chatInput1);
      for await (const item of result1.https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/streamgeneratecontentresult.html) {
        console.log(item.https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentresponse.html[0].https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/generatecontentcandidate.html.https://docs.cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/content.html[0].text);
      }
    }

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=generativeaionvertexai).