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

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

代码示例

Node.js

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

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

const {VertexAI} = require('@google-cloud/vertexai');

/**
 * TODO(developer): Update these variables before running the sample.
 */
async function createNonStreamingChat(
  projectId = 'PROJECT_ID',
  location = 'us-central1',
  model = 'gemini-1.5-flash-001'
) {
  // Initialize Vertex with your Cloud project and location
  const vertexAI = new VertexAI({project: projectId, location: location});

  // Instantiate the model
  const generativeModel = vertexAI.getGenerativeModel({
    model: model,
  });

  const chat = generativeModel.startChat({});

  const result1 = await chat.sendMessage('Hello');
  const response1 = await result1.response;
  console.log('Chat response 1: ', JSON.stringify(response1));

  const result2 = await chat.sendMessage(
    'Can you tell me a scientific fun fact?'
  );
  const response2 = await result2.response;
  console.log('Chat response 2: ', JSON.stringify(response2));

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

后续步骤

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