-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Adding content cache samples #4147
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
Merged
gericdong
merged 24 commits into
GoogleCloudPlatform:main
from
Guiners:sample/content_cache
Sep 11, 2025
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ad2afbd
linter and all modules from content cache
52fde9f
content-cache code with a tests
a2fccb5
content-cache code with tests
c01d51e
bumping version and fixing tests
09d739b
bumping version and fixing tests
16d1cc0
Merge branch 'main' into sample/content_cache
msampathkumar 7db1efc
Merge branch 'main' into sample/content_cache
Guiners d2adbeb
adding samples, test, lints
074fb57
Merge branch 'main' into sample/content_cache
Guiners cb6d443
Merge branch 'main' into sample/content_cache
gericdong cb1a968
Merge branch 'main' into sample/content_cache
Guiners ace80bf
adding samples, test, lints
9a31704
adding samples, test, lints
671fab3
Merge branch 'main' into sample/content_cache
Guiners f2de576
Merge branch 'main' into sample/content_cache
Guiners 1464d23
adding samples, test, lints
63dc8fe
Merge branch 'main' into sample/content_cache
msampathkumar 73153f6
adding samples, test, lints
edfbd5a
adding samples, test, lints
3944dac
Merge branch 'main' into sample/content_cache
gericdong 8c4d191
adding samples, test, lints
28f2cfd
Merge remote-tracking branch 'origin/sample/content_cache' into sampl…
2260479
fixing functions names
bfa0216
Merge branch 'main' into sample/content_cache
gericdong 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
87 changes: 87 additions & 0 deletions
87
genai/content-cache/content-cache-create-with-txt-gcs-pdf.js
This file contains hidden or 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,87 @@ | ||
| // Copyright 2025 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf] | ||
|
|
||
| const {GoogleGenAI} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
| async function generateContent( | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| httpOptions: { | ||
| apiVersion: 'v1', | ||
| }, | ||
| }); | ||
|
|
||
| const systemInstruction = ` | ||
| You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts. | ||
| Now look at these research papers, and answer the following questions. | ||
| `; | ||
|
|
||
| const contents = [ | ||
| { | ||
| role: 'user', | ||
| parts: [ | ||
| { | ||
| fileData: { | ||
| fileUri: | ||
| 'gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf', | ||
| mimeType: 'application/pdf', | ||
| }, | ||
| }, | ||
| { | ||
| fileData: { | ||
| fileUri: 'gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf', | ||
| mimeType: 'application/pdf', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| const contentCache = await client.caches.create({ | ||
| model: 'gemini-2.5-flash', | ||
| config: { | ||
| contents: contents, | ||
| systemInstruction: systemInstruction, | ||
| displayName: 'example-cache', | ||
| ttl: '86400s', | ||
| }, | ||
| }); | ||
|
|
||
| console.log(contentCache); | ||
| console.log(contentCache.name); | ||
|
|
||
| // Example response: | ||
| // projects/111111111111/locations/us-central1/cachedContents/1111111111111111111 | ||
| // CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, | ||
| // text_count=153, total_token_count=43130, video_duration_seconds=None) | ||
|
|
||
| return contentCache.name; | ||
| } | ||
|
|
||
| // [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf] | ||
|
|
||
| module.exports = { | ||
| generateContent, | ||
| }; |
This file contains hidden or 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,52 @@ | ||
| // Copyright 2025 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_contentcache_delete] | ||
| const {GoogleGenAI} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
|
|
||
| async function generateContent( | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION, | ||
| cacheName = 'example-cache' | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| httpOptions: { | ||
| apiVersion: 'v1', | ||
| }, | ||
| }); | ||
|
|
||
| console.log('Removing cache'); | ||
| const contentCache = await client.caches.delete({ | ||
| name: cacheName, | ||
| }); | ||
|
|
||
| console.log(contentCache.text); | ||
|
|
||
| return contentCache; | ||
| } | ||
| // Example response | ||
| // Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111 | ||
| // [END googlegenaisdk_contentcache_delete] | ||
|
|
||
| module.exports = { | ||
| generateContent, | ||
| }; | ||
This file contains hidden or 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,64 @@ | ||
| // Copyright 2025 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_contentcache_list] | ||
|
|
||
| const {GoogleGenAI} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
| async function generateContent( | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| httpOptions: { | ||
| apiVersion: 'v1', | ||
| }, | ||
| }); | ||
|
|
||
| const contentCacheList = await client.caches.list(); | ||
|
|
||
| // Access individual properties of a ContentCache object(s) | ||
| const contentCacheNames = []; | ||
| for (const contentCache of contentCacheList.pageInternal) { | ||
| console.log( | ||
| `Cache \`${contentCache.name}\` for model \`${contentCache.model}\`` | ||
| ); | ||
| console.log(`Last updated at: ${contentCache.updateTime}`); | ||
| console.log(`Expires at: ${contentCache.expireTime}`); | ||
| contentCacheNames.push(contentCache.name); | ||
| } | ||
| console.log(contentCacheNames); | ||
|
|
||
| // Example response: | ||
| // * Cache `projects/111111111111/locations/us-central1/cachedContents/1111111111111111111` for | ||
| // model `projects/111111111111/locations/us-central1/publishers/google/models/gemini-XXX-pro-XXX` | ||
| // * Last updated at: 2025-02-13 14:46:42.620490+00:00 | ||
| // * CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, text_count=153, total_token_count=43130, video_duration_seconds=None) | ||
| // ... | ||
|
|
||
| return contentCacheNames; | ||
| } | ||
|
|
||
| // [END googlegenaisdk_contentcache_list] | ||
|
|
||
| module.exports = { | ||
| generateContent, | ||
| }; | ||
This file contains hidden or 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,77 @@ | ||
| // Copyright 2025 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_contentcache_update] | ||
| const {GoogleGenAI} = require('@google/genai'); | ||
| const {DateTime} = require('luxon'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
|
|
||
| async function generateContent( | ||
Guiners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION, | ||
| cacheName = 'example-cache' | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| httpOptions: { | ||
| apiVersion: 'v1', | ||
| }, | ||
| }); | ||
|
|
||
| let contentCache = await client.caches.get({ | ||
| name: cacheName, | ||
| }); | ||
|
|
||
| console.log('Expire time', contentCache.expireTime); | ||
|
|
||
| contentCache = await client.caches.update({ | ||
| name: cacheName, | ||
| config: { | ||
| ttl: '36000s', | ||
| }, | ||
| }); | ||
|
|
||
| const expireTime = DateTime.fromISO(contentCache.expireTime, {zone: 'utc'}); | ||
| const now = DateTime.utc(); | ||
| const timeDiff = expireTime.diff(now, ['seconds']); | ||
|
|
||
| console.log('Expire time (after update):', expireTime.toISO()); | ||
| console.log('Expire time (in seconds):', Math.floor(timeDiff.seconds)); | ||
|
|
||
| const nextWeekUtc = DateTime.utc().plus({days: 7}); | ||
| console.log('Next week (UTC):', nextWeekUtc.toISO()); | ||
|
|
||
| contentCache = await client.caches.update({ | ||
| name: cacheName, | ||
| config: { | ||
| expireTime: nextWeekUtc, | ||
| }, | ||
| }); | ||
|
|
||
| console.log('Expire time (after update):', contentCache.expireTime); | ||
| return contentCache; | ||
| } | ||
| // Example response | ||
| // Expire time(after update): 2025-02-20 15:51:42.614968+00:00 | ||
| // [END googlegenaisdk_contentcache_update] | ||
|
|
||
| module.exports = { | ||
| generateContent, | ||
| }; | ||
This file contains hidden or 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,57 @@ | ||
| // Copyright 2025 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_contentcache_use_with_txt] | ||
|
|
||
| const {GoogleGenAI} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; | ||
|
|
||
| async function generateContent( | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION, | ||
| cacheName = 'example-cache' | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| httpOptions: { | ||
| apiVersion: 'v1', | ||
| }, | ||
| }); | ||
|
|
||
| const response = await client.models.generateContent({ | ||
| model: 'gemini-2.5-flash', | ||
| contents: 'Summarize the pdfs', | ||
| config: { | ||
| cachedContent: cacheName, | ||
| }, | ||
| }); | ||
|
|
||
| console.log(response.text); | ||
|
|
||
| return response.text; | ||
| } | ||
| // Example response | ||
| // The Gemini family of multimodal models from Google DeepMind demonstrates remarkable capabilities across various | ||
| // modalities, including image, audio, video, and text.... | ||
| // [END googlegenaisdk_contentcache_use_with_txt] | ||
|
|
||
| module.exports = { | ||
| generateContent, | ||
| }; |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.