Skip to content

Commit 9fdf9b6

Browse files
GuinersGuinersmsampathkumargericdong
authored
feat(genai): Adding content cache samples (#4147)
* linter and all modules from content cache * content-cache code with a tests * content-cache code with tests * bumping version and fixing tests * bumping version and fixing tests * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * adding samples, test, lints * fixing functions names --------- Co-authored-by: Guiners <rkoza@softserveinc.com> Co-authored-by: Sampath Kumar <sampathm@google.com> Co-authored-by: Eric Dong <itseric@google.com>
1 parent 22ab754 commit 9fdf9b6

8 files changed

+431
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
18+
19+
const {GoogleGenAI} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
async function generateContentCache(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
httpOptions: {
32+
apiVersion: 'v1',
33+
},
34+
});
35+
36+
const systemInstruction = `
37+
You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts.
38+
Now look at these research papers, and answer the following questions.
39+
`;
40+
41+
const contents = [
42+
{
43+
role: 'user',
44+
parts: [
45+
{
46+
fileData: {
47+
fileUri:
48+
'gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf',
49+
mimeType: 'application/pdf',
50+
},
51+
},
52+
{
53+
fileData: {
54+
fileUri: 'gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf',
55+
mimeType: 'application/pdf',
56+
},
57+
},
58+
],
59+
},
60+
];
61+
62+
const contentCache = await client.caches.create({
63+
model: 'gemini-2.5-flash',
64+
config: {
65+
contents: contents,
66+
systemInstruction: systemInstruction,
67+
displayName: 'example-cache',
68+
ttl: '86400s',
69+
},
70+
});
71+
72+
console.log(contentCache);
73+
console.log(contentCache.name);
74+
75+
// Example response:
76+
// projects/111111111111/locations/us-central1/cachedContents/1111111111111111111
77+
// CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167,
78+
// text_count=153, total_token_count=43130, video_duration_seconds=None)
79+
80+
return contentCache.name;
81+
}
82+
83+
// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
84+
85+
module.exports = {
86+
generateContentCache,
87+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_contentcache_delete]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function deleteContentCache(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION,
26+
cacheName = 'example-cache'
27+
) {
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
32+
httpOptions: {
33+
apiVersion: 'v1',
34+
},
35+
});
36+
37+
console.log('Removing cache');
38+
const contentCache = await client.caches.delete({
39+
name: cacheName,
40+
});
41+
42+
console.log(contentCache.text);
43+
44+
return contentCache;
45+
}
46+
// Example response
47+
// Deleted Cache projects/111111111111/locations/us-central1/cachedContents/1111111111111111111
48+
// [END googlegenaisdk_contentcache_delete]
49+
50+
module.exports = {
51+
deleteContentCache,
52+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_contentcache_list]
18+
19+
const {GoogleGenAI} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
async function listContentCaches(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
httpOptions: {
32+
apiVersion: 'v1',
33+
},
34+
});
35+
36+
const contentCacheList = await client.caches.list();
37+
38+
// Access individual properties of a ContentCache object(s)
39+
const contentCacheNames = [];
40+
for (const contentCache of contentCacheList.pageInternal) {
41+
console.log(
42+
`Cache \`${contentCache.name}\` for model \`${contentCache.model}\``
43+
);
44+
console.log(`Last updated at: ${contentCache.updateTime}`);
45+
console.log(`Expires at: ${contentCache.expireTime}`);
46+
contentCacheNames.push(contentCache.name);
47+
}
48+
console.log(contentCacheNames);
49+
50+
// Example response:
51+
// * Cache `projects/111111111111/locations/us-central1/cachedContents/1111111111111111111` for
52+
// model `projects/111111111111/locations/us-central1/publishers/google/models/gemini-XXX-pro-XXX`
53+
// * Last updated at: 2025-02-13 14:46:42.620490+00:00
54+
// * CachedContentUsageMetadata(audio_duration_seconds=None, image_count=167, text_count=153, total_token_count=43130, video_duration_seconds=None)
55+
// ...
56+
57+
return contentCacheNames;
58+
}
59+
60+
// [END googlegenaisdk_contentcache_list]
61+
62+
module.exports = {
63+
listContentCaches,
64+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_contentcache_update]
18+
const {GoogleGenAI} = require('@google/genai');
19+
const {DateTime} = require('luxon');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
24+
async function updateContentCache(
25+
projectId = GOOGLE_CLOUD_PROJECT,
26+
location = GOOGLE_CLOUD_LOCATION,
27+
cacheName = 'example-cache'
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
httpOptions: {
34+
apiVersion: 'v1',
35+
},
36+
});
37+
38+
let contentCache = await client.caches.get({
39+
name: cacheName,
40+
});
41+
42+
console.log('Expire time', contentCache.expireTime);
43+
44+
contentCache = await client.caches.update({
45+
name: cacheName,
46+
config: {
47+
ttl: '36000s',
48+
},
49+
});
50+
51+
const expireTime = DateTime.fromISO(contentCache.expireTime, {zone: 'utc'});
52+
const now = DateTime.utc();
53+
const timeDiff = expireTime.diff(now, ['seconds']);
54+
55+
console.log('Expire time (after update):', expireTime.toISO());
56+
console.log('Expire time (in seconds):', Math.floor(timeDiff.seconds));
57+
58+
const nextWeekUtc = DateTime.utc().plus({days: 7});
59+
console.log('Next week (UTC):', nextWeekUtc.toISO());
60+
61+
contentCache = await client.caches.update({
62+
name: cacheName,
63+
config: {
64+
expireTime: nextWeekUtc,
65+
},
66+
});
67+
68+
console.log('Expire time (after update):', contentCache.expireTime);
69+
return contentCache;
70+
}
71+
// Example response
72+
// Expire time(after update): 2025-02-20 15:51:42.614968+00:00
73+
// [END googlegenaisdk_contentcache_update]
74+
75+
module.exports = {
76+
updateContentCache,
77+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_contentcache_use_with_txt]
18+
19+
const {GoogleGenAI} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
23+
24+
async function useContentCache(
25+
projectId = GOOGLE_CLOUD_PROJECT,
26+
location = GOOGLE_CLOUD_LOCATION,
27+
cacheName = 'example-cache'
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
httpOptions: {
34+
apiVersion: 'v1',
35+
},
36+
});
37+
38+
const response = await client.models.generateContent({
39+
model: 'gemini-2.5-flash',
40+
contents: 'Summarize the pdfs',
41+
config: {
42+
cachedContent: cacheName,
43+
},
44+
});
45+
46+
console.log(response.text);
47+
48+
return response.text;
49+
}
50+
// Example response
51+
// The Gemini family of multimodal models from Google DeepMind demonstrates remarkable capabilities across various
52+
// modalities, including image, audio, video, and text....
53+
// [END googlegenaisdk_contentcache_use_with_txt]
54+
55+
module.exports = {
56+
useContentCache,
57+
};

genai/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@google/genai": "1.12.0",
1717
"axios": "^1.6.2",
18+
"luxon": "^3.7.1",
1819
"supertest": "^7.0.0"
1920
},
2021
"devDependencies": {

0 commit comments

Comments
 (0)