บริการ Vertex AI ช่วยให้คุณใช้ Vertex AI API ใน Apps Script ได้ API นี้ให้คุณเข้าถึง Gemini และโมเดล Generative AI อื่นๆ สำหรับการสร้างข้อความ การสร้างรูปภาพ และอื่นๆ
หากต้องการเริ่มต้นใช้งานบริการขั้นสูงนี้ ให้ลองใช้คู่มือเริ่มใช้งาน
ข้อกำหนดเบื้องต้น
โปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงิน หากต้องการตรวจสอบว่าโปรเจ็กต์ที่มีอยู่เปิดใช้การเรียกเก็บเงินแล้วหรือไม่ ให้ดูที่ยืนยันสถานะการเรียกเก็บเงินของโปรเจ็กต์ หากต้องการสร้างโปรเจ็กต์และตั้งค่าการเรียกเก็บเงิน โปรดดูสร้างโปรเจ็กต์ Google Cloud
ใน Google Cloud Console ให้ไปที่โปรเจ็กต์ Cloud แล้วเปิดใช้ Vertex AI API โดยทำดังนี้
เปิดใช้บริการ Vertex AI ในโปรเจ็กต์ Apps Script โปรดดูขั้นตอนต่างๆ ที่ บริการขั้นสูงของ Google
ข้อมูลอ้างอิง
ดูข้อมูลเพิ่มเติมเกี่ยวกับบริการนี้ได้ที่เอกสารอ้างอิง 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
สร้างข้อความโดยใช้บัญชีบริการ
ตัวอย่างต่อไปนี้แสดงวิธีสร้างข้อความโดยตรวจสอบสิทธิ์เป็นโปรเจ็กต์ 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);
}