-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add canonical File API samples #204
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { GoogleGenerativeAI } from "@google/generative-ai"; | ||
import { GoogleAIFileManager, FileState } from "@google/generative-ai/server"; | ||
import { dirname } from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const mediaPath = __dirname + "/media"; | ||
|
||
async function filesCreateImage() { | ||
// [START files_create_image] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const uploadResult = await fileManager.uploadFile( | ||
`${mediaPath}/jetpack.jpg`, | ||
{ | ||
mimeType: "image/jpeg", | ||
displayName: "Jetpack drawing", | ||
}, | ||
); | ||
// View the response. | ||
console.log( | ||
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`, | ||
); | ||
|
||
const genAI = new GoogleGenerativeAI(process.env.API_KEY); | ||
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); | ||
const result = await model.generateContent([ | ||
"Tell me about this image.", | ||
{ | ||
fileData: { | ||
fileUri: uploadResult.file.uri, | ||
mimeType: uploadResult.file.mimeType, | ||
}, | ||
}, | ||
]); | ||
console.log(result.response.text()); | ||
// [END files_create_image] | ||
} | ||
|
||
async function filesCreateAudio() { | ||
// [START files_create_audio] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const uploadResult = await fileManager.uploadFile( | ||
`${mediaPath}/samplesmall.mp3`, | ||
{ | ||
mimeType: "audio/mp3", | ||
displayName: "Audio sample", | ||
}, | ||
); | ||
|
||
let file = await fileManager.getFile(uploadResult.file.name); | ||
while (file.state === FileState.PROCESSING) { | ||
process.stdout.write("."); | ||
// Sleep for 10 seconds | ||
await new Promise((resolve) => setTimeout(resolve, 10_000)); | ||
// Fetch the file from the API again | ||
file = await fileManager.getFile(uploadResult.file.name); | ||
} | ||
|
||
if (file.state === FileState.FAILED) { | ||
throw new Error("Audio processing failed."); | ||
} | ||
|
||
// View the response. | ||
console.log( | ||
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`, | ||
); | ||
|
||
const genAI = new GoogleGenerativeAI(process.env.API_KEY); | ||
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); | ||
const result = await model.generateContent([ | ||
"Tell me about this audio clip.", | ||
{ | ||
fileData: { | ||
fileUri: uploadResult.file.uri, | ||
mimeType: uploadResult.file.mimeType, | ||
}, | ||
}, | ||
]); | ||
console.log(result.response.text()); | ||
// [END files_create_audio] | ||
} | ||
|
||
async function filesCreateText() { | ||
// [START files_create_text] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const uploadResult = await fileManager.uploadFile(`${mediaPath}/a11.txt`, { | ||
mimeType: "text/plain", | ||
displayName: "Apollo 11", | ||
}); | ||
// View the response. | ||
console.log( | ||
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`, | ||
); | ||
|
||
const genAI = new GoogleGenerativeAI(process.env.API_KEY); | ||
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); | ||
const result = await model.generateContent([ | ||
"Transcribe the first few sentences of this document.", | ||
{ | ||
fileData: { | ||
fileUri: uploadResult.file.uri, | ||
mimeType: uploadResult.file.mimeType, | ||
}, | ||
}, | ||
]); | ||
console.log(result.response.text()); | ||
// [END files_create_text] | ||
} | ||
|
||
async function filesCreateVideo() { | ||
// [START files_create_video] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const uploadResult = await fileManager.uploadFile( | ||
`${mediaPath}/Big_Buck_Bunny.mp4`, | ||
{ | ||
mimeType: "video/mp4", | ||
displayName: "Big Buck Bunny", | ||
}, | ||
); | ||
|
||
let file = await fileManager.getFile(uploadResult.file.name); | ||
while (file.state === FileState.PROCESSING) { | ||
process.stdout.write("."); | ||
// Sleep for 10 seconds | ||
await new Promise((resolve) => setTimeout(resolve, 10_000)); | ||
// Fetch the file from the API again | ||
file = await fileManager.getFile(uploadResult.file.name); | ||
} | ||
|
||
if (file.state === FileState.FAILED) { | ||
throw new Error("Video processing failed."); | ||
} | ||
|
||
// View the response. | ||
console.log( | ||
`Uploaded file ${uploadResult.file.displayName} as: ${uploadResult.file.uri}`, | ||
); | ||
|
||
const genAI = new GoogleGenerativeAI(process.env.API_KEY); | ||
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); | ||
const result = await model.generateContent([ | ||
"Tell me about this video.", | ||
{ | ||
fileData: { | ||
fileUri: uploadResult.file.uri, | ||
mimeType: uploadResult.file.mimeType, | ||
}, | ||
}, | ||
]); | ||
console.log(result.response.text()); | ||
// [END files_create_video] | ||
} | ||
|
||
async function filesList() { | ||
// [START files_list] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const listFilesResponse = await fileManager.listFiles(); | ||
|
||
// View the response. | ||
for (const file of listFilesResponse.files) { | ||
console.log(`name: ${file.name} | display name: ${file.displayName}`); | ||
} | ||
// [END files_list] | ||
} | ||
|
||
async function filesGet() { | ||
// [START files_get] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const uploadResponse = await fileManager.uploadFile( | ||
`${mediaPath}/jetpack.jpg`, | ||
{ | ||
mimeType: "image/jpeg", | ||
displayName: "Jetpack drawing", | ||
}, | ||
); | ||
|
||
// Get the previously uploaded file's metadata. | ||
const getResponse = await fileManager.getFile(uploadResponse.file.name); | ||
|
||
// View the response. | ||
console.log( | ||
`Retrieved file ${getResponse.displayName} as ${getResponse.uri}`, | ||
); | ||
// [END files_get] | ||
} | ||
|
||
async function filesDelete() { | ||
// [START files_delete] | ||
const fileManager = new GoogleAIFileManager(process.env.API_KEY); | ||
|
||
const uploadResult = await fileManager.uploadFile( | ||
`${mediaPath}/jetpack.jpg`, | ||
{ | ||
mimeType: "image/jpeg", | ||
displayName: "Jetpack drawing", | ||
}, | ||
); | ||
|
||
// Delete the file. | ||
await fileManager.deleteFile(uploadResult.file.name); | ||
|
||
console.log(`Deleted ${uploadResult.file.displayName}`); | ||
// [END files_delete] | ||
} | ||
|
||
async function runAll() { | ||
// Comment out or delete any sample cases you don't want to run. | ||
// await filesCreateImage(); | ||
// await filesCreateAudio(); | ||
// await filesCreateText(); | ||
// await filesCreateVideo(); | ||
// await filesList(); | ||
// await filesGet(); | ||
// await filesDelete(); | ||
} | ||
|
||
runAll(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to uncomment these
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, thanks again.