יצירת שיחה לא רציפה עם 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 {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 .