Vertex AI hizmeti

Vertex AI hizmeti, Apps Script'te Vertex AI API'yi kullanmanıza olanak tanır. Bu API ile metin oluşturma, görüntü oluşturma ve daha fazlası için Gemini'a ve diğer üretken yapay zeka modellerine erişebilirsiniz.

Ön koşullar

Referans

Bu hizmet hakkında daha fazla bilgi için Vertex AI API referans belgelerine bakın. Apps Komut Dosyası'ndaki tüm gelişmiş hizmetler gibi Vertex AI hizmeti de herkese açık API ile aynı nesneleri, yöntemleri ve parametreleri kullanır.

Örnek kod

Aşağıdaki örnek kodda Vertex AI API'nin 1. sürümü kullanılmaktadır.

Metin oluşturun

Bu örnek kodda, Gemini 2.5 Flash modeline metin oluşturma isteminin nasıl gönderileceği gösterilmektedir. İşlev, çıktıyı Apps Komut Dosyası'nın yürütme günlüğüne döndürür.

/**
 * 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 kısmını Cloud projenizin proje kimliği ile değiştirin.