diff --git a/genai/content-cache/content-cache-create-with-txt-gcs-pdf.js b/genai/content-cache/content-cache-create-with-txt-gcs-pdf.js new file mode 100644 index 0000000000..34b6778ea1 --- /dev/null +++ b/genai/content-cache/content-cache-create-with-txt-gcs-pdf.js @@ -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 generateContentCache( + 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 = { + generateContentCache, +}; diff --git a/genai/content-cache/content-cache-delete.js b/genai/content-cache/content-cache-delete.js new file mode 100644 index 0000000000..00528dd700 --- /dev/null +++ b/genai/content-cache/content-cache-delete.js @@ -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 deleteContentCache( + 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 = { + deleteContentCache, +}; diff --git a/genai/content-cache/content-cache-list.js b/genai/content-cache/content-cache-list.js new file mode 100644 index 0000000000..eae9cbfbbe --- /dev/null +++ b/genai/content-cache/content-cache-list.js @@ -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 listContentCaches( + 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 = { + listContentCaches, +}; diff --git a/genai/content-cache/content-cache-update.js b/genai/content-cache/content-cache-update.js new file mode 100644 index 0000000000..c7e5764694 --- /dev/null +++ b/genai/content-cache/content-cache-update.js @@ -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 updateContentCache( + 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 = { + updateContentCache, +}; diff --git a/genai/content-cache/content-cache-use-with-txt.js b/genai/content-cache/content-cache-use-with-txt.js new file mode 100644 index 0000000000..0572dbc2a1 --- /dev/null +++ b/genai/content-cache/content-cache-use-with-txt.js @@ -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 useContentCache( + 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 = { + useContentCache, +}; diff --git a/genai/package.json b/genai/package.json index 1f23438fc3..8713d4595e 100644 --- a/genai/package.json +++ b/genai/package.json @@ -15,6 +15,7 @@ "dependencies": { "@google/genai": "1.12.0", "axios": "^1.6.2", + "luxon": "^3.7.1", "supertest": "^7.0.0" }, "devDependencies": { diff --git a/genai/test/content-cache-create-use-update-delete.test.js b/genai/test/content-cache-create-use-update-delete.test.js new file mode 100644 index 0000000000..f12e7bd73e --- /dev/null +++ b/genai/test/content-cache-create-use-update-delete.test.js @@ -0,0 +1,65 @@ +// 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'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; + +const createSample = require('../content-cache/content-cache-create-with-txt-gcs-pdf.js'); +const useSample = require('../content-cache/content-cache-use-with-txt.js'); +const updateSample = require('../content-cache/content-cache-update.js'); +const deleteSample = require('../content-cache/content-cache-delete.js'); +const {delay} = require('./util'); + +describe('content-cache-create-use-update-delete', async function () { + this.timeout(600000); + this.retries(5); + await delay(this.test); + + let contentCacheName; + + it('should create content cache', async () => { + contentCacheName = await createSample.generateContentCache(projectId); + assert.isString(contentCacheName); + assert.isAbove(contentCacheName.length, 0); + }); + + it('should update content cache', async () => { + await updateSample.updateContentCache( + projectId, + undefined, + contentCacheName + ); + }); + + it('should use content cache', async () => { + const response = await useSample.useContentCache( + projectId, + undefined, + contentCacheName + ); + assert.isString(response); + }); + + it('should delete content cache', async () => { + await deleteSample.deleteContentCache( + projectId, + undefined, + contentCacheName + ); + }); +}); diff --git a/genai/test/content-cache-list.test.js b/genai/test/content-cache-list.test.js new file mode 100644 index 0000000000..079580431b --- /dev/null +++ b/genai/test/content-cache-list.test.js @@ -0,0 +1,28 @@ +// 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'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const projectId = process.env.CAIP_PROJECT_ID; +const sample = require('../content-cache/content-cache-list.js'); + +describe('contentcache-list', async () => { + it('should return object with names of catches', async () => { + const output = await sample.listContentCaches(projectId); + assert.isArray(output); + }); +});