Vertex AI 서비스

Vertex AI 서비스를 사용하면 Apps Script에서 Vertex AI API를 사용할 수 있습니다. 이 API를 사용하면 텍스트 생성, 이미지 생성 등을 위해 Gemini 및 기타 생성형 AI 모델에 액세스할 수 있습니다.

기본 요건

참조

이 서비스에 대한 자세한 내용은 Vertex AI API 참조 문서를 참고하세요. Apps Script의 모든 고급 서비스와 마찬가지로 Vertex AI 서비스는 공개 API와 동일한 객체, 메서드, 매개변수를 사용합니다.

샘플 코드

다음 샘플 코드는 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 프로젝트의 프로젝트 ID로 바꿉니다.