Vertex AI सेवा

Vertex AI सेवा की मदद से, Apps Script में Vertex AI API का इस्तेमाल किया जा सकता है. इस API की मदद से, Gemini और अन्य जनरेटिव एआई मॉडल को ऐक्सेस किया जा सकता है. इनकी मदद से, टेक्स्ट और इमेज जनरेट की जा सकती हैं. इसके अलावा, और भी कई काम किए जा सकते हैं.

इस बेहतर सेवा का इस्तेमाल शुरू करने के लिए, क्विकस्टार्ट गाइड पढ़ें.

ज़रूरी शर्तें

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानने के लिए, Vertex AI API के रेफ़रंस दस्तावेज़ देखें. Apps Script में उपलब्ध सभी ऐडवांस सेवाओं की तरह, Vertex AI सेवा भी सार्वजनिक एपीआई के ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल करती है.

नमूना कोड

नीचे दिए गए सैंपल कोड में, Vertex AI API के वर्शन 1 का इस्तेमाल किया गया है.

टेक्स्ट जनरेट करना

इस सैंपल कोड में, टेक्स्ट जनरेट करने के लिए 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 प्रोजेक्ट का प्रोजेक्ट आईडी डालें.

सेवा खाते का इस्तेमाल करके टेक्स्ट जनरेट करना

यहां दिए गए उदाहरण में, सेवा खाते का इस्तेमाल करके, 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) {
  const service = getServiceAccountService();

  // 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,
    {},
    // Authenticate with the service account token.
    { Authorization: `Bearer ${service.getAccessToken()}` },
  );

  // Use optional chaining for safe property access
  return response?.candidates?.[0]?.content?.parts?.[0]?.text || 'No response generated.';
}

/**
 * Get a new OAuth2 service for a given service account.
 */
function getServiceAccountService() {
  const serviceAccountKeyString = PropertiesService.getScriptProperties().getProperty('SERVICE_ACCOUNT_KEY');

  if (!serviceAccountKeyString) {
    throw new Error('SERVICE_ACCOUNT_KEY property is not set. Please follow the setup instructions.');
  }

  const serviceAccountKey = JSON.parse(serviceAccountKeyString);

  const CLIENT_EMAIL = serviceAccountKey.client_email;
  const PRIVATE_KEY = serviceAccountKey.private_key;
  const SCOPES = ['https://www.googleapis.com/auth/cloud-platform'];

  return OAuth2.createService('ServiceAccount')
      .setTokenUrl('https://oauth2.googleapis.com/token')
      .setPrivateKey(PRIVATE_KEY)
      .setIssuer(CLIENT_EMAIL)
      .setPropertyStore(PropertiesService.getScriptProperties())
      .setScope(SCOPES);
}