Skip to content

Commit 52fde9f

Browse files
author
Guiners
committed
content-cache code with a tests
1 parent ad2afbd commit 52fde9f

7 files changed

+124
-64
lines changed

genai/content_cache/contentcache_create_with_txt_gcs_pdf.js renamed to genai/content-cache/content-cache-create-with-txt-gcs-pdf.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,60 @@ const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
2323
async function generateContent(
2424
projectId = GOOGLE_CLOUD_PROJECT,
2525
location = GOOGLE_CLOUD_LOCATION
26-
){
27-
28-
26+
) {
2927
const ai = new GoogleGenAI({
3028
vertexai: true,
3129
project: projectId,
3230
location: location,
3331
httpOptions: {
34-
apiVersion: 'v1'
35-
}
32+
apiVersion: 'v1',
33+
},
3634
});
3735

3836
const systemInstruction = `
3937
You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts.
4038
Now look at these research papers, and answer the following questions.
4139
`;
4240

43-
4441
const contents = [
4542
{
46-
role: "user",
43+
role: 'user',
4744
parts: [
4845
{
4946
fileData: {
50-
fileUri: "gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf",
51-
mimeType: "application/pdf",
52-
}
47+
fileUri:
48+
'gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf',
49+
mimeType: 'application/pdf',
50+
},
5351
},
5452
{
5553
fileData: {
56-
fileUri: "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf",
57-
mimeType: "application/pdf",
58-
}
59-
}
60-
]
61-
}
54+
fileUri: 'gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf',
55+
mimeType: 'application/pdf',
56+
},
57+
},
58+
],
59+
},
6260
];
6361

64-
65-
const content_cache = await ai.caches.create({
62+
const contentCache = await ai.caches.create({
6663
model: 'gemini-2.5-flash',
6764
config: {
6865
contents: contents,
6966
systemInstruction: systemInstruction,
7067
displayName: 'example-cache',
71-
ttl: '86400s'
68+
ttl: '86400s',
7269
},
7370
});
7471

72+
console.log(contentCache);
73+
console.log(contentCache.name);
7574

76-
console.log(content_cache);
77-
console.log(content_cache.name);
78-
79-
return content_cache.name;
80-
75+
return contentCache.name;
8176
}
8277

8378
// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
8479

8580
module.exports = {
8681
generateContent,
87-
};
82+
};

genai/content_cache/contentcache_delete.js renamed to genai/content-cache/content-cache-delete.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const {GoogleGenAI} = require('@google/genai');
2020
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
2121
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
2222

23-
const CACHE_NAME = 'projects/448220130128/locations/us-central1/cachedContents/4839555542676406272'
23+
const CACHE_NAME =
24+
'projects/448220130128/locations/us-central1/cachedContents/4839555542676406272';
2425

2526
async function generateContent(
2627
projectId = GOOGLE_CLOUD_PROJECT,
@@ -32,22 +33,21 @@ async function generateContent(
3233
project: projectId,
3334
location: location,
3435
httpOptions: {
35-
apiVersion: 'v1'
36-
}
36+
apiVersion: 'v1',
37+
},
3738
});
3839

39-
console.log('Removing cache')
40+
console.log('Removing cache');
4041
const contentCache = await ai.caches.delete({
41-
name: cacheName
42+
name: cacheName,
4243
});
4344

4445
console.log(contentCache.text);
4546

4647
return contentCache;
47-
4848
}
4949
// [END googlegenaisdk_contentcache_delete]
5050

5151
module.exports = {
5252
generateContent,
53-
};
53+
};
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ async function generateContent(
2929
project: projectId,
3030
location: location,
3131
httpOptions: {
32-
apiVersion: 'v1'
33-
}
32+
apiVersion: 'v1',
33+
},
3434
});
3535

36-
const contentCacheList = await ai.caches.list()
37-
36+
const contentCacheList = await ai.caches.list();
3837

3938
// Access individual properties of a ContentCache object(s)
40-
const contentCacheNames = []
39+
const contentCacheNames = [];
4140
for (const contentCache of contentCacheList.pageInternal) {
42-
console.log(`Cache \`${contentCache.name}\` for model \`${contentCache.model}\``);
41+
console.log(
42+
`Cache \`${contentCache.name}\` for model \`${contentCache.model}\``
43+
);
4344
console.log(`Last updated at: ${contentCache.updateTime}`);
4445
console.log(`Expires at: ${contentCache.expireTime}`);
4546
contentCacheNames.push(contentCache.name);
4647
}
47-
console.log()
48+
console.log();
4849

4950
return contentCacheNames;
50-
5151
}
5252
// [END googlegenaisdk_contentcache_list]
5353

5454
module.exports = {
5555
generateContent,
56-
};
56+
};

genai/content_cache/contentcache_update.js renamed to genai/content-cache/content-cache-update.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,63 @@
1616

1717
// [START googlegenaisdk_contentcache_update]
1818
const {GoogleGenAI} = require('@google/genai');
19-
const { DateTime } = require('luxon');
20-
19+
const {DateTime} = require('luxon');
2120

2221
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
2322
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
2423

25-
const CACHE_NAME = 'projects/448220130128/locations/us-central1/cachedContents/4839555542676406272'
24+
const CACHE_NAME =
25+
'projects/448220130128/locations/us-central1/cachedContents/4839555542676406272';
2626

2727
async function generateContent(
2828
projectId = GOOGLE_CLOUD_PROJECT,
2929
location = GOOGLE_CLOUD_LOCATION,
3030
cacheName = CACHE_NAME
3131
) {
32-
3332
const ai = new GoogleGenAI({
3433
vertexai: true,
3534
project: projectId,
3635
location: location,
3736
httpOptions: {
38-
apiVersion: 'v1'
39-
}
37+
apiVersion: 'v1',
38+
},
4039
});
4140

4241
let contentCache = await ai.caches.get({
43-
name: cacheName
42+
name: cacheName,
4443
});
4544

4645
console.log('Expire time', contentCache.expireTime);
4746

4847
contentCache = await ai.caches.update({
4948
name: cacheName,
5049
config: {
51-
ttl: '36000s'
52-
}
50+
ttl: '36000s',
51+
},
5352
});
5453

55-
const expireTime = DateTime.fromISO(contentCache.expireTime, { zone: 'utc' });
54+
const expireTime = DateTime.fromISO(contentCache.expireTime, {zone: 'utc'});
5655
const now = DateTime.utc();
5756
const timeDiff = expireTime.diff(now, ['seconds']);
5857

5958
console.log('Expire time (after update):', expireTime.toISO());
6059
console.log('Expire time (in seconds):', Math.floor(timeDiff.seconds));
6160

62-
const nextWeekUtc = DateTime.utc().plus({ days: 7 });
61+
const nextWeekUtc = DateTime.utc().plus({days: 7});
6362
console.log('Next week (UTC):', nextWeekUtc.toISO());
6463

6564
contentCache = await ai.caches.update({
6665
name: cacheName,
6766
config: {
68-
expireTime: nextWeekUtc
69-
}
67+
expireTime: nextWeekUtc,
68+
},
7069
});
7170

7271
console.log('Expire time (after update):', contentCache.expireTime);
7372
return contentCache;
74-
7573
}
7674
// [END googlegenaisdk_contentcache_update]
7775

7876
module.exports = {
7977
generateContent,
80-
};
78+
};

genai/content_cache/contentcache_use_with_txt.js renamed to genai/content-cache/content-cache-use-with-txt.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,37 @@ const {GoogleGenAI} = require('@google/genai');
2121
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
2222
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
2323

24-
const CACHE_NAME = 'projects/448220130128/locations/us-central1/cachedContents/8186785596975349760'
24+
const CACHE_NAME =
25+
'projects/448220130128/locations/us-central1/cachedContents/8186785596975349760';
2526

2627
async function generateContent(
2728
projectId = GOOGLE_CLOUD_PROJECT,
2829
location = GOOGLE_CLOUD_LOCATION,
2930
cacheName = CACHE_NAME
30-
){
31-
31+
) {
3232
const ai = new GoogleGenAI({
3333
vertexai: true,
3434
project: projectId,
3535
location: location,
3636
httpOptions: {
37-
apiVersion: 'v1'
38-
}
37+
apiVersion: 'v1',
38+
},
3939
});
4040

4141
const response = await ai.models.generateContent({
4242
model: 'gemini-2.5-flash',
4343
contents: 'Summarize the pdfs',
4444
config: {
45-
cachedContent: cacheName
45+
cachedContent: cacheName,
4646
},
4747
});
4848

4949
console.log(response.text);
5050

5151
return response.text;
52-
5352
}
5453
// [END googlegenaisdk_contentcache_use_with_txt]
5554

5655
module.exports = {
5756
generateContent,
58-
};
57+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const {assert} = require('chai');
4+
const {describe, it} = require('mocha');
5+
6+
const projectId = process.env.CAIP_PROJECT_ID;
7+
8+
const createSample = require('../content-cache/content-cache-create-with-txt-gcs-pdf.js');
9+
const useSample = require('../content-cache/content-cache-use-with-txt.js');
10+
const updateSample = require('../content-cache/content-cache-update.js');
11+
const deleteSample = require('../content-cache/content-cache-delete.js');
12+
13+
describe('content-cache-create-use-update-delete', function () {
14+
this.timeout(60000);
15+
let contentCacheName;
16+
17+
it('should create content cache', async () => {
18+
contentCacheName = await createSample.generateContent(projectId);
19+
assert.isString(contentCacheName);
20+
assert.isAbove(contentCacheName.length, 0);
21+
});
22+
23+
it('should update content cache', async () => {
24+
await updateSample.generateContent(projectId, undefined, contentCacheName);
25+
});
26+
27+
it('should use content cache', async () => {
28+
const response = await useSample.generateContent(
29+
projectId,
30+
undefined,
31+
contentCacheName
32+
);
33+
assert.isString(response);
34+
});
35+
36+
it('should delete content cache', async () => {
37+
await deleteSample.generateContent(projectId, undefined, contentCacheName);
38+
});
39+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../content-cache/content-cache-list.js');
22+
23+
describe('contentcache-list', async () => {
24+
it('should return object with names of catches', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert.isArray(output);
27+
assert.isAbove(output.length, 0);
28+
});
29+
});

0 commit comments

Comments
 (0)