使用 Vertex AI 创建多回合非流式对话

此示例演示了如何设置 Vertex AI SDK 并创建非流式对话,其中包含发送给模型的 3 条连续消息。

代码示例

Node.js

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Node.js 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Node.js API 参考文档。

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证。

const {GoogleGenAI} = require('@google/genai');
/**
 * TODO(developer): Update these variables before running the sample.
 */
async function createNonStreamingChat(
  projectId = 'PROJECT_ID',
  location = 'us-central1',
  model = 'gemini-2.5-flash'
) {
  // Initialize client with your Cloud project and location
  const client = new GoogleGenAI({
    vertexai: true,
    project: projectId,
    location: location,
  });

  const chat = client.chats.create({
    model: model,
  });

  const response1 = await chat.sendMessage({message: 'Hello'});
  console.log('Chat response 1: ', response1.text);

  const response2 = await chat.sendMessage({
    message: 'Can you tell me a scientific fun fact?',
  });
  console.log('Chat response 2: ', response2.text);

  const response3 = await chat.sendMessage({
    message: 'How can I learn more about that?',
  });
  console.log('Chat response 3: ', response3.text);
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器。