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