Create a multi-turn non-streaming conversation with Vertex AI
Stay organized with collections
Save and categorize content based on your preferences.
Sample demonstrating how to set-up the Vertex AI SDK and create a non-streaming conversation with three consecutive messages sent to the model.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Create a multi-turn non-streaming conversation with Vertex AI\n\nSample demonstrating how to set-up the Vertex AI SDK and create a non-streaming conversation with three consecutive messages sent to the model.\n\nCode sample\n-----------\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Node.js API\nreference documentation](/nodejs/docs/reference/aiplatform/latest).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const {VertexAI} = require('https://cloud.google.com/nodejs/docs/reference/vertexai/latest/overview.html');\n\n /**\n * TODO(developer): Update these variables before running the sample.\n */\n async function createNonStreamingChat(\n projectId = 'PROJECT_ID',\n location = 'us-central1',\n model = 'gemini-2.0-flash-001'\n ) {\n // Initialize Vertex with your Cloud project and location\n const vertexAI = new https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/vertexai.html({project: projectId, location: location});\n\n // Instantiate the model\n const generativeModel = vertexAI.https://cloud.google.com/nodejs/docs/reference/vertexai/latest/vertexai/vertexai.html({\n model: model,\n });\n\n const chat = generativeModel.startChat({});\n\n const result1 = await chat.sendMessage('Hello');\n const response1 = await result1.response;\n console.log('Chat response 1: ', JSON.stringify(response1));\n\n const result2 = await chat.sendMessage(\n 'Can you tell me a scientific fun fact?'\n );\n const response2 = await result2.response;\n console.log('Chat response 2: ', JSON.stringify(response2));\n\n const result3 = await chat.sendMessage('How can I learn more about that?');\n const response3 = await result3.response;\n console.log('Chat response 3: ', JSON.stringify(response3));\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=aiplatform)."]]