Gemini API terus berkembang. Pada tanggal 5 Agustus 2024, saya memperbarui sebagian besar GeminiWithFiles ke v2.xx Dengan pembaruan besar ini, versi diubah dari v1 ke v2.
Jika Anda ingin menggunakan GeminiWithFiles v1.xx, silakan lihat di sini.
Ini adalah perpustakaan Google Apps Script untuk Gemini API dengan file.
Pustaka Google Apps Script baru bernama GeminiWithFiles menyederhanakan penggunaan Gemini, model bahasa besar, untuk memproses data tidak terstruktur seperti gambar dan PDF. GeminiWithFiles dapat mengunggah file, menghasilkan konten, dan membuat deskripsi dari banyak gambar sekaligus. Ini secara signifikan mengurangi beban kerja dan memperluas kemungkinan penggunaan Gemini.
Baru-baru ini, Gemini, model bahasa besar dari Google AI, telah menghadirkan kemungkinan baru untuk berbagai tugas dengan memungkinkan penggunaan data tidak terstruktur sebagai data terstruktur. Hal ini sangat penting karena sejumlah besar informasi terdapat dalam format tidak terstruktur seperti dokumen teks, gambar, dan video.
API Gemini 1.5, yang dirilis baru-baru ini, secara signifikan memperluas kemampuan ini. Ini dapat menghasilkan konten hingga 1 juta token, peningkatan substansial dibandingkan versi sebelumnya. Selain itu, Gemini 1.5 kini dapat memproses hingga 3.000 file gambar, jauh melebihi batas 16 gambar pada Gemini 1.0. Ref
Meskipun Gemini tidak dapat langsung bekerja dengan format Google Drive seperti Dokumen, Spreadsheet, dan Slide, ada solusinya. Pada tahap saat ini, data PDF bisa langsung diproses dengan Gemini API. Dengan menggunakan ini, file Google Docs tersebut dikonversi ke PDF dan digunakan dengan Gemini API. Ref
Laporan ini memperkenalkan pustaka Google Apps Script baru yang disebut "GeminiWithFiles" yang menyederhanakan proses ini. GeminiWithFiles memungkinkan pengguna mengunggah file dan menghasilkan konten dengan mudah menggunakan kemampuan kuat Gemini. Hal ini juga memungkinkan pembuatan deskripsi yang efisien dari beberapa gambar dengan satu panggilan API, sehingga mengurangi beban kerja secara signifikan dibandingkan dengan memproses setiap gambar satu per satu seperti yang ditunjukkan dalam laporan saya sebelumnya. Ref
Dengan menyederhanakan proses dan memperluas kemampuan, GeminiWithFiles menjanjikan berbagai kasus penggunaan di berbagai domain. Laporan ini berfungsi sebagai pendekatan yang diperluas dari laporan sebelumnya, yang bertujuan untuk lebih mengurangi biaya proses dan meningkatkan efisiensi saat bekerja dengan Gemini dan data tidak terstruktur.
Saya membuat perpustakaan ini berdasarkan laporan berikut.
Perpustakaan GeminiWithFiles ini memungkinkan Anda berinteraksi dengan Gemini, platform pemrosesan dan manajemen dokumen yang kuat, melalui API yang mudah digunakan. Inilah yang dapat Anda capai dengan perpustakaan ini:
Manajemen File:
Unggah Konten:
Manajemen Riwayat Obrolan:
Pembuatan Konten:
Spesifikasi Keluaran:
Tentukan format output yang diinginkan untuk hasil yang dihasilkan oleh Gemini API.
Menggunakan skema response_mime_type
dan JSON, format output dikontrol. Ref
Untuk menguji skrip ini, silakan lakukan langkah-langkah berikut.
Silakan akses https://makersuite.google.com/app/apikey dan buat kunci API Anda. Pada saat itu, aktifkan API Bahasa Generatif di konsol API. Kunci API ini digunakan untuk contoh skrip ini.
Dokumen resmi ini juga dapat dilihat. Ref.
Harap buat proyek Skrip Google Apps mandiri. Tentu saja, skrip ini juga dapat digunakan dengan skrip yang terikat pada container.
Dan silahkan buka script editor proyek Google Apps Script.
Ada 2 pola untuk menggunakan GeminiWithFiles.
Jika Anda menggunakan perpustakaan ini sebagai perpustakaan Google Apps Script, harap pasang perpustakaan tersebut ke proyek Google Apps Script Anda sebagai berikut.
Buat proyek Skrip Google Apps. Atau, buka proyek Google Apps Script Anda.
Instal perpustakaan ini.
1dolXnIeXKz-BH1BlwRDaKhzC2smJcGyVxMxGYhaY2kqiLa857odLXrIC
Jika Anda menggunakan perpustakaan ini dalam proyek Skrip Google Apps Anda sendiri, salin dan tempelkan skrip "classGeminiWithFiles.js" ke proyek Skrip Google Apps Anda. Dengan ini, skrip dapat digunakan.
"main.js" digunakan untuk perpustakaan Google Apps Script. Jadi, pada pola ini, Anda tidak diwajibkan untuk menggunakannya.
Perpustakaan ini menggunakan 2 cakupan berikut.
https://www.googleapis.com/auth/script.external_request
https://www.googleapis.com/auth/drive
Jika Anda ingin menggunakan token akses, harap tautkan Proyek Google Cloud Platform ke Proyek Google Apps Script. Dan, silakan tambahkan cakupan berikut.
https://www.googleapis.com/auth/generative-language
Anda juga dapat melihat dokumen resmi Gemini API di https://ai.google.dev/api/rest.
Metode | Keterangan |
---|---|
setFileIds(fileIds, asImage = false) | Tetapkan ID file. |
setBlob(gumpalan) | Atur gumpalan. |
denganUploadedFilesByGenerateContent(fileList = []) | Buat objek untuk menggunakan metode generateContent. |
unggahFile(n = 50) | Unggah file ke Gemini. |
dapatkanDaftar File() | Dapatkan daftar file di Gemini. |
deleteFiles(nama, n = 50) | Hapus file dari Gemini. |
menghasilkan Konten (objek) | Metode utama. Hasilkan konten dengan Gemini API. |
setFileIdsOrUrlsWithResumableUpload(objek) | File lebih dari 50 MB dapat diunggah ke Gemini. |
Saat Anda memasang GeminiWithFiles sebagai pustaka untuk proyek Skrip Google Apps Anda, harap gunakan skrip berikut.
const g = GeminiWithFiles . geminiWithFiles ( object ) ;
atau
Saat Anda langsung menyalin dan menempelkan skrip Kelas GeminiWithFiles ke proyek Skrip Google Apps Anda, silakan gunakan skrip berikut.
const g = new GeminiWithFiles ( object ) ;
Nilai object
adalah sebagai berikut.
{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.
Jika Anda ingin menggunakan response_mime_type
, berikan jsonSchema
ke metode generateContent. Pada tahap saat ini, hanya "application/json"
yang dapat digunakan untuk response_mime_type
.
Bila Anda ingin menggunakan systemInstruction
, harap konfirmasikan dokumen resmi Ref.
Gemini 1.5 Flash Terbaru ( models/gemini-1.5-flash-latest
) digunakan sebagai model default. Bila ingin menggunakan Gemini 1.5 Pro Terbaru ( models/gemini-1.5-pro-latest
), silakan gunakan seperti const g = GeminiWithFiles.geminiWithFiles({ apiKey, model: "models/gemini-1.5-pro-latest" })
.
Pada tahap saat ini, ketika response_schema
digunakan, response_mime_type: "application/json"
secara otomatis digunakan.
Tetapkan ID file. File ID file diunggah ke Gemini.
Dalam hal ini, async/await digunakan dalam fungsi tersebut.
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
ke-1 dan ke-2 adalah String[] (ID file di Google Drive) dan boolean. Jika argumen ke-2 salah, file ID file yang dimasukkan akan diunggah sebagai data mentah. Jika argumen ke-2 benar, file ID file yang dimasukkan akan dikonversi menjadi data gambar dan diunggah. Default argumen ke-2 adalah salah.false
pada metode ini seperti setFileIds(fileIds, false)
. Atur gumpalan. Gumpalan tersebut diunggah ke Gemini.
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
adalah Blob[].Buat objek untuk menggunakan metode generateContent.
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
hanya memiliki satu argumen. Itu adalah nilai dari metode getFileList. Anda dapat melihat nilai sebenarnya setelah Anda mengunggah file.Unggah file ke Gemini. File diunggah ke Gemini menggunakan ID file atau blob yang dimasukkan.
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 ) ;
}
Dalam skrip ini, file fileIds
diunggah ke Gemini dengan data mentah. Jika setFileIds(fileIds, false)
diubah menjadi setFileIds(fileIds, true)
, file akan diunggah ke Gemini sebagai gambar.
Saat Anda langsung menggunakan Blob, Anda dapat menggunakan skrip berikut.
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 ) ;
}
Dapatkan daftar file di Gemini.
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 ) ;
}
Hapus file dari Gemini.
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.` ) ;
}
Metode utama. Hasilkan konten dengan Gemini API. Contoh skrip selengkapnya dapat dilihat pada bagian “Contoh skrip” berikut.
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 ) ;
}
Dalam skrip ini, konten dihasilkan dengan pemanggilan fungsi.
Bila ingin menggunakan response_mime_type
, berikan metode jsonSchema
ke generateContent sebagai berikut. Dalam hal ini, dengan hanya memberikan skema JSON, perpustakaan ini dapat mengembalikan objek yang valid. Anda juga dapat melihat informasi detail tentang response_mime_type
di laporan saya.
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 ) ;
}
Ketika skrip ini dijalankan, diperoleh hasil sebagai berikut.
[
{ "recipe_name" : " Chocolate Chip Cookies " },
{ "recipe_name" : " Peanut Butter Cookies " },
{ "recipe_name" : " Oatmeal Cookies " },
{ "recipe_name" : " Sugar Cookies " },
{ "recipe_name" : " Snickerdoodle Cookies " }
]
Cara ini dapat mengupload file berukuran lebih dari 50 MB.
Dari v2.xx, hal ini dapat dicapai. Ini dari Ref dan Ref.
Mulai v2.0.3, saat Anda menggunakan metode ini, harap sertakan propertiesService: PropertiesService.getScriptProperties()
ke dalam objek awal sebagai berikut. Karena, ketika PropertiesService.getScriptProperties()
digunakan di perpustakaan, nilainya dimasukkan ke dalam perpustakaan. Saat saya membuat Ref dan Ref, saya mengira skrip tersebut digunakan dengan cara copy dan paste, bukan perpustakaan. Jadi, saya menyertakan PropertiesService.getScriptProperties()
dalam skrip. Tapi saya perhatikan bahwa ketika ini digunakan dengan GeminiWithFiles, setiap pengguna diharuskan menggunakan PropertiesService.getScriptProperties()
. Jadi, saya memodifikasi ini.
Contoh skripnya adalah sebagai berikut.
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 ) ;
}
Saat skrip ini dijalankan, log berikut dapat dilihat di log.
- 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.
Jika file anda berukuran besar dan keadaan file yang diupload belum masih "AKTIF", silahkan test script berikut ini.
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".
}
Dengan ini, file dapat diunggah. Dan, Anda dapat menggunakan file yang diunggah setelah menunggu cukup waktu untuk mengubah status menjadi "AKTIF". File yang diunggah dapat digunakan sebagai berikut.
function myFunction ( ) {
const fileList = [ ### ] ; // This is from the above script.
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 res = g . withUploadedFilesByGenerateContent ( fileList ) . generateContent ( { q } ) ;
console . log ( res ) ;
}
Sebagai opsi tambahan, ketika Anda ingin mengupload data dengan resumable upload sebagai upload baru, silakan atur resumableUploadAsNewUpload: true
sebagai berikut. Dengan ini, properti dibersihkan dan pengunggahan dijalankan.
function myFunction ( ) {
const fileList = [ ### ] ; // This is from the above script.
const apiKey = "###" ; // Please set your API key.
const q = "Description this video." ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , propertiesService : PropertiesService . getScriptProperties ( ) , resumableUploadAsNewUpload : true } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, propertiesService: PropertiesService.getScriptProperties(), resumableUploadAsNewUpload: true }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . withUploadedFilesByGenerateContent ( fileList ) . generateContent ( { q } ) ;
console . log ( res ) ;
}
Ketika q
digunakan, hanya pertanyaan teks yang dapat digunakan untuk menghasilkan konten. Bila Anda ingin menggunakan suku cadang khusus Anda, Anda dapat melakukannya sebagai berikut.
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 parts = [ { text : "What is Google Apps Script?" } ] ;
const res = g . generateContent ( { parts } ) ;
console . log ( res ) ;
}
Saat Anda ingin menggunakan pemanggilan fungsi, Anda dapat menggunakan contoh skrip berikut.
function myFunction_functionCalling ( ) {
const apiKey = "###" ; // Please set your API key.
// Sample functions
const functions = {
params_ : {
getTanaike : {
description : "Get information about Tanaike. Value is a text." ,
} ,
} ,
getTanaike : (
_ // ref: https://tanaikech.github.io/about/
) =>
"As a Japanese scientist holding a Ph.D. in Physics, I am also a Google Developer Expert (GDE) in Google Workspace and a Google Cloud Champion Innovator. I am driven by a deep curiosity to explore, think creatively, and ultimately create new things. Specifically, I have a passion for crafting innovative solutions that are entirely novel, solutions that haven't yet been introduced to the world. It's in this spirit that I approach innovation. Interestingly, these new ideas often come to me during sleep, which I then strive to bring to life in the real world. Thankfully, some of these have already found practical applications." ,
} ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , functions } ) ; // 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 res = g . generateContent ( { q : "What is Tanaike? Return answer within 50 words." } ) ;
console . log ( res ) ;
}
Contoh fungsi ini berasal dari posting ini.
Saat Anda ingin mengembalikan data mentah dari Gemini API, Anda juga dapat menggunakan contoh skrip berikut.
function myFunction_generateContent1b ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , exportRawData : true } ) ; // 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 res1 = g . generateContent ( { q : "What is Google Apps Script?" } ) ;
console . log ( JSON . stringify ( res1 ) ) ;
}
Dengan menggunakan exportRawData: true
, Anda dapat mengambil data mentah dari Gemini API sebagai berikut.
[
{
"candidates" :[
{
"content" :{
"parts" :[
{
"text" : " Google Apps Script is ... "
}
],
"role" : " model "
},
"finishReason" : " STOP " ,
"index" : 0 ,
"safetyRatings" :[
{
"category" : " HARM_CATEGORY_SEXUALLY_EXPLICIT " ,
"probability" : " NEGLIGIBLE "
},
{
"category" : " HARM_CATEGORY_HATE_SPEECH " ,
"probability" : " NEGLIGIBLE "
},
{
"category" : " HARM_CATEGORY_HARASSMENT " ,
"probability" : " NEGLIGIBLE "
},
{
"category" : " HARM_CATEGORY_DANGEROUS_CONTENT " ,
"probability" : " NEGLIGIBLE "
}
]
}
],
"usageMetadata" :{
"promptTokenCount" : 7 ,
"candidatesTokenCount" : 459 ,
"totalTokenCount" : 466
}
}
]
console . log ( GeminiWithFiles . geminiWithFiles ( ) . functions ) ;
atau
console . log ( new GeminiWithFiles ( ) . functions ) ;
function myFunction_history ( ) {
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.
// Question 1
const res1 = g . generateContent ( { q : "What is Google Apps Script?" } ) ;
console . log ( res1 ) ;
// Question 2
const res2 = g . generateContent ( { q : "What is my 1st question?" } ) ;
console . log ( res2 ) ;
console . log ( g . history ) ; // Here
}
function myFunction ( ) {
const history = [ , , , ] ; // Please set your history.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , history } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, history }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . generateContent ( { q : "What is my 1st question?" } ) ;
console . log ( res ) ;
}
Dalam penjelasan ini, ketika skrip ini digunakan sebagai pustaka Skrip Google Apps, untuk membuat konstruktor digunakan GeminiWithFiles.geminiWithFiles
. Jika skrip ini digunakan dengan langsung menyalin dan menempelkannya ke proyek Google Apps Script Anda, new GeminiWithFiles
akan digunakan, bukan GeminiWithFiles.geminiWithFiles
. Harap berhati-hati tentang hal ini.
Contoh skripnya adalah sebagai berikut.
Skrip ini menghasilkan konten dari sebuah teks.
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 ) ;
}
const g = new GeminiWithFiles.geminiWithFiles({ apiKey });
.const g = new Gemini({ apiKey });
. Skrip ini menghasilkan konten dengan obrolan.
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.
// Question 1
const res1 = g . generateContent ( { q : "What is Google Apps Script?" } ) ;
console . log ( res1 ) ;
// Question 2
const res2 = g . generateContent ( { q : "What is my 1st question?" } ) ;
console . log ( res2 ) ;
}
Saat skrip ini dijalankan, res1
dan res2
adalah sebagai berikut.
res1
Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with Google Workspace.
res2
Your first question was "What is Google Apps Script?"
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , doCountToken : true } ) ; // 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.
// Question 1
const q =
"Return the current population of Kyoto, Osaka, Aichi, Fukuoka, Tokyo in Japan as JSON data with the format that the key and values are the prefecture name and the population, respectively." ;
const res1 = g . generateContent ( { q } ) ;
console . log ( res1 ) ;
// Question 2
const res2 = g . generateContent ( {
q : "Also, return the current area of them as JSON data with the format that the key and values are the prefecture name and the area (km^2), respectively." ,
} ) ;
console . log ( res2 ) ;
}
Saat skrip ini dijalankan, nilai berikut dapat dilihat di log. Dengan doCountToken: true
, Anda dapat melihat total token.
{
"totalTokens": 40
}
res1
{
Kyoto: 1464956,
Fukuoka: 5135214,
Osaka: 8838716,
Tokyo: 14047594,
Aichi: 7552873
}
{
"totalTokens": 77
}
res2
{
Kyoto: 4612.71,
Tokyo: 2194.07,
Aichi: 5172.4,
Osaka: 1904.99,
Fukuoka: 4986.51
}
Dalam hal ini, async/await digunakan dalam fungsi tersebut.
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 , doCountToken : true } ) ; // 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 ) ;
}
Exceeded maximum execution time
. Harap berhati-hati tentang hal ini. Dalam contoh ini, beberapa file gambar diunggah dan deskripsi dibuat dari file gambar yang diunggah. Contoh ini akan menjadi versi yang diperluas dari laporan saya sebelumnya "Membuat Deskripsi File di Google Drive Secara Otomatis menggunakan Gemini Pro API dengan Google Apps Script".
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const folderId = "###" ; // Please set your folder ID including images.
const q = [
`Create each description from each image file within 100 words in the order of given fileData.` ,
`Return the results as an array` ,
`Return only raw Array without a markdown. No markdown format.` ,
`The required properties of each element in the array are as follows` ,
`` ,
`[Properties of each element in the array]` ,
`"name": "Name of file"` ,
`"description": "Created description"` ,
`` ,
`If the requirement information is not found, set "no value".` ,
`Return only raw Array without a markdown. No markdown format. No markdown tags.` ,
] . join ( "n" ) ;
const fileIds = [ ] ;
const files = DriveApp . searchFiles (
`(mimeType = 'image/png' or mimeType = 'image/jpeg') and trashed = false and ' ${ folderId } ' in parents`
) ;
while ( files . hasNext ( ) ) {
fileIds . push ( files . next ( ) . getId ( ) ) ;
}
if ( fileIds . length == 0 ) return ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , doCountToken : true , response_mime_type : "application/json" } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, doCountToken: true, response_mime_type: "application/json" }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const fileList = g . setFileIds ( fileIds ) . uploadFiles ( ) ;
const res = g
. withUploadedFilesByGenerateContent ( fileList )
. generateContent ( { q } ) ;
// g.deleteFiles(fileList.map(({ name }) => name)); // If you want to delete the uploaded files, please use this.
console . log ( res ) ;
}
Ketika skrip ini dijalankan, diperoleh hasil sebagai berikut. Dalam hal ini, nilai name
adalah ID file.
[
{
"name" : " ### " ,
"description" : " ### "
},
,
,
,
]
Ketika 20 contoh gambar yang dihasilkan oleh Gemini digunakan, diperoleh hasil sebagai berikut.
Saat skrip ini dijalankan, 20 gambar diunggah dan deskripsi dari 20 gambar yang diunggah dapat diperoleh dengan satu panggilan API.
Poin penting, dalam pengujian saya, ketika jumlah file gambar banyak, skrip harus dipisahkan antara pengunggahan file dan pembuatan konten. Selain itu, jika terdapat 50 file gambar, deskripsinya dapat dibuat dengan benar. Namun, pada kasus lebih dari 50 gambar, ada kemungkinan terjadi kesalahan. Jadi silahkan sesuaikan jumlah filenya dengan keadaan anda.
Dalam contoh ini, beberapa faktur file PDF diunggah dan diurai sebagai objek. Contoh ini akan menjadi versi yang diperluas dari laporan saya sebelumnya "Parsing Invoice menggunakan Gemini 1.5 API dengan Google Apps Script".
function myFunction_parseInvoices ( ) {
const apiKey = "###" ; // Please set your API key.
// Please set file IDs of PDF file of invoices on Google Drive.
const fileIds = [
"###fileID1###" ,
"###fileID2###" ,
,
,
,
] ;
const q = [
`Create an array including JSON object parsed the following images of the invoices.` ,
`The giving images are the invoices.` ,
`Return an array including JSON object.` ,
`No descriptions and explanations. Return only raw array including JSON objects without markdown. No markdown format.` ,
`The required properties in each JSON object in an array are as follows.` ,
`` ,
`[Properties in JSON object]` ,
`"name": "Name given as 'Filename'"` ,
`"invoiceTitle": "title of invoice"` ,
`"invoiceDate": "date of invoice"` ,
`"invoiceNumber": "number of the invoice"` ,
`"invoiceDestinationName": "Name of destination of invoice"` ,
`"invoiceDestinationAddress": "address of the destination of invoice"` ,
`"totalCost": "total cost of all costs"` ,
`"table": "Table of invoice. This is a 2-dimensional array. Add the first header row to the table in the 2-dimensional array."` ,
`` ,
`[Format of 2-dimensional array of "table"]` ,
`"title or description of item", "number of items", "unit cost", "total cost"` ,
`` ,
`If the requirement information is not found, set "no value".` ,
`Return only raw array including JSON objects without markdown. No markdown format. No markcodn tags.` ,
] . join ( "n" ) ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , doCountToken : true , response_mime_type : "application/json" } ) ; // 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 . setFileIds ( fileIds ) . uploadFiles ( ) ;
const res = g . withUploadedFilesByGenerateContent ( fileList ) . generateContent ( { q } ) ;
// g.deleteFiles(fileList.map(({ name }) => name)); // If you want to delete the uploaded files, please use this.
console . log ( res ) ;
}
Sebagai contoh kertas, bila kertas berikut digunakan,
Contoh faktur ini berasal dari templat desain Faktur Microsoft.
Contoh faktur ini berasal dari templat desain Faktur Microsoft.
hasil berikut diperoleh dengan satu panggilan API. Ditemukan bahwa faktur data PDF yang diunggah dapat diurai dengan benar.
[
{
"name" : " ###fileID1### " ,
"invoiceDate" : " 4/1/2024 " ,
"totalCost" : " $192.50 " ,
"invoiceNumber" : " 100 " ,
"invoiceDestinationAddress" : " The Palm Tree Nursery \ n987 6th Ave \ nSanta Fe, NM 11121 " ,
"invoiceTitle" : " Invoice " ,
"invoiceDestinationName" : " Maria Sullivan " ,
"table" : [
[
" Salesperson " ,
" Job " ,
" Sales " ,
" Description " ,
" Unit Price " ,
" Line Total "
],
[ " Sonu Jain " , " " , " 20.00 " , " Areca palm " , " $2.50 " , " $50.00 " ],
[ " " , " " , " 35.00 " , " Majesty palm " , " $3.00 " , " $105.00 " ],
[ " " , " " , " 15.00 " , " Bismarck palm " , " $2.50 " , " $37.50 " ]
]
},
{
"name" : " ###fileID2### " ,
"invoiceDate" : " 4/5, 2024 " ,
"invoiceTitle" : " INVOICE " ,
"invoiceDestinationAddress" : " Downtown Pets \ n132 South Street \ nManhattan, NY 15161 " ,
"totalCost" : " $4350 " ,
"table" : [
[ " DESCRIPTION " , " HOURS " , " RATE " , " AMOUNT " ],
[ " Pour cement foundation " , " 4.00 " , " $150.00 " , " $600 " ],
[ " Framing and drywall " , " 16.00 " , " $180.00 " , " $2880 " ],
[ " Tiling and flooring install " , " 9.00 " , " $150.00 " , " $1350 " ]
],
"invoiceDestinationName" : " Nazar Neill " ,
"invoiceNumber" : " 4/5 "
}
]
Dalam sampel ini, beberapa makalah berisi data PDF diunggah, dan teks ringkasan untuk setiap makalah dikeluarkan.
function myFunction_parsePapers ( ) {
const apiKey = "###" ; // Please set your API key.
// Please set file IDs of the papers of PDF files.
const fileIds = [ "###fileID1###" , "###fileID2###" ] ;
const q = [
`Summary the following manuscripts within 500 words.` ,
`Return the results as an array` ,
`Return only raw Array without a markdown. No markdown format.` ,
`The required properties of each element in the array are as follows` ,
`` ,
`[Properties of each element in the array]` ,
`"name": "Name given as 'Filename'"` ,
`"title": "Title of manuscript` ,
`"summary": "Created description"` ,
`` ,
`If the requirement information is not found, set "no value".` ,
`Return only raw Array without a markdown. No markdown format. No markdown tags.` ,
] . join ( "n" ) ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , doCountToken : true } ) ; // 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 . setFileIds ( fileIds ) . uploadFiles ( ) ;
const res = g
. withUploadedFilesByGenerateContent ( fileList )
. generateContent ( { q } ) ;
// g.deleteFiles(fileList.map(({ name }) => name)); // If you want to delete the uploaded files, please use this.
console . log ( res ) ;
}
Sebagai contoh kertas, bila kertas berikut digunakan,
hasil berikut diperoleh dengan satu panggilan API. Ditemukan bahwa makalah yang diunggah yang dikonversi dari data PDF ke data gambar dapat diproses.
[
{
"name" : " ###fileID1### " ,
"title" : " The Particle Problem in the General Theory of Relativity " ,
"summary" : " This paper investigates the possibility of a singularity-free solution to the field equations in general relativity. The authors propose a new theoretical approach that eliminates singularities by introducing a new variable into the equations. They explore the implications of this approach for the understanding of particles, suggesting that particles can be represented as " bridges " connecting different sheets of spacetime. "
},
{
"name" : " ###fileID2### " ,
"title" : " Attention Is All You Need " ,
"summary" : " This paper proposes a novel neural network architecture called the Transformer, which relies entirely on an attention mechanism to draw global dependencies between input and output sequences. The Transformer model achieves state-of-the-art results on machine translation tasks and offers significant advantages in terms of parallelization and computational efficiency compared to recurrent neural networks. "
}
]
Pada tahap saat ini, hanya "application/json"
yang dapat digunakan untuk response_mime_type
.
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( {
apiKey ,
doCountToken : true ,
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 res1 = g . generateContent ( { q : "What is Google Apps Script?" } ) ;
console . log ( res1 ) ;
}
Dalam hal ini, hasilnya dikembalikan sebagai array sebagai berikut.
[
" Google Apps Script is a cloud-based scripting platform that lets you integrate with and automate tasks across Google products like Gmail, Calendar, Drive, and more. It's based on JavaScript and provides easy ways to automate tasks across Google products and third-party services. "
]
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( {
apiKey ,
doCountToken : true ,
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.
// Question 1
const jsonSchema1 = {
title : "Current population of Kyoto, Osaka, Aichi, Fukuoka, Tokyo in Japan" ,
description :
"Return the current population of Kyoto, Osaka, Aichi, Fukuoka, Tokyo in Japan" ,
type : "object" ,
properties : {
propertyNames : {
description : "Prefecture names" ,
} ,
patternProperties : {
"" : { type : "number" , description : "Population" } ,
} ,
} ,
} ;
const res1 = g . generateContent ( { jsonSchema : jsonSchema1 } ) ;
console . log ( res1 ) ;
// Question 2
const jsonSchema2 = {
title : "Current area of them" ,
description : "Return the current area of them." ,
type : "object" ,
properties : {
propertyNames : {
description : "Prefecture names" ,
} ,
patternProperties : {
"" : { type : "number" , description : "Area. Unit is km^2." } ,
} ,
} ,
} ;
const res2 = g . generateContent ( { jsonSchema : jsonSchema2 } ) ;
console . log ( res2 ) ;
}
Dalam hal ini, nilai hasil dapat diperoleh dengan memberikan skema JSON saja. Hasilnya adalah sebagai berikut.
Untuk pertanyaan pertama
{
"Kyoto" : 2579970 ,
"Osaka" : 8837684 ,
"Aichi" : 7552873 ,
"Fukuoka" : 5138217 ,
"Tokyo" : 14047594
}
Untuk pertanyaan ke-2
{
"Kyoto" : 4612.19 ,
"Osaka" : 1904.99 ,
"Aichi" : 5172.92 ,
"Fukuoka" : 4986.51 ,
"Tokyo" : 2194.07
}
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
// Please set file IDs of PDF file of invoices.
const fileIds = [ "###fileID1###" , "###fileID2###" ] ;
const jsonSchema = {
title :
"Array including JSON object parsed the following images of the invoices" ,
description :
"Create an array including JSON object parsed the following images of the invoices." ,
type : "array" ,
items : {
type : "object" ,
properties : {
name : {
description : "Name given as 'Filename'" ,
type : "string" ,
} ,
invoiceTitle : {
description : "Title of invoice" ,
type : "string" ,
} ,
invoiceDate : {
description : "Date of invoice" ,
type : "string" ,
} ,
invoiceNumber : {
description : "Number of the invoice" ,
type : "string" ,
} ,
invoiceDestinationName : {
description : "Name of destination of invoice" ,
type : "string" ,
} ,
invoiceDestinationAddress : {
description : "Address of the destination of invoice" ,
type : "string" ,
} ,
totalCost : {
description : "Total cost of all costs" ,
type : "string" ,
} ,
table : {
description :
"Table of invoice. This is a 2-dimensional array. Add the first header row to the table in the 2-dimensional array. The column should be 'title or description of item', 'number of items', 'unit cost', 'total cost'" ,
type : "array" ,
} ,
} ,
required : [
"name" ,
"invoiceTitle" ,
"invoiceDate" ,
"invoiceNumber" ,
"invoiceDestinationName" ,
"invoiceDestinationAddress" ,
"totalCost" ,
"table" ,
] ,
additionalProperties : false ,
} ,
} ;
const g = GeminiWithFiles . geminiWithFiles ( {
apiKey ,
doCountToken : true ,
response_mime_type : "application/json" ,
} ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, doCountToken: true, response_mime_type: "application/json" }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const fileList = g . setFileIds ( fileIds , true ) . uploadFiles ( ) ;
const res = g
. withUploadedFilesByGenerateContent ( fileList )
. generateContent ( { jsonSchema } ) ;
// g.deleteFiles(fileList.map(({ name }) => name)); // If you want to delete the uploaded files, please use this.
console . log ( JSON . stringify ( res ) ) ;
}
Ketika skrip ini dijalankan ke faktur yang sama di bagian "Unggah faktur data PDF dan parsing", hasil yang sama diperoleh.
Jika Anda ingin mengembalikan nilai skema JSON dengan kompleksitas tinggi, response_mime_type
mungkin cocok.
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const systemInstruction = { parts : [ { text : "You are a cat. Your name is Neko." } ] } ;
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , systemInstruction , response_mime_type : "application/json" } ) ; // This is for installing GeminiWithFiles as a library.
// const g = new GeminiWithFiles({ apiKey, systemInstruction, response_mime_type: "application/json" }); // 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 ) ;
}
Saat skrip ini dijalankan, [ 'Meow? What is Google Apps Script? Is it something I can chase? ?' ]
dikembalikan. Anda dapat melihat nilai systemInstruction
tercermin dalam konten yang dihasilkan.
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const fileIds = [ "###" ] ; // Please set your movie file (MP4).
const g = GeminiWithFiles . geminiWithFiles ( { apiKey } ) ;
const fileList = g . setFileIds ( fileIds ) . uploadFiles ( ) ;
const res = g . withUploadedFilesByGenerateContent ( fileList ) . generateContent ( { q : "Describe this video." } ) ;
console . log ( res ) ;
}
Saat skrip ini dijalankan, file video MP4 diunggah ke Gemini dan menghasilkan konten dengan file video yang diunggah.
Poin penting, pada tahap saat ini, ukuran unggahan maksimum dengan UrlFetchApp dari Google Apps Script adalah 50 MB. Ref Jadi, ketika Anda mengupload file video, harap gunakan file yang berukuran kurang dari 50 MB. Harap berhati-hati tentang hal ini.
Mulai v1.0.7, ketika doCountToken: true
dan exportTotalTokens: true
digunakan dalam objek argumen geminiWithFiles
, total token dikembalikan. Dalam hal ini, nilai yang dikembalikan adalah objek seperti {returnValue: "###", totalTokens: ###}
. Contoh skripnya adalah sebagai berikut.
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , exportTotalTokens : true } ) ;
const res = g . generateContent ( { q : "What is Gemini?" } ) ;
console . log ( res ) ;
}
Saat skrip ini dijalankan, hasil berikut akan dikembalikan.
{
"returnValue" : " " Gemini " can refer to several things, so please provide me with more context. For example, are you asking about: nn * **Gemini (constellation):** A constellation in the Northern Hemisphere, known for its distinctive twin stars, Castor and Pollux. n * **Gemini (astrological sign):** The third sign of the Zodiac, associated with those born between May 21st and June 20th. n * **Gemini (programming language):** A procedural programming language created by Niklaus Wirth, known for its simplicity and emphasis on structured programming. n * **Gemini (Google AI model):** A large language model developed by Google, known for its advanced conversational abilities and ability to generate different creative text formats. n * **Gemini (NASA mission):** A crewed spaceflight mission to the Moon, planned for 2024. nn Once you tell me what kind of Gemini you're interested in, I can give you a more specific answer! " ,
"usageMetadata" :{
"promptTokenCount" : 5 ,
"candidatesTokenCount" : 200 ,
"totalTokenCount" : 205
}
}
Dari v2.xx, hal ini dapat dicapai. Ini dari Ref dan Ref.
Contoh skrip dapat dilihat di sini.
Perintah ini berasal dari dokumen resmi ini.
Untuk menggunakan codeExecution, silakan gunakan tools: [{ codeExecution: {} }]
dan exportRawData: true
ke dalam metode geminiWithFiles
sebagai berikut.
function myFunction ( ) {
const apiKey = "###" ; // Please set your API key.
const g = GeminiWithFiles . geminiWithFiles ( { apiKey , tools : [ { codeExecution : { } } ] , exportRawData : true } ) ;
// const g = new GeminiWithFiles({ apiKey, tools: [{ codeExecution: {} }], exportRawData: true }); // This is for directly copying and pasting Class GeminiWithFiles into your Google Apps Script project.
const res = g . generateContent ( { q : 'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.' } ) ;
console . log ( res . candidates [ 0 ] . content . parts ) ;
}
Ketika skrip ini dijalankan, diperoleh hasil sebagai berikut.
[
{
"text" : " I will generate Python code to calculate the sum of the first 50 prime numbers. nn "
},
{
"executableCode" :{
"language" : " PYTHON " ,
"code" : " n def is_prime(num): n """n Checks if a number is prime. n """n if num <= 1: n return False n for i in range(2, int(num**0.5) + 1): n if num % i == 0: n return False n return True nn primes = [] n n = 2 n while len(primes) < 50: n if is_prime(n): n primes.append(n) n n += 1 nn print(f \ 'The first 50 prime numbers are: {primes} \ ') n print(f \ 'The sum of the first 50 prime numbers is: {sum(primes)} \ ') n "
}
},
{
"codeExecutionResult" :{
"outcome" : " OUTCOME_OK " ,
"output" : " The first 50 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229] n The sum of the first 50 prime numbers is: 5117 n "
}
},
{
"text" : " The code first defines a function `is_prime(num)` to check if a number is prime. The function iterates through all numbers from 2 to the square root of the given number. If any of these numbers divide the given number, then the number is not prime. Otherwise, the number is prime. nn Then, the code initializes an empty list called `primes` to store the prime numbers. It also initializes a variable `n` to 2, which is the first prime number. nn The code then enters a `while` loop that continues until 50 prime numbers are found. Inside the loop, the code checks if the current number `n` is prime using the `is_prime` function. If it is, the number is appended to the `primes` list. nn After the loop, the code prints the list of prime numbers and the sum of the prime numbers. nn The output shows that the sum of the first 50 prime numbers is 5117. "
}
]
image/png,image/jpeg,image/webp,image/heic,image/heif
audio/wav,audio/mp3,audio/aiff,audio/aac,audio/ogg,audio/flac
Exceeded maximum execution time
. Harap berhati-hati tentang hal ini.Saya telah mengajukan permintaan berikut di masa mendatang ke pelacak masalah Google. Ref
Saya rasa akan lebih bermanfaat lagi bagi pengguna Gemini jika file di Google Drive bisa langsung digunakan oleh Gemini API hanya dengan menggunakan ID filenya saja. Hal ini juga akan mengurangi biaya pengunggahan data secara signifikan.
Menurut saya kemampuan untuk menyertakan metadata khusus dengan file yang diunggah akan sangat berguna untuk mengelola file dalam jumlah besar.
Saat saya menguji fungsi yang memanggil untuk mengontrol format keluaran, terkadang saya mendapatkan kesalahan kode status 500. Namun, ketika saya menguji response_mime_type
, kesalahan seperti itu jarang terjadi. Saya tidak yakin apakah ini spesifikasi saat ini.
Gambar abstrak teratas dibuat oleh Gemini dari bagian "Deskripsi".
MIT
Tanaike
Menyumbangkan
v1.0.0 (26 April 2024)
v1.0.1 (2 Mei 2024)
response_mime_type
harus dapat digunakan untuk mengontrol format keluaran. Refv1.0.2 (7 Mei 2024)
parts
telah ditambahkan. Dari versi ini, Anda dapat memilih salah satu dari q
, jsonSchema
, dan parts
.systemInstruction
dapat digunakan.toolConfig
telah ditambahkan ke badan permintaan.v1.0.3 (17 Mei 2024)
v1.0.4 (29 Mei 2024)
model.countToken
digunakan dengan file yang diunggah, saya mengonfirmasi bahwa kesalahan seperti You do not have permission to access the File ### or it may not exist.
muncul. Untuk menangani masalah ini, saya memodifikasi perpustakaan.v1.0.5 (7 Juni 2024)
v1.0.6 (15 Juni 2024)
v1.0.7 (4 Juli 2024)
doCountToken: true
dan exportTotalTokens: true
digunakan dalam objek argumen geminiWithFiles
, total token dikembalikan. Dalam hal ini, nilai yang dikembalikan adalah objek seperti {returnValue: "###", totalTokens: ###}
.v2.0.0 (3 Agustus 2024)
functions: {}
digunakan. Jadi, pemanggilan fungsi default telah dihapus. Karena pada tahap saat ini, keluaran JSON dapat dengan mudah dikembalikan menggunakan skema JSON dan response_mime_type
. Referensi Referensimodels/gemini-1.5-pro-latest
menjadi models/gemini-1.5-flash-latest
.exportTotalTokens
diubah. Setelah v2.xx, jika ini benar, objek usageMetadata
termasuk promptTokenCount
, candidatesTokenCount
, totalTokenCount
diekspor. Pada saat itu, konten yang dihasilkan dan usageMetadata
dikembalikan sebagai objek.v2.0.1 (4 Agustus 2024)
codeExecution
dapat digunakan. Refv2.0.2 (26 September 2024)
generationConfig
, properti response_schema
dan temperature
ditambahkan.v2.0.3 (19 November 2024)
setFileIdsOrUrlsWithResumableUpload
. Mulai v2.0.3, saat Anda menggunakan metode ini, harap sertakan propertiesService: PropertiesService.getScriptProperties()
ke dalam objek awal sebagai berikut. Karena, ketika PropertiesService.getScriptProperties()
digunakan di perpustakaan, nilainya dimasukkan ke dalam perpustakaan. Saat saya membuat Ref dan Ref, saya mengira skrip tersebut digunakan dengan cara copy dan paste, bukan perpustakaan. Jadi, saya menyertakan PropertiesService.getScriptProperties()
dalam skrip. Tapi saya perhatikan bahwa ketika ini digunakan dengan GeminiWithFiles, setiap pengguna diharuskan menggunakan PropertiesService.getScriptProperties()
. Jadi, saya memodifikasi ini.resumableUploadAsNewUpload: true
. Ref Dengan ini, properti dibersihkan dan pengunggahan dijalankan.ATAS