יצירת שיחה לא רציפה עם Vertex AI

דוגמה שממחישה איך מגדירים את Vertex AI SDK ויוצרים שיחה לא רציפה עם שלוש הודעות רצופות שנשלחות למודל.

דוגמת קוד

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי Node.jsהוראות ההגדרה במאמר Vertex AI quickstart using client libraries. מידע נוסף מופיע במאמרי העזרה של Vertex AI Node.js API.

כדי לבצע אימות ב-Vertex AI, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

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-2.0-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 .