Gemini API ยังคงเติบโตอย่างต่อเนื่อง ในวันที่ 5 สิงหาคม 2024 ฉันได้อัปเดต GeminiWithFiles เป็น v2.xx เป็นส่วนใหญ่ ด้วยการอัปเดตครั้งใหญ่นี้ เวอร์ชันจะเปลี่ยนจาก v1 เป็น v2
หากคุณต้องการใช้ GeminiWithFiles v1.xx โปรดดูที่นี่
นี่คือไลบรารี Google Apps Script สำหรับ Gemini API พร้อมไฟล์
ไลบรารี Google Apps Script ใหม่ชื่อ GeminiWithFiles ช่วยให้การใช้ Gemini ซึ่งเป็นโมเดลภาษาขนาดใหญ่ง่ายขึ้น เพื่อประมวลผลข้อมูลที่ไม่มีโครงสร้าง เช่น รูปภาพและ PDF GeminiWithFiles สามารถอัปโหลดไฟล์ สร้างเนื้อหา และสร้างคำอธิบายจากหลายภาพได้ในคราวเดียว สิ่งนี้จะช่วยลดภาระงานลงอย่างมากและเพิ่มความเป็นไปได้ในการใช้ Gemini
เมื่อเร็วๆ นี้ Gemini ซึ่งเป็นโมเดลภาษาขนาดใหญ่จาก Google AI ได้นำความเป็นไปได้ใหม่ๆ มาสู่งานต่างๆ โดยเปิดใช้งานการใช้ข้อมูลที่ไม่มีโครงสร้างเป็นข้อมูลที่มีโครงสร้าง สิ่งนี้มีความสำคัญอย่างยิ่งเนื่องจากมีข้อมูลจำนวนมหาศาลอยู่ในรูปแบบที่ไม่มีโครงสร้าง เช่น เอกสารข้อความ รูปภาพ และวิดีโอ
Gemini 1.5 API ซึ่งเปิดตัวเมื่อเร็วๆ นี้ ได้ขยายขีดความสามารถเหล่านี้อย่างมาก สามารถสร้างเนื้อหาได้มากถึง 1 ล้านโทเค็น ซึ่งเพิ่มขึ้นอย่างมากเมื่อเทียบกับเวอร์ชันก่อนหน้า นอกจากนี้ Gemini 1.5 ยังสามารถประมวลผลไฟล์รูปภาพได้มากถึง 3,000 ไฟล์ ซึ่งเกินขีดจำกัด 16 รูปภาพของ Gemini 1.0 อย่างมากมาย อ้างอิง
แม้ว่า Gemini จะไม่สามารถทำงานกับรูปแบบ Google ไดรฟ์ เช่น เอกสาร ชีต และสไลด์ได้โดยตรง แต่ก็มีวิธีแก้ไขชั่วคราว ในระยะปัจจุบัน ข้อมูล PDF สามารถประมวลผลได้โดยตรงด้วย Gemini API เมื่อใช้สิ่งนี้ ไฟล์ Google Docs เหล่านั้นจะถูกแปลงเป็น PDF และใช้กับ Gemini API อ้างอิง
รายงานนี้จะแนะนำไลบรารี Google Apps Script ใหม่ที่เรียกว่า "GeminiWithFiles" ซึ่งช่วยให้กระบวนการนี้ง่ายขึ้น GeminiWithFiles ช่วยให้ผู้ใช้สามารถอัพโหลดไฟล์และสร้างเนื้อหาได้อย่างง่ายดายโดยใช้ความสามารถอันทรงพลังของ Gemini นอกจากนี้ยังช่วยให้สร้างคำอธิบายที่มีประสิทธิภาพจากรูปภาพหลายรูปด้วยการเรียก API เพียงครั้งเดียว ซึ่งช่วยลดภาระงานได้อย่างมากเมื่อเทียบกับการประมวลผลแต่ละรูปภาพแยกกันดังที่แสดงในรายงานก่อนหน้าของฉัน อ้างอิง
ด้วยการปรับปรุงประสิทธิภาพกระบวนการและขยายขีดความสามารถ GeminiWithFiles ถือเป็นคำมั่นสัญญาสำหรับกรณีการใช้งานที่หลากหลายในโดเมนที่แตกต่างกัน รายงานนี้ทำหน้าที่เป็นแนวทางเพิ่มเติมจากฉบับก่อนหน้า โดยมีเป้าหมายเพื่อลดต้นทุนกระบวนการและปรับปรุงประสิทธิภาพเมื่อทำงานกับ Gemini และข้อมูลที่ไม่มีโครงสร้าง
ฉันสร้างไลบรารีนี้ตามรายงานต่อไปนี้
ไลบรารี GeminiWithFiles นี้ช่วยให้คุณสามารถโต้ตอบกับ Gemini ซึ่งเป็นแพลตฟอร์มการประมวลผลและการจัดการเอกสารอันทรงพลังผ่าน API ที่ใช้งานง่าย นี่คือสิ่งที่คุณสามารถทำได้ด้วยไลบรารีนี้:
การจัดการไฟล์:
การอัปโหลดเนื้อหา:
การจัดการประวัติการแชท:
การสร้างเนื้อหา:
ข้อกำหนดเอาท์พุท:
ระบุรูปแบบเอาต์พุตที่ต้องการสำหรับผลลัพธ์ที่สร้างโดย Gemini API
การใช้ response_mime_type
และสคีมา JSON รูปแบบเอาต์พุตจะถูกควบคุม อ้างอิง
เพื่อทดสอบสคริปต์นี้ โปรดทำตามขั้นตอนต่อไปนี้
โปรดเข้าถึง https://makersuite.google.com/app/apikey และสร้างคีย์ API ของคุณ ในเวลานั้น โปรดเปิดใช้งาน Generative Language API ที่คอนโซล API คีย์ API นี้ใช้สำหรับสคริปต์ตัวอย่างนี้
สามารถดูเอกสารอย่างเป็นทางการนี้ได้ อ้างอิง
โปรดสร้างโครงการ Google Apps Script แบบสแตนด์อโลน แน่นอนว่าสคริปต์นี้สามารถใช้กับสคริปต์ที่ผูกกับคอนเทนเนอร์ได้
และโปรดเปิดตัวแก้ไขสคริปต์ของโครงการ Google Apps Script
การใช้ GeminiWithFiles มี 2 รูปแบบ
หากคุณใช้ไลบรารีนี้เป็นไลบรารี Google Apps Script โปรดติดตั้งไลบรารีนี้ในโครงการ Google Apps Script ของคุณดังนี้
สร้างโครงการ Google Apps Script หรือเปิดโครงการ Google Apps Script ของคุณ
ติดตั้งไลบรารีนี้
1dolXnIeXKz-BH1BlwRDaKhzC2smJcGyVxMxGYhaY2kqiLa857odLXrIC
หากคุณใช้ไลบรารีนี้ในโครงการ Google Apps Script ของคุณเอง โปรดคัดลอกและวางสคริปต์ "classGeminiWithFiles.js" ในโครงการ Google Apps Script ของคุณ เท่านี้ก็สามารถใช้สคริปท์ได้แล้ว
"main.js" ใช้สำหรับไลบรารี Google Apps Script ดังนั้นในรูปแบบนี้คุณไม่จำเป็นต้องใช้มัน
ไลบรารีนี้ใช้ 2 ขอบเขตต่อไปนี้
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/drive
หากคุณต้องการใช้โทเค็นการเข้าถึง โปรดเชื่อมโยงโครงการ Google Cloud Platform กับโครงการสคริปต์ Google Apps และโปรดเพิ่มขอบเขตต่อไปนี้
https://www.googleapis.com/auth/generative-language
นอกจากนี้ คุณสามารถดูเอกสารอย่างเป็นทางการของ Gemini API ได้ที่ https://ai.google.dev/api/rest
วิธีการ | คำอธิบาย |
---|---|
setFileIds (fileIds, asImage = false) | ตั้งค่า ID ไฟล์ |
setBlobs(หยด) | ตั้งหยด |
withUploadedFilesByGenerateContent (รายการไฟล์ = []) | สร้างอ็อบเจ็กต์สำหรับการใช้เมธอด createContent |
อัพโหลดไฟล์ (n = 50) | อัปโหลดไฟล์ไปยังราศีเมถุน |
getFileList() | รับรายชื่อไฟล์ในราศีเมถุน |
ลบไฟล์(ชื่อ, n = 50) | ลบไฟล์ออกจากราศีเมถุน |
สร้างเนื้อหา (วัตถุ) | วิธีการหลัก. สร้างเนื้อหาโดย Gemini API |
setFileIdsOrUrlsWithResumableUpload (วัตถุ) | ไฟล์ที่มีขนาดเกิน 50 MB สามารถอัปโหลดไปยัง Gemini ได้ |
เมื่อคุณติดตั้ง GeminiWithFiles เป็นไลบรารีในโครงการ Google Apps Script โปรดใช้สคริปต์ต่อไปนี้
const g = GeminiWithFiles . geminiWithFiles ( object ) ;
หรือ
เมื่อคุณคัดลอกและวางสคริปต์ของ Class GeminiWithFiles โดยตรงในโครงการ Google Apps Script โปรดใช้สคริปต์ต่อไปนี้
const g = new GeminiWithFiles ( object ) ;
มูลค่าของ object
มีดังนี้
{Object} object API key or access token for using Gemini API.
{String} object.apiKey API key.
{String} object.accessToken Access token.
{String} object.model Model. Default is "models/gemini-1.5-pro-latest".
{String} object.version Version of API. Default is "v1beta".
{Boolean} object.doCountToken Default is false. If this is true, when Gemini API is requested, the token of request is shown in the log.
{Array} object.history History for continuing chat.
{Array} object.functions If you want to give the custom functions, please use this.
{String} object.response_mime_type In the current stage, only "application/json" can be used.
{String} object.responseMimeType In the current stage, only "application/json" can be used.
{Object} object.response_schema JSON schema for controlling the output format.
{Object} object.responseSchema JSON schema for controlling the output format.
{Number} object.temperature Control the randomness of the output.
{Object} object.systemInstruction Ref: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini.
{Boolean} object.exportTotalTokens When this is true, the total tokens are exported as the result value. At that time, the generated content and the total tokens are returned as an object.
{Boolean} object.exportRawData The default value is false. When this is true, the raw data returned from Gemini API is returned.
{Object} object.toolConfig The default is null. If you want to directly give the object of "toolConfig", please use this.
{Array} object.tools The default value is null. For example, when you want to use "codeExecution", please set `tools: [{ codeExecution: {}}]`.
{PropertiesService.Properties} object.propertiesService PropertiesService.getScriptProperties()
{Boolean} object.resumableUploadAsNewUpload When you want to upload the data with the resumable upload as new upload, please set this as true. The default is false.
เมื่อคุณต้องการใช้ response_mime_type
โปรดให้ jsonSchema
เพื่อสร้างเมธอด Content ในขั้นตอนปัจจุบัน เฉพาะ "application/json"
เท่านั้นที่สามารถใช้เพื่อ response_mime_type
เมื่อคุณต้องการใช้ systemInstruction
โปรดยืนยันเอกสารอย่างเป็นทางการ Ref.
Gemini 1.5 Flash ล่าสุด ( models/gemini-1.5-flash-latest
) ถูกใช้เป็นโมเดลเริ่มต้น เมื่อคุณต้องการใช้ Gemini 1.5 Pro ล่าสุด ( models/gemini-1.5-pro-latest
) โปรดใช้เช่น const g = GeminiWithFiles.geminiWithFiles({ apiKey, model: "models/gemini-1.5-pro-latest" })
.
ในขั้นตอนปัจจุบัน เมื่อใช้ response_schema
ระบบจะใช้ response_mime_type: "application/json"
โดยอัตโนมัติ
ตั้งค่า ID ไฟล์ ไฟล์รหัสไฟล์จะถูกอัปโหลดไปยังราศีเมถุน
ในกรณีนี้ จะใช้ async/await ในฟังก์ชัน
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const folderId = "###" ; // Please set your folder ID including images.
let fileIds = [ ] ;
const files = DriveApp . getFolderById ( folderId ) . getFiles ( ) ;
while ( files . hasNext ( ) ) {
const file = files . next ( ) ;
fileIds . push ( file . getId ( ) ) ;
}
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . setFileIds ( fileIds , false ) . uploadFiles ( ) ;
console . log ( res ) ;
}
setFileIds
คือ String[] (รหัสไฟล์บน Google Drive) และบูลีน ตามลำดับ หากอาร์กิวเมนต์ที่ 2 เป็นเท็จ ไฟล์ที่ป้อนของรหัสไฟล์จะถูกอัปโหลดเป็นข้อมูลดิบ หากอาร์กิวเมนต์ที่ 2 เป็นจริง ไฟล์ที่ป้อนของ ID ไฟล์จะถูกแปลงเป็นข้อมูลรูปภาพและจะถูกอัปโหลด ค่าเริ่มต้นของอาร์กิวเมนต์ที่ 2 เป็นเท็จfalse
ในวิธีนี้ได้ เช่น setFileIds(fileIds, false)
ตั้งหยด Blobs ถูกอัปโหลดไปยังราศีเมถุน
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const folderId = "###" ; // Please set your folder ID including images.
const blobs = [ ] ;
const files = DriveApp . getFolderById ( folderId ) . getFiles ( ) ;
while ( files . hasNext ( ) ) {
blobs . push ( files . next ( ) . getBlob ( ) ) ;
}
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . setBlobs ( blobs ) . uploadFiles ( ) ;
console . log ( res ) ;
}
setBlobs
คือ Blob[]สร้างอ็อบเจ็กต์สำหรับการใช้เมธอด createContent
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const q = "###" ; // Please set your question.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const fileList = g . getFileList ( ) ;
const res = g
. withUploadedFilesByGenerateContent ( fileList )
. generateContent ( { q } ) ;
console . log ( res ) ;
}
withUploadedFilesByGenerateContent
มีอาร์กิวเมนต์เดียวเท่านั้น นั่นคือค่าจากเมธอด getFileList คุณสามารถดูค่าจริงได้หลังจากที่คุณอัปโหลดไฟล์อัปโหลดไฟล์ไปยังราศีเมถุน ไฟล์จะถูกอัปโหลดไปยังราศีเมถุนโดยใช้ ID ไฟล์ที่ป้อนหรือ blobs
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const fileIds = [ "###fileId1###" , "###fileId2###" , , , ] ; // Please set your file IDs in this array.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . setFileIds ( fileIds , false ) . uploadFiles ( ) ;
console . log ( res ) ;
}
ในสคริปต์นี้ ไฟล์ของ fileIds
จะถูกอัปโหลดไปยัง Gemini พร้อมด้วยข้อมูลดิบ หาก setFileIds(fileIds, false)
ถูกแก้ไขเป็น setFileIds(fileIds, true)
ไฟล์จะถูกอัพโหลดไปยัง Gemini เป็นรูปภาพ
เมื่อคุณใช้ Blob โดยตรง คุณสามารถใช้สคริปต์ต่อไปนี้
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const fileIds = [ "###fileId1###" , "###fileId2###" , , , ] ; // Please set your file IDs in this array.
const blobs = fileIds . map ( id => DriveApp . getFileById ( id ) . getBlob ( ) ) ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . setBlobs ( blobs ) . uploadFiles ( ) ;
console . log ( res ) ;
}
รับรายชื่อไฟล์ในราศีเมถุน
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . getFileList ( ) ;
console . log ( res ) ;
}
ลบไฟล์ออกจากราศีเมถุน
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const names = g . getFileList ( ) . map ( ( { name } ) => name ) ;
if ( names . length == 0 ) return ;
g . deleteFiles ( names ) ;
console . log ( ` ${ names . length } files were deleted.` ) ;
}
วิธีการหลัก. สร้างเนื้อหาโดย Gemini API สามารถดูสคริปต์ตัวอย่างเพิ่มเติมได้ในส่วน "สคริปต์ตัวอย่าง" ต่อไปนี้
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . generateContent ( { q : "What is Google Apps Script?" } ) ;
console . log ( res ) ;
}
ในสคริปต์นี้ เนื้อหาถูกสร้างขึ้นด้วยการเรียกใช้ฟังก์ชัน
เมื่อคุณต้องการใช้ response_mime_type
โปรดให้ jsonSchema
สร้างเมธอด Content ดังนี้ ในกรณีนี้ ด้วยการให้เฉพาะสคีมา JSON ไลบรารีนี้สามารถส่งคืนออบเจ็กต์ที่ถูกต้องได้ คุณยังสามารถดูข้อมูลโดยละเอียดเกี่ยวกับ response_mime_type
ได้ที่รายงานของฉัน
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( {
apiKey ,
response_mime_type : "application/json" ,
} ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, response_mime_type: "application/json" }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const jsonSchema = {
title : "5 popular cookie recipes" ,
description : "List 5 popular cookie recipes." ,
type : "array" ,
items : {
type : "object" ,
properties : {
recipe_name : {
description : "Names of recipe." ,
type : "string" ,
} ,
} ,
} ,
} ;
const res = g . generateContent ( { jsonSchema } ) ;
console . log ( res ) ;
}
เมื่อรันสคริปต์นี้ จะได้ผลลัพธ์ต่อไปนี้
[
{ "recipe_name" : " Chocolate Chip Cookies " },
{ "recipe_name" : " Peanut Butter Cookies " },
{ "recipe_name" : " Oatmeal Cookies " },
{ "recipe_name" : " Sugar Cookies " },
{ "recipe_name" : " Snickerdoodle Cookies " }
]
วิธีนี้สามารถอัปโหลดไฟล์ได้มากกว่า 50 MB
จาก v2.xx สามารถทำได้ นี่มาจาก Ref และ Ref
จากเวอร์ชัน 2.0.3 เมื่อคุณใช้เมธอดนี้ โปรดรวม propertiesService: PropertiesService.getScriptProperties()
ลงในอ็อบเจ็กต์เริ่มต้นดังต่อไปนี้ เนื่องจากเมื่อใช้ PropertiesService.getScriptProperties()
ในไลบรารี ค่าต่างๆ จะถูกใส่ลงในไลบรารี เมื่อฉันสร้าง Ref and Ref ฉันคิดว่าสคริปต์นั้นถูกใช้โดยการคัดลอกและวางแทนไลบรารี ดังนั้นฉันจึงรวม PropertiesService.getScriptProperties()
ไว้ในสคริปต์ แต่ฉันสังเกตเห็นว่าเมื่อใช้สิ่งนี้กับ GeminiWithFiles ผู้ใช้แต่ละคนจะต้องใช้ PropertiesService.getScriptProperties()
ดังนั้นฉันจึงแก้ไขสิ่งนี้
สคริปต์ตัวอย่างมีดังนี้
function myFunction ( ) {
// This URL is from https://github.com/google/generative-ai-docs/blob/main/site/en/gemini-api/docs/prompting_with_media.ipynb
const url = "https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4" ; // 64,657,027 bytes
const apiKey = "###" ; // Please set your API key.
const q = "Description this video." ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , propertiesService : PropertiesService . getScriptProperties ( ) } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, propertiesService: PropertiesService.getScriptProperties() }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const fileList = g . setFileIdsOrUrlsWithResumableUpload ( [ { url } ] ) . uploadFiles ( ) ;
Utilities . sleep ( 10000 ) ; // This might be required to be used because the state of the uploaded file might not be active.
const res = g . withUploadedFilesByGenerateContent ( fileList ) . generateContent ( { q } ) ;
console . log ( res ) ;
}
เมื่อสคริปต์นี้รัน สามารถดูบันทึกต่อไปนี้ได้ที่บันทึก
- Get metadata
- Calculate chunks
- Get location
- Download and upload data.
- Now... 1/4
- Start downloading data with 0-16777215
- Finished downloading data with 0-16777215
- Start uploading data with 0-16777215
- Finished uploading data with 0-16777215
- Upload the next chunk.
- Now... 2/4
- Start downloading data with 16777216-33554431
- Finished downloading data with 16777216-33554431
- Start uploading data with 16777216-33554431
- Finished uploading data with 16777216-33554431
- Upload the next chunk.
- Now... 3/4
- Start downloading data with 33554432-50331647
- Finished downloading data with 33554432-50331647
- Start uploading data with 33554432-50331647
- Finished uploading data with 33554432-50331647
- Upload the next chunk.
- Now... 4/4
- Start downloading data with 50331648-64657026
- Finished downloading data with 50331648-64657026
- Start uploading data with 50331648-64657026
- Finished uploading data with 50331648-64657026
- Done.
- Now, the state of the uploaded files "url@https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4$page@1$maxPage@1" is not active. So, it will wait until it is active. Please wait for 10 seconds. Retry (1/3)
- 1 uploaded files are used with generateCotent.
- The video is a cartoon that shows a large, white rabbit in a field. The rabbit is shown waking up from a nap and then is seen eating an apple. After eating the apple, the rabbit is approached by a bird. The rabbit is scared of the bird and tries to hide from it. The bird flies away. The rabbit is seen smiling and then a squirrel flies toward the rabbit. The squirrel is startled by the rabbit and flies away. The rabbit is then seen catching another squirrel with its vine and the scene ends with a close-up of the rabbit's face.
หากไฟล์ของคุณมีขนาดใหญ่และสถานะของไฟล์ที่อัปโหลดยังไม่เป็น "ใช้งานอยู่" โปรดทดสอบสคริปต์ต่อไปนี้
function myFunction ( ) {
// This URL is from https://github.com/google/generative-ai-docs/blob/main/site/en/gemini-api/docs/prompting_with_media.ipynb
const url = "https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4" ; // 64,657,027 bytes
const apiKey = "###" ; // Please set your API key.
const q = "Description this video." ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , propertiesService : PropertiesService . getScriptProperties ( ) } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, propertiesService: PropertiesService.getScriptProperties() }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const fileList = g . setFileIdsOrUrlsWithResumableUpload ( [ { url } ] ) . uploadFiles ( ) ;
console . log ( JSON . stringify ( fileList ) ) ;
// Please copy the value of "fileList".
}
เท่านี้ก็สามารถอัพโหลดไฟล์ได้แล้ว และคุณสามารถใช้ไฟล์ที่อัปโหลดได้หลังจากรอเวลาเพียงพอเพื่อเปลี่ยนสถานะเป็น "ใช้งานอยู่" ไฟล์ที่อัพโหลดสามารถใช้งานได้ดังนี้