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 サンプル ブラウザをご覧ください。