שירות Vertex AI מאפשר לכם להשתמש ב-Vertex AI API ב-Apps Script. ממשק ה-API הזה מאפשר לכם לגשת ל-Gemini ולמודלים אחרים של AI גנרטיבי כדי ליצור טקסט, ליצור תמונות ועוד.
כדי להתחיל להשתמש בשירות המתקדם הזה, אפשר לנסות את המדריך למתחילים.
דרישות מוקדמות
פרויקט ב-Google Cloud שהחיוב בו מופעל. כדי לבדוק אם החיוב מופעל בפרויקט קיים, אפשר לעיין במאמר אימות סטטוס החיוב של הפרויקטים. כדי ליצור פרויקט ולהגדיר חיוב, אפשר לעיין במאמר יצירת פרויקט ב-Google Cloud.
במסוף Google Cloud, עוברים לפרויקט Cloud ומפעילים את Vertex AI API:
בפרויקט Apps Script, מפעילים את שירות Vertex AI. הוראות מפורטות מופיעות במאמר בנושא שירותים מתקדמים של Google.
חומרי עזר
מידע נוסף על השירות הזה זמין במאמרי העזרה של Vertex AI API. בדומה לכל השירותים המתקדמים ב-Apps Script, שירות Vertex AI משתמש באותם אובייקטים, שיטות ופרמטרים כמו ה-API הציבורי.
קוד לדוגמה
בדוגמת הקוד הבאה נעשה שימוש בגרסה 1 של Vertex AI API.
יצירת טקסט
בדוגמת הקוד הזו מוצג איך להנחות את מודל Gemini 2.5 Flash ליצור טקסט. הפונקציה מחזירה את הפלט אל יומן הביצוע של Apps Script.
/**
* Main entry point to test the Vertex AI integration.
*/
function main() {
const prompt = 'What is Apps Script in one sentence?';
try {
const response = callVertexAI(prompt);
console.log(`Response: ${response}`);
} catch (error) {
console.error(`Failed to call Vertex AI: ${error.message}`);
}
}
/**
* Calls the Vertex AI Gemini model.
*
* @param {string} prompt - The user's input prompt.
* @return {string} The text generated by the model.
*/
function callVertexAI(prompt) {
// Configuration
const projectId = 'GOOGLE_CLOUD_PROJECT_ID';
const region = 'us-central1';
const modelName = 'gemini-2.5-flash';
const model = `projects/${projectId}/locations/${region}/publishers/google/models/${modelName}`;
const payload = {
contents: [{
role: 'user',
parts: [{
text: prompt
}]
}],
generationConfig: {
temperature: 0.1,
maxOutputTokens: 2048
}
};
// Execute the request using the Vertex AI Advanced Service
const response = VertexAI.Endpoints.generateContent(payload, model);
// Use optional chaining for safe property access
return response?.candidates?.[0]?.content?.parts?.[0]?.text || 'No response generated.';
}
מחליפים את GOOGLE_CLOUD_PROJECT_ID במזהה הפרויקט של פרויקט Cloud.