Membuat percakapan non-streaming multi-giliran dengan Vertex AI

Contoh yang menunjukkan cara menyiapkan Vertex AI SDK dan membuat percakapan non-streaming dengan tiga pesan berurutan yang dikirim ke model.

Contoh kode

Node.js

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai Vertex AI menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi API Node.js Vertex AI.

Untuk melakukan autentikasi ke Vertex AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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);
}

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat Google Cloud browser contoh.