خدمة Vertex AI

تتيح لك خدمة Vertex AI استخدام Vertex AI API في Apps Script. تتيح لك واجهة برمجة التطبيقات هذه استخدام Gemini ونماذج أخرى من الذكاء الاصطناعي التوليدي لإنشاء النصوص والصور وغير ذلك.

المتطلبات الأساسية

مراجع

لمزيد من المعلومات حول هذه الخدمة، يُرجى الاطّلاع على المستندات المرجعية الخاصة بواجهة Vertex AI API. مثل جميع الخدمات المتقدّمة في Apps Script، تستخدم خدمة Vertex AI العناصر والطرق والمعلَمات نفسها التي تستخدمها واجهة برمجة التطبيقات العامة.

نموذج التعليمات البرمجية

يستخدِم نموذج الرمز البرمجي التالي الإصدار 1 من واجهة برمجة التطبيقات Vertex AI API.

إنشاء نص

يوضّح نموذج الرمز هذا كيفية الطلب من نموذج Gemini 2.5 Flash إنشاء نص. تعرض الدالة الناتج في سجلّ التنفيذ في "برمجة تطبيقات Google".

/**
 * 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 بـ معرّف المشروع في مشروعك على السحابة الإلكترونية.