Vertex AI পরিষেবা আপনাকে Apps Script-এ Vertex AI API ব্যবহার করতে দেয়। এই API আপনাকে টেক্সট জেনারেশন, ইমেজ জেনারেশন এবং আরও অনেক কিছুর জন্য Gemini এবং অন্যান্য জেনারেটিভ AI মডেলগুলিতে অ্যাক্সেস দেয়।
এই উন্নত পরিষেবাটি শুরু করতে, কুইকস্টার্ট ব্যবহার করে দেখুন।
পূর্বশর্ত
বিলিং সক্ষম করা একটি গুগল ক্লাউড প্রকল্প। বিদ্যমান কোনও প্রকল্পে বিলিং সক্ষম করা আছে কিনা তা পরীক্ষা করতে, আপনার প্রকল্পগুলির বিলিং স্থিতি যাচাই করুন দেখুন। একটি প্রকল্প তৈরি করতে এবং বিলিং সেট আপ করতে, একটি গুগল ক্লাউড প্রকল্প তৈরি করুন দেখুন।
গুগল ক্লাউড কনসোলে, আপনার ক্লাউড প্রজেক্টে যান এবং Vertex AI API সক্ষম করুন:
আপনার অ্যাপস স্ক্রিপ্ট প্রজেক্টে, Vertex AI পরিষেবা চালু করুন। ধাপগুলির জন্য, উন্নত Google পরিষেবাগুলি দেখুন।
তথ্যসূত্র
এই পরিষেবা সম্পর্কে আরও তথ্যের জন্য, Vertex AI API রেফারেন্স ডকুমেন্টেশন দেখুন। Apps Script-এর সমস্ত উন্নত পরিষেবার মতো, Vertex AI পরিষেবাটি পাবলিক API-এর মতো একই বস্তু, পদ্ধতি এবং পরামিতি ব্যবহার করে।
নমুনা কোড
নিম্নলিখিত নমুনা কোডটি Vertex AI API এর সংস্করণ 1 ব্যবহার করে।
টেক্সট তৈরি করুন
এই নমুনা কোডটি দেখায় কিভাবে জেমিনি 2.5 ফ্ল্যাশ মডেলকে টেক্সট তৈরি করতে বলা হয়। ফাংশনটি আউটপুটটি অ্যাপস স্ক্রিপ্টের এক্সিকিউশন লগে ফেরত পাঠায়।
/**
* 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 প্রতিস্থাপন করুন।
একটি পরিষেবা অ্যাকাউন্ট ব্যবহার করে টেক্সট তৈরি করুন
নিম্নলিখিত উদাহরণটি দেখায় কিভাবে একটি পরিষেবা অ্যাকাউন্ট ব্যবহার করে একটি অ্যাপস স্ক্রিপ্ট প্রকল্প হিসাবে প্রমাণীকরণ করে পাঠ্য তৈরি করা যায়।
/**
* 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);
}