1. ก่อนเริ่มต้น
ใน Codelab นี้ คุณจะได้เรียนรู้วิธีทำให้งานของ Google Workspace เป็นแบบอัตโนมัติด้วยฟีเจอร์การเรียกใช้ฟังก์ชันและโหมดหลายรูปแบบของ Gemini API
ข้อกำหนดเบื้องต้น
- มีความคุ้นเคยกับ Apps Script, JavaScript หรือภาษาโปรแกรมที่คล้ายกัน
สิ่งที่ได้เรียนรู้
- วิธีใช้ประโยชน์จากการเรียกใช้ฟังก์ชันและฟีเจอร์หลายรูปแบบของ Gemini API
- วิธีเชื่อมโยงการเรียก Gemini API หลายๆ ครั้งเข้าด้วยกัน
- วิธีใช้ Gemini API เพื่อทำให้งานของ Google Workspace เป็นแบบอัตโนมัติ
สิ่งที่ต้องมี
- เว็บเบราว์เซอร์
- บัญชี Gmail อีกวิธีหนึ่งคือบัญชี Google Workspace ที่ใช้การตั้งค่าเฉพาะของ Gemini API
- การเชื่อมต่อจากภูมิภาคที่รองรับสำหรับ Gemini API
- ไม่บังคับ: อินเทอร์เฟซบรรทัดคำสั่งพร้อมโปรแกรม
curl
เพื่อทดสอบคำขอ API โดยตรง
โค้ดที่สมบูรณ์สำหรับ Codelab นี้มีอยู่ใน Gemini API Cookbook บน GitHub ตรวจสอบดูว่าคุณต้องการใส่โค้ดที่กรอกแล้วหรือยัง
2. ตั้งค่า Gemini API
เกี่ยวกับ Gemini
โมเดล Gemini คือกลุ่มโมเดล AI ที่ใหญ่ที่สุดและมากความสามารถที่สุดของ Google คุณสามารถใช้ Gemini API เพื่อใช้ประโยชน์จากโมเดลเหล่านี้ในแอป คุณยังลองใช้ Gemini API ได้ใน Google AI Studio ซึ่งเป็นเว็บอินเทอร์เฟซของ API ที่คุณสามารถลองใช้พรอมต์ ปรับแต่งการตั้งค่าโมเดล และปรับแต่งโมเดลที่กำหนดเองได้โดยไม่ต้องเขียนโค้ดใดๆ
ซื้อคีย์
- หากต้องการใช้ Gemini API ให้สร้างคีย์ API ใน Google AI Studio
ไม่บังคับ: ทดสอบคีย์
หากคุณมีสิทธิ์เข้าถึงบรรทัดคำสั่งที่มี curl ให้เพิ่มคีย์ในบรรทัดแรกของบล็อกต่อไปนี้ แล้วเรียกใช้ในเทอร์มินัลเพื่อทดสอบคีย์ API
export GOOGLE_API_KEY=Paste_your_API_key_here
curl "https://generativelanguage.googleapis.com/v1beta/models?key=${GOOGLE_API_KEY}"
คุณควรดูรายการโมเดลในรูปแบบ JSON เช่น model/gemini-1.0-pro ซึ่งหมายความว่าใช้งานได้แล้ว
3. ไม่บังคับ: ส่งคำขอไปยัง Gemini API
ในขั้นตอนที่ไม่บังคับนี้ คุณจะได้ส่งคำขอไปยัง Gemini API เพื่อให้เข้าใจวิธีการสร้างเนื้อหาได้ดียิ่งขึ้นก่อนที่จะเพิ่มลงในแอป Apps Script
เกี่ยวกับโมเดล
Gemini API มีโมเดลจำนวนมากที่มีความสามารถและข้อจำกัดแตกต่างกัน โมเดลแต่ละรายการจะระบุพร้อมความสามารถไว้ในหน้าโมเดล Gemini
การส่งคำขอแรกของคุณ
หากต้องการให้ Gemini API สร้างพรอมต์ข้อความให้เสร็จสมบูรณ์ คุณต้องสร้างคำขอ JSON และส่งไปยังปลายทาง REST API
โดยทำตามขั้นตอนต่อไปนี้
- ในไฟล์ใหม่ ให้ป้อนคำขอ JSON ต่อไปนี้
{
contents: [
{
parts: [
{ text: 'The most important aspects of a persuasive presentation are:' }
]
}
]
}
คำขอ JSON มีพรอมต์ต่อไปนี้: The most important aspects of a persuasive presentation are:
โมเดลจะดำเนินการตามคำสั่งนี้จนจบและให้ผลลัพธ์โดยตรง
คำขอ JSON มีฟิลด์ระดับบนสุด 3 ฟิลด์ที่จะป้อนข้อมูล ได้แก่ contents
, generationConfig
และ safetySettings
ต้องใช้ contents
เท่านั้น เครื่องมืออื่นๆ จะมีกลไกสำหรับควบคุมเอาต์พุต
- บันทึก JSON นี้ในไฟล์
presentation.txt
แล้วส่งไปยังcurl
โดยตรงดังตัวอย่างต่อไปนี้
curl -H 'Content-Type: application/json' -X POST -d @presentation.txt \
'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro-latest:generateContent?key='${GOOGLE_API_KEY}
ในตัวอย่างนี้คุณตั้งค่าต่อไปนี้ใน URL
v1beta
ระบุเวอร์ชันของ APIgemini-1.0-pro-latest
ระบุว่า Gemini 1.0 Pro เป็นโมเดลและใช้สแนปชอตล่าสุดgenerateContent
จะระบุเมธอด API ที่คุณเรียกใช้
คุณควรเห็นผลลัพธ์ที่คล้ายกับ:
{
"candidates": [
{
"content": {
"parts": [
{
"text": "* **Credibility:** The audience must trust that you are an expert on the subject matter and that you have their best interests at heart.\n* **Clearness:** Your message must be easy to understand and follow. Avoid using jargon or technical terms that your audience may not be familiar with.\n* **Concreteness:** Use specific examples and data to support your arguments. Avoid making vague or general claims.\n* **Emotional appeal:** In addition to appealing to the audience's logical side, you should also try to connect with them on an emotional level. Use storytelling, humor, and personal anecdotes to make your points more memorable and engaging.\n* **Strong closing:** End your presentation with a strong call to action. Tell the audience what you want them to do and why it is important for them to do it."
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0,
"safetyRatings": [...]
}
],
"promptFeedback": {
"safetyRatings": [...]
}
}
ต่อไปนี้เป็นเอาต์พุตของเทอร์มินัลที่มีการจัดรูปแบบเป็นประจำเพื่อให้อ่านได้ง่าย
- ความน่าเชื่อถือ: ผู้ชมต้องเชื่อมั่นว่าคุณเป็นผู้เชี่ยวชาญในเรื่องนั้นๆ และคุณใส่ใจผลประโยชน์ที่ดีที่สุดของพวกเขา
- ความชัดเจน: ข้อความของคุณต้องเข้าใจง่ายและทำตามได้ง่าย หลีกเลี่ยงการใช้ศัพท์เฉพาะหรือศัพท์เทคนิคที่ผู้ชมอาจไม่คุ้นเคยกับ
- ความเป็นรูปธรรม: ใช้ตัวอย่างและข้อมูลที่เจาะจงเพื่อสนับสนุนอาร์กิวเมนต์ของคุณ หลีกเลี่ยงการอ้างสิทธิ์ที่คลุมเครือหรือการกล่าวอ้างทั่วไป
- การดึงดูดทางอารมณ์: นอกจากการดึงดูดด้านตรรกะของผู้ชมแล้ว คุณยังควรพยายามสานสัมพันธ์กับผู้ชมในระดับอารมณ์ด้วย ใช้การเล่าเรื่อง อารมณ์ขัน และเกร็ดเล็กเกร็ดน้อยส่วนตัวเพื่อทำให้ประเด็นของคุณน่าจดจำและน่าสนใจยิ่งขึ้น
- การปิดอย่างชัดเจน: จบการนำเสนอด้วยคำกระตุ้นการตัดสินใจที่หนักแน่น บอกผู้ชมถึงสิ่งที่คุณต้องการให้พวกเขาทำ และบอกเหตุผลว่าทำไมพวกเขาจึงจำเป็นต้องทำเช่นนั้น
ดูข้อมูลเพิ่มเติมเกี่ยวกับการตั้งค่าอื่นๆ รวมถึง generationConfig
และ safetySettings
ได้ที่คู่มือข้อความแจ้งและความปลอดภัย
4. เรียกใช้ Gemini API จาก Apps Script
- ไปที่ script.new แล้วระบบจะสร้างไฟล์ Apps Script ของ
code.gs
ให้คุณโดยอัตโนมัติ - วางเคอร์เซอร์ไว้เหนือไฟล์
code.gs
แล้วคลิก > เปลี่ยนชื่อ - เปลี่ยนชื่อไฟล์เป็น
utils.gs
- ในไฟล์ ให้นำฟังก์ชัน
myFunction
ออกเพื่อที่ไฟล์ว่างเปล่า
เพิ่มคีย์ API ลงในโปรเจ็กต์
- เลือกการตั้งค่าโปรเจ็กต์ในเมนูการนำทาง
- ในส่วนคุณสมบัติของสคริปต์ ให้คลิก เพิ่มพร็อพเพอร์ตี้ของสคริปต์
- ใต้พร็อพเพอร์ตี้ ให้ป้อน
GOOGLE_API_KEY
- ในส่วนค่า ให้ป้อนคีย์ API จาก Google AI Studio
- คลิกบันทึกพร็อพเพอร์ตี้ของสคริปต์
- กลับไปที่เครื่องมือแก้ไข
เพิ่มโค้ด Gemini API
ในไฟล์ utils.gs
ให้ทำตามขั้นตอนต่อไปนี้
ตั้งค่าคีย์ API และปลายทาง ดังนี้
const properties = PropertiesService.getScriptProperties().getProperties();
const geminiApiKey = properties['GOOGLE_API_KEY'];
const geminiEndpoint = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro-latest:generateContent?key=${geminiApiKey}`;
- เพิ่มฟังก์ชันต่อไปนี้ที่เรียกใช้ Gemini API ด้วยพรอมต์ที่เฉพาะเจาะจง
function callGemini(prompt, temperature=0) {
const payload = {
"contents": [
{
"parts": [
{
"text": prompt
},
]
}
],
"generationConfig": {
"temperature": temperature,
},
};
const options = {
'method' : 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(geminiEndpoint, options);
const data = JSON.parse(response);
const content = data["candidates"][0]["content"]["parts"][0]["text"];
return content;
}
- เพิ่มฟังก์ชันต่อไปนี้ที่ตั้งค่าข้อความแจ้ง
function testGemini() {
const prompt = "The best thing since sliced bread is";
const output = callGemini(prompt);
console.log(prompt, output);
}
ทดสอบ
- คลิก บันทึก
- เลือก
testGemini
ในรายการฟังก์ชันแบบเลื่อนลง แล้วคลิก - ยอมรับสิทธิ์ที่จำเป็น โค้ดควรทำงานและคุณควรเห็นผลลัพธ์บางรายการของคอนโซลพร้อมผลลัพธ์ในบันทึกการดำเนินการ
ใช้งานได้แล้ว!
5. เรียกใช้ Gemini API ด้วยรูปภาพ
หนึ่งในฟีเจอร์ที่มีประสิทธิภาพมากที่สุดของตระกูล Gemini โมเดลคือการรองรับการป้อนข้อมูลแบบหลายโมดัล ซึ่งหมายความว่าคุณสามารถให้ข้อมูลได้มากกว่าแค่ข้อความ ในส่วนนี้ คุณจะเพิ่มฟังก์ชันที่เรียกใช้ Gemini API ด้วยรูปภาพ
- เพิ่มบรรทัดต่อไปนี้ที่ด้านบนของไฟล์
utils.gs
หลังการประกาศconst geminiEndpoint
ที่มีอยู่
const geminiProVisionEndpoint = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro-vision-latest:generateContent?key=${geminiApiKey}`;
เพิ่มโค้ด Gemini Vision
- เพิ่มฟังก์ชันลงในไฟล์
utils.gs
เพื่อเรียกใช้ปลายทางที่เพิ่มใหม่ต่อไปนี้
function callGeminiProVision(prompt, image, temperature=0) {
const imageData = Utilities.base64Encode(image.getAs('image/png').getBytes());
const payload = {
"contents": [
{
"parts": [
{
"text": prompt
},
{
"inlineData": {
"mimeType": "image/png",
"data": imageData
}
}
]
}
],
"generationConfig": {
"temperature": temperature,
},
};
const options = {
'method' : 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(geminiProVisionEndpoint, options);
const data = JSON.parse(response);
const content = data["candidates"][0]["content"]["parts"][0]["text"];
return content;
}
- เพิ่มฟังก์ชันทดสอบต่อไปนี้
function testGeminiVision() {
const prompt = "Provide a fun fact about this object.";
const image = UrlFetchApp.fetch('https://storage.googleapis.com/generativeai-downloads/images/instrument.jpg').getBlob();
const output = callGeminiProVision(prompt, image);
console.log(prompt, output);
}
ฟังก์ชันนี้จะโหลดรูปภาพทดสอบจากอินเทอร์เน็ตและส่งไปยังฟังก์ชันที่คุณกำหนด หลังจากนั้น ก็ต่อสายไฟเพื่อใช้แผนภูมิจากสเปรดชีต ดังนั้นนี่จึงเป็นเพียงการทดสอบเท่านั้น
ทดสอบ
- บันทึกและเรียกใช้ฟังก์ชัน
testGeminiVision
จากนั้นตรวจสอบผลลัพธ์
6. เรียกใช้ Gemini API ด้วยเครื่องมือ
นอกจากข้อความและรูปภาพแล้ว คุณยังให้สิทธิ์เข้าถึงเครื่องมือในพรอมต์ได้ด้วย
เพิ่มโค้ดการจัดการเครื่องมือ
- เพิ่มฟังก์ชันลงในไฟล์
utils.gs
ที่ยอมรับข้อกำหนดของเครื่องมือ ดังนี้
function callGeminiWithTools(prompt, tools, temperature=0) {
const payload = {
"contents": [
{
"parts": [
{
"text": prompt
},
]
}
],
"tools" : tools,
"generationConfig": {
"temperature": temperature,
},
};
const options = {
'method' : 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(geminiEndpoint, options);
const data = JSON.parse(response);
const content = data["candidates"][0]["content"]["parts"][0]["functionCall"];
return content;
}
ดูข้อมูลเพิ่มเติมเกี่ยวกับสคีมานี้และช่องที่ใช้ได้ที่ข้อมูลอ้างอิง API ของ FunctionDeclaration
ทดสอบ
- กำหนดเครื่องมือที่โมเดลสามารถใช้เพื่อค้นหาวันที่และเวลาปัจจุบัน:
function testGeminiTools() {
const prompt = "Tell me how many days there are left in this month.";
const tools = {
"function_declarations": [
{
"name": "datetime",
"description": "Returns the current date and time as a formatted string.",
"parameters": {
"type": "string"
}
}
]
};
const output = callGeminiWithTools(prompt, tools);
console.log(prompt, output);
}
รูปแบบที่ใช้ที่นี่คือสคีมา FunctionDeclaration
คุณไม่ได้เรียกใช้ฟังก์ชันวันที่และเวลา คุณจะได้รับเพียงตัวบ่งชี้ว่าโมเดลขอการเรียกใช้ฟังก์ชัน คุณจะจัดการการเรียกใช้ฟังก์ชันในขั้นตอนถัดไป
- บันทึกและเรียกใช้ฟังก์ชัน
testGeminiTools
เพื่อดูเอาต์พุต
7. ภาพรวมของการผสานรวมการสาธิตกับ Google Workspace
เมื่อเข้าใจวิธีการทำงานของการเรียกใช้ฟังก์ชันแล้ว คุณก็ขยายความสามารถของโมเดล Gemini ไปยังบริการอื่นๆ ได้อย่างง่ายดาย ในอีกไม่กี่ส่วนถัดไป คุณจะได้สร้างการผสานรวมกับผลิตภัณฑ์ของ Google Workspace เช่น Google ไดรฟ์, Google สไลด์ และ Google ชีต นี่คือแผนภาพอย่างง่าย
เมื่อมีคำค้นหาของผู้ใช้เข้ามาในภาพรวม คุณจะใช้การเรียกฟังก์ชันของ Gemini API ในการพิจารณาว่าจะใช้เครื่องมือใด คุณสร้างเครื่องมือ 3 อย่างที่สามารถทำสิ่งต่อไปนี้
- จัดการประชุม ฟังก์ชัน
setupMeeting()
ในแผนภาพจะเรียกใช้ Gemini 1.0 Pro API เพื่อสรุปบล็อกใน Google ไดรฟ์ และเพิ่มสรุปลงในการประชุมที่สร้างขึ้นใหม่ใน Google ปฏิทิน - ร่างอีเมลตามข้อมูลเชิงลึกจากแผนภูมิ ฟังก์ชัน
draftEmail()
ในแผนภาพจะเรียกใช้ Gemini 1.0 Pro Vision เพื่อวิเคราะห์แผนภูมิใน Google ชีตและเขียนอีเมลใน Gmail โดยอิงจากการวิเคราะห์ดังกล่าว - สร้างสไลด์โครงกระดูก ฟังก์ชัน
createDeck()
ในแผนภาพจะเรียกใช้ Gemini 1.0 Pro ให้ระดมความคิดเกี่ยวกับหัวข้อย่อยสำหรับชุดสไลด์ใน Google สไลด์
สำหรับแต่ละเครื่องมือ คุณจะต้องทำ 3 อย่างต่อไปนี้
- กำหนดว่าการตอบกลับการเรียกใช้ฟังก์ชันของ Gemini API จะขอเรียกใช้เครื่องมือดังกล่าวในบล็อก
if...else
หรือไม่ - โปรดเพิ่มฟังก์ชันจริงเพื่อใช้งานฟังก์ชันของเครื่องมือ
- ประกาศเครื่องมือด้วย Gemini API เพื่อให้โมเดล Gemini ทราบถึงการมีอยู่ของเครื่องมือดังกล่าวและแสดงการตอบกลับการเรียกใช้ฟังก์ชันที่ถูกต้องได้
8. จัดการประชุมด้วย Apps Script
ขั้นตอนแรก ตั้งค่าการประชุมใน Google ปฏิทินโดยอัตโนมัติ แต่คุณจะเพิ่มคำอธิบายซึ่งเป็นข้อมูลสรุปของไฟล์ใน Google ไดรฟ์ด้วย
โดยทำตามขั้นตอนต่อไปนี้
- ดาวน์โหลดไฟล์ข้อความนี้ ซึ่งเป็นสำเนาข้อความของบล็อกการเปิดตัว Gemini 1.5 Pro
- อัปโหลดไฟล์ไปยังโฟลเดอร์รากใน Google ไดรฟ์
- ในเครื่องมือแก้ไข ให้สร้างไฟล์
main.gs
แล้วเพิ่มโค้ดต่อไปนี้
function main() {
const userQuery = "Set up a meeting at 10AM tomorrow with Helen to discuss the news in the Gemini-blog.txt file.";
var tool_use = callGeminiWithTools(userQuery, WORKSPACE_TOOLS);
Logger.log(tool_use);
if(tool_use['name'] == "setupMeeting") {
setupMeeting(tool_use['args']['time'], tool_use['args']['recipient'], tool_use['args']['filename']);
Logger.log("Your meeting has been set up.");
}
else
Logger.log("no proper tool found");
}
ในหน้านี้ คุณจะเรียกใช้ความสามารถในการเรียกใช้ฟังก์ชันของ Gemini API ถัดไป คุณต้องกำหนดฟังก์ชันของเครื่องมือ
- ทางด้านซ้ายของตัวแก้ไข ให้คลิก + เพิ่มบริการ > ข้างบริการ > API ของ Google ปฏิทิน > เพิ่ม การดำเนินการนี้จะเปิดใช้บริการ Google ปฏิทินขั้นสูง ซึ่งคุณจำเป็นต้องใช้สำหรับ API ขั้นสูงบางรายการในภายหลัง
- เพิ่มโค้ดต่อไปนี้ในไฟล์
utils.gs
function attachFileToMeeting(event, file, fileName) {
// Get the iCal ID for the event.
const iCalEventId = event.getId();
// Log the ID and title for debugging.
console.log(`iCal event ID: ${iCalEventId}`);
console.log(`event Title: ${event.getTitle()}`);
// Set up the options for listing the event with the advanced Google Calendar service.
const options = {
iCalUID: iCalEventId,
};
// Use the primary calendar as the calendar ID to list events.
const calendarId = 'primary';
// Use the advanced Google Calendar service to list the event.
const calEvents = Calendar.Events.list(calendarId, options);
// Get the Calendar ID used by the advanced Google Calendar service.
const eventId = calEvents.items[0].id;
// Get the file URL for the attachment.
const fileUrl = file.getUrl();
// Set up the patch options to add the file.
var patch = {
attachments: [{
'fileUrl': fileUrl,
'title': fileName
}]
};
// Patch the event to add the file as an attachment.
Calendar.Events.patch(patch, 'primary', eventId, {"supportsAttachments": true});
}
function setupMeeting(time, recipient, filename) {
const files = DriveApp.getFilesByName(filename);
const file = files.next();
const blogContent = file.getAs("text/*").getDataAsString();
var geminiOutput = callGemini("Give me a really short title of this blog and a summary with less than three sentences. Please return the result as a JSON with two fields: title and summary. \n" + blogContent);
// The Gemini model likes to enclose the JSON with ```json and ```
geminiOutput = JSON.parse(geminiOutput.replace(/```(?:json|)/g, ""));
const title = geminiOutput['title'];
const fileSummary = geminiOutput['summary'];
const event = CalendarApp.getDefaultCalendar().createEventFromDescription(`meet ${recipient} at ${time} to discuss "${title}"`);
event.setDescription(fileSummary);
attachFileToMeeting(event, file, filename);
}
โค้ดนี้จะทำหน้าที่ดังต่อไปนี้
- ฟังก์ชัน
setupMeeting()
จะทำงานใน Google ไดรฟ์ของคุณและค้นหาไฟล์Gemini-blog.txt
ชื่อไฟล์นี้จะส่งคืนโดยอัตโนมัติจากการเรียกใช้ฟังก์ชันของ Gemini API ในขั้นตอนที่ 3 - ฟังก์ชัน
setupMeeting()
จะเรียกใช้ Gemini API เพื่อสรุปเนื้อหาของไฟล์ จัดการประชุมด้วย CalendarApp โดยใช้คำอธิบายรูปแบบอิสระ และเพิ่มข้อมูลสรุปลงในการประชุม - ฟังก์ชัน
setupMeeting()
จะเรียกใช้ฟังก์ชันattachFileToMeeting()
เพื่อใช้บริการ Google ปฏิทินขั้นสูงเพื่อแนบไฟล์บล็อกกับการประชุม
- เพิ่มโค้ดต่อไปนี้ที่ด้านบนของไฟล์
utils.gs
const WORKSPACE_TOOLS = {
"function_declarations": [
{
"name": "setupMeeting",
"description": "Sets up a meeting in Google Calendar.",
"parameters": {
"type": "object",
"properties": {
"time": {
"type": "string",
"description": "The time of the meeting."
},
"recipient": {
"type": "string",
"description": "The name of the recipient."
},
"filename": {
"type": "string",
"description": "The name of the file."
},
},
"required": [
"time",
"recipient",
"filename"
]
}
},
// You add tools here.
]
};
- ในโปรแกรมตัดต่อ ให้กลับไปที่ไฟล์
main.gs
แล้วคลิก - หาก Google Workspace ขอสิทธิ์ในการเรียกใช้สคริปต์ ให้คลิกตกลง
ในอีกไม่กี่วินาที บันทึกการดำเนินการจะแสดงข้อความแจ้งว่าตั้งค่าการประชุมแล้ว
- ใน Google ปฏิทิน ให้มองหาการประชุมที่มีข้อมูลสรุปและไฟล์แนบ
9. ร่างอีเมลด้วย Apps Script
ขั้นตอนต่อไป ให้คุณร่างอีเมลใน Gmail โดยอัตโนมัติ ต่อไปนี้คือสถานการณ์สมมติ: สมมติว่าคุณดำเนินการวิเคราะห์ข้อมูลใน Google ชีต คุณใส่ตัวเลขทั้งหมดให้พร้อมและสร้างแผนภูมิ คุณต้องการใช้ Gemini Pro Vision API เพื่อร่างอีเมลตามแผนภูมิ
โดยทำตามขั้นตอนต่อไปนี้
- เปิดชีตนี้แล้วคลิกไฟล์ -> ทำสำเนา
- ในช่องข้อความชื่อในกล่องโต้ตอบคัดลอกเอกสาร ให้แทนที่ชื่อเริ่มต้น
Copy of CollegeExpenses
ด้วยCollegeExpenses
- ในไฟล์
main.gs
ให้แทนที่การค้นหาก่อนหน้าของผู้ใช้ด้วยรายการใหม่ แล้วเพิ่มโค้ดต่อไปนี้ลงในคำสั่งif...else
function main() {
// const userQuery = "Set up a meeting at 5PM with Helen to discuss the news in the Gemini-1.5-blog.txt file.";
const userQuery = "Draft an email for Mary with insights from the chart in the CollegeExpenses sheet.";
if(...) {...}
// Add this code
else if(tool_use['name'] == "draftEmail") {
draftEmail(tool_use['args']['sheet_name'], tool_use['args']['recipient']);
Logger.log("Check your Gmail to review the draft");
}
else {...}
}
- เพิ่มโค้ดต่อไปนี้ในไฟล์
utils.gs
function draftEmail(sheet_name, recipient) {
const prompt = `Compose the email body for ${recipient} with your insights for this chart. Use information in this chart only and do not do historical comparisons. Be concise.`;
var files = DriveApp.getFilesByName(sheet_name);
var sheet = SpreadsheetApp.openById(files.next().getId()).getSheetByName("Sheet1");
var expenseChart = sheet.getCharts()[0];
var chartFile = DriveApp.createFile(expenseChart.getBlob().setName("ExpenseChart.png"));
var emailBody = callGeminiProVision(prompt, expenseChart);
GmailApp.createDraft(recipient+"@demo-email-provider.com", "College expenses", emailBody, {
attachments: [chartFile.getAs(MimeType.PNG)],
name: 'myname'
});
}
ฟังก์ชันนี้จะดึงแผนภูมิค่าใช้จ่ายวิทยาลัยจากชีตแล้วส่งให้ Gemini Pro Vision เพื่อร่างอีเมล Gemini Pro Vision จะดึงข้อมูลจากแผนภูมิและร่างเนื้อหาของอีเมลในนามของคุณ
- ในไฟล์
utils.gs
ให้เพิ่มโค้ดต่อไปนี้ลงในออบเจ็กต์WORKSPACE_TOOLS
หลังความคิดเห็นYou add tools here
WORKSPACE_TOOLS = {
"function_declarations": [
// You add tools here.
{
"name": "draftEmail",
"description": "Write an email by analyzing data or charts in a Google Sheets file.",
"parameters": {
"type": "object",
"properties": {
"sheet_name": {
"type": "string",
"description": "The name of the sheet to analyze."
},
"recipient": {
"type": "string",
"description": "The name of the recipient."
},
},
"required": [
"sheet_name",
"recipient"
]
}
},
]
};
- ในเครื่องมือแก้ไข ให้กลับไปที่ไฟล์
main.gs
แล้วคลิก - เปิด Gmail หลังจากผ่านไป 10-20 วินาที คุณควรจะเห็นอีเมลฉบับร่างดังนี้
คุณสามารถแก้ไขอีเมลฉบับร่างก่อนส่ง อีเมลนี้เขียนขึ้นโดย Gemini Pro Vision ทั้งหมดหลังจากที่คุณส่งพรอมต์สั้นๆ และเขียนแผนภูมิมาให้
10. สร้างโครงกระดูกด้วย Apps Script
ถัดไป ให้สร้างสไลด์โครงกระดูกใน Google สไลด์โดยอัตโนมัติด้วย Apps Script
โดยทำตามขั้นตอนต่อไปนี้
- ในไฟล์
main.gs
ให้แทนที่การค้นหาก่อนหน้าของผู้ใช้ด้วยรายการใหม่และเพิ่มโค้ดต่อไปนี้ลงในคำสั่งif...else
function main() {
// const userQuery = "Draft an email for Mary with insights from the chart in the CollegeExpenses sheet.";
const userQuery = "Help me put together a deck about water conservation.";
if(...) {...}
// Add this code
else if(tool_use['name'] == 'createDeck') {
deckURL = createDeck(tool_use['args']['topic']);
Logger.log("Deck URL: " + deckURL);
}
else {...}
}
- เพิ่มโค้ดต่อไปนี้ในไฟล์
utils.gs
function createDeck(topic) {
const prompt = `I'm preparing a ${NUM_SLIDES}-slide deck to discuss ${topic}. Please help me brainstorm and generate main bullet points for each slide. Keep the title of each slide short. Please produce the result as a valid JSON so that I can pass it to other APIs.`;
var geminiOutput = callGemini(prompt, 0.4);
// The Gemini model likes to enclose the JSON with ```json and ```
geminiOutput = geminiOutput.replace(/```(?:json|)/g, "");
const bulletPoints = JSON.parse(geminiOutput);
// Create a Google Slides presentation.
const presentation = SlidesApp.create("My New Presentation");
// Set up the opening slide.
var slide = presentation.getSlides()[0];
var shapes = slide.getShapes();
shapes[0].getText().setText(topic);
var body;
for (var i = 0; i < NUM_SLIDES; i++) {
slide = presentation.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
shapes = slide.getShapes();
// Set title.
shapes[0].getText().setText(bulletPoints['slides'][i]['title']);
// Set body.
body = "";
for (var j = 0; j < bulletPoints['slides'][i]['bullets'].length; j++) {
body += '* ' + bulletPoints['slides'][i]['bullets'][j] + '\n';
}
shapes[1].getText().setText(body);
}
return presentation.getUrl();
}
ฟังก์ชันนี้เรียกใช้ Gemini API เพื่อระดมความคิดเกี่ยวกับหัวข้อหนึ่งๆ แสดงผลหัวข้อย่อยในรูปแบบ
JSON แล้วใช้สคริปต์ Apps เพื่อเติมสไลด์โครงกระดูก
- ในไฟล์
utils.gs
ให้เพิ่มโค้ดต่อไปนี้ลงในออบเจ็กต์WORKSPACE_TOOLS
หลังความคิดเห็นYou add tools here
WORKSPACE_TOOLS = {
"function_declarations": [
// You add tools here.
{
"name": "createDeck",
"description": "Build a simple presentation deck with Google Slides and return the URL.",
"parameters": {
"type": "object",
"properties": {
"topic": {
"type": "string",
"description": "The topic that the presentation is about."
},
},
"required": [
"topic"
]
}
},
]
};
- ที่ด้านบนของไฟล์
utils.gs
ให้กำหนดค่าคงที่ต่อไปนี้
const NUM_SLIDES = 3;
นี่เป็นจำนวนสไลด์ที่โมเดล Gemini สร้างขึ้นเพิ่มเติมจากสไลด์เริ่มต้น
- ในเครื่องมือแก้ไข ให้กลับไปที่ไฟล์
main.gs
แล้วคลิก ในอีกไม่กี่วินาที คุณจะเห็น URL งานนำเสนอในบันทึกการดำเนินการ - ใช้เบราว์เซอร์เพื่อเปิด URL คุณจะเห็นโครงกระดูกเต็มไปด้วยหัวข้อย่อย
11. แนวคิดสำหรับการสำรวจ
นอกจากการผสานรวมทั้ง 3 แบบแล้ว คุณยังสำรวจแนวคิดต่อไปนี้ได้
- สร้างแชทบ็อตใน Google Chat หนึ่งในกรณีการใช้งานยอดนิยมสําหรับ Large Language Models (LLM) คือการสร้างแชทบ็อต Gemini API ช่วยให้คุณสร้างแชทบ็อตสำหรับ Google Chat ได้อย่างง่ายดาย ดูข้อมูลเพิ่มเติมได้ที่ Google Chat API และ Codelab ในการสร้างแอปสำหรับ Google Chat ด้วย Gemini
- การดึงข้อมูล Augmented Generation (RAG) ด้วยข้อมูลของคุณเองใน Google ไดรฟ์หรือ Keep ใน Codelab นี้ คุณจะใช้ไฟล์ข้อความเพียงไฟล์เดียวสำหรับการสรุป อย่างไรก็ตาม คุณยังสามารถใช้เนื้อหาจาก Google ไดรฟ์และ Keep ส่วนตัว เช่น โน้ต, PDF และรูปภาพ ร่วมกับ Gemini API, ฐานข้อมูลเวกเตอร์ หรือใช้เครื่องมือการจัดการเป็นกลุ่ม (ไม่บังคับ) อย่างเช่น LangChain เพื่อทำ RAG และปรับเปลี่ยนคำตอบของโมเดลให้เหมาะกับคุณตามข้อมูลของคุณได้
- ใช้ความสามารถในการเรียกใช้ฟังก์ชันหลายรอบสำหรับ Gemini API การเรียกฟังก์ชันของ Gemini API ไม่จำกัดเพียงเลี้ยวเดียว และคุณสามารถเรียกใช้ฟังก์ชันหลายรอบสำหรับงานที่ซับซ้อนขึ้นได้
- ทำได้มากกว่า Google Workspace เมื่อเข้าใจวิธีผสานรวม Gemini API กับ Google Workspace แล้ว คุณก็สามารถใช้ประโยชน์จาก Google Workspace และใช้ประโยชน์จาก API อื่นๆ ทั่วโลกได้
12. ขอแสดงความยินดี
คุณได้เรียนรู้เกี่ยวกับความสามารถแบบหลายโมดัลและการเรียกใช้ฟังก์ชันของ Gemini API ไปแล้ว คุณใช้ Apps Script เพื่อทำงานบางอย่างของ Google Workspace แบบอัตโนมัติได้ด้วย Apps Script