Skip to content

Commit 75305dd

Browse files
jdomingrJuan Dominguez
andauthored
feat: add new GenAI SDK content cache samples (#10141)
* feat: add new GenAI SDK content cache samples * change some comments to keep consistency * fix region tag and add some comments * fix controlled generation test case --------- Co-authored-by: Juan Dominguez <[email protected]>
1 parent 04386c1 commit 75305dd

File tree

7 files changed

+442
-1
lines changed

7 files changed

+442
-1
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.contentcache;
18+
19+
// [START googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.CachedContent;
23+
import com.google.genai.types.Content;
24+
import com.google.genai.types.CreateCachedContentConfig;
25+
import com.google.genai.types.HttpOptions;
26+
import com.google.genai.types.Part;
27+
import java.time.Duration;
28+
import java.util.Optional;
29+
30+
public class ContentCacheCreateWithTextGcsPdf {
31+
32+
public static void main(String[] args) {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String modelId = "gemini-2.5-flash";
35+
contentCacheCreateWithTextGcsPdf(modelId);
36+
}
37+
38+
// Creates a cached content using text and gcs pdfs files
39+
public static Optional<String> contentCacheCreateWithTextGcsPdf(String modelId) {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
try (Client client =
43+
Client.builder()
44+
.location("global")
45+
.vertexAI(true)
46+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
47+
.build()) {
48+
49+
// Set the system instruction
50+
Content systemInstruction =
51+
Content.fromParts(
52+
Part.fromText(
53+
"You are an expert researcher. You always stick to the facts"
54+
+ " in the sources provided, and never make up new facts.\n"
55+
+ "Now look at these research papers, and answer the following questions."));
56+
57+
// Set pdf files
58+
Content contents =
59+
Content.fromParts(
60+
Part.fromUri(
61+
"gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf", "application/pdf"),
62+
Part.fromUri(
63+
"gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", "application/pdf"));
64+
65+
// Configuration for cached content using pdfs files and text
66+
CreateCachedContentConfig config =
67+
CreateCachedContentConfig.builder()
68+
.systemInstruction(systemInstruction)
69+
.contents(contents)
70+
.displayName("example-cache")
71+
.ttl(Duration.ofSeconds(86400))
72+
.build();
73+
74+
CachedContent cachedContent = client.caches.create(modelId, config);
75+
cachedContent.name().ifPresent(System.out::println);
76+
cachedContent.usageMetadata().ifPresent(System.out::println);
77+
// Example response:
78+
// projects/111111111111/locations/global/cachedContents/1111111111111111111
79+
// CachedContentUsageMetadata{audioDurationSeconds=Optional.empty, imageCount=Optional[167],
80+
// textCount=Optional[153], totalTokenCount=Optional[43125],
81+
// videoDurationSeconds=Optional.empty}
82+
return cachedContent.name();
83+
}
84+
}
85+
}
86+
// [END googlegenaisdk_contentcache_create_with_txt_gcs_pdf]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.contentcache;
18+
19+
// [START googlegenaisdk_contentcache_delete]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.HttpOptions;
23+
24+
public class ContentCacheDelete {
25+
26+
public static void main(String[] args) {
27+
// TODO(developer): Replace these variables before running the sample.
28+
// E.g cacheName = "projects/111111111111/locations/global/cachedContents/1111111111111111111"
29+
String cacheName = "your-cache-name";
30+
contentCacheDelete(cacheName);
31+
}
32+
33+
// Deletes the cache using the specified cache name
34+
public static void contentCacheDelete(String cacheName) {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
try (Client client =
38+
Client.builder()
39+
.location("global")
40+
.vertexAI(true)
41+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
42+
.build()) {
43+
44+
client.caches.delete(cacheName, null);
45+
System.out.println("Deleted cache: " + cacheName);
46+
// Example response
47+
// Deleted cache: projects/111111111111/locations/global/cachedContents/1111111111111111111
48+
49+
}
50+
}
51+
}
52+
// [END googlegenaisdk_contentcache_delete]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.contentcache;
18+
19+
// [START googlegenaisdk_contentcache_list]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.CachedContent;
23+
import com.google.genai.types.HttpOptions;
24+
import com.google.genai.types.ListCachedContentsConfig;
25+
26+
public class ContentCacheList {
27+
28+
public static void main(String[] args) {
29+
contentCacheList();
30+
}
31+
32+
// Lists all cached contents
33+
public static void contentCacheList() {
34+
// Initialize client that will be used to send requests. This client only needs to be created
35+
// once, and can be reused for multiple requests.
36+
try (Client client =
37+
Client.builder()
38+
.location("global")
39+
.vertexAI(true)
40+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
41+
.build()) {
42+
43+
for (CachedContent content : client.caches.list(ListCachedContentsConfig.builder().build())) {
44+
content.name().ifPresent(name -> System.out.println("Name: " + name));
45+
content.model().ifPresent(model -> System.out.println("Model: " + model));
46+
content.updateTime().ifPresent(time -> System.out.println("Last updated at: " + time));
47+
content.expireTime().ifPresent(time -> System.out.println("Expires at: " + time));
48+
}
49+
// Example response:
50+
// Name: projects/111111111111/locations/global/cachedContents/1111111111111111111
51+
// Model:
52+
// projects/111111111111/locations/global/publishers/google/models/gemini-2.5-flash
53+
// Last updated at: 2025-07-28T21:54:19.125825Z
54+
// Expires at: 2025-08-04T21:54:18.328233500Z
55+
// ...
56+
}
57+
}
58+
}
59+
// [END googlegenaisdk_contentcache_list]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.contentcache;
18+
19+
// [START googlegenaisdk_contentcache_update]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.CachedContent;
23+
import com.google.genai.types.HttpOptions;
24+
import com.google.genai.types.UpdateCachedContentConfig;
25+
import java.time.Duration;
26+
import java.time.Instant;
27+
import java.time.temporal.ChronoUnit;
28+
29+
public class ContentCacheUpdate {
30+
31+
public static void main(String[] args) {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// E.g cacheName = "projects/111111111111/locations/global/cachedContents/1111111111111111111"
34+
String cacheName = "your-cache-name";
35+
contentCacheUpdate(cacheName);
36+
}
37+
38+
// Updates the cache using the specified cache resource name
39+
public static void contentCacheUpdate(String cacheName) {
40+
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
try (Client client =
44+
Client.builder()
45+
.location("global")
46+
.vertexAI(true)
47+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
48+
.build()) {
49+
50+
// Get info of the cached content
51+
CachedContent cachedContent = client.caches.get(cacheName, null);
52+
53+
cachedContent.expireTime()
54+
.ifPresent(expireTime -> System.out.println("Expire time: " + expireTime));
55+
// Example response
56+
// Expire time: 2025-07-29T23:39:49.227291Z
57+
58+
// Update expire time using TTL
59+
CachedContent updatedCachedContent =
60+
client.caches.update(
61+
cacheName,
62+
UpdateCachedContentConfig.builder().ttl(Duration.ofSeconds(36000)).build());
63+
64+
updatedCachedContent.expireTime()
65+
.ifPresent(expireTime -> System.out.println("Expire time after update: " + expireTime));
66+
// Example response
67+
// Expire time after update: 2025-07-30T08:40:33.537205Z
68+
69+
// Update expire time using specific time stamp
70+
Instant nextWeek = Instant.now().plus(7, ChronoUnit.DAYS);
71+
updatedCachedContent =
72+
client.caches.update(
73+
cacheName, UpdateCachedContentConfig.builder().expireTime(nextWeek).build());
74+
75+
updatedCachedContent
76+
.expireTime()
77+
.ifPresent(expireTime -> System.out.println("Expire time after update: " + expireTime));
78+
// Example response
79+
// Expire time after update: 2025-08-05T22:40:33.713988900Z
80+
81+
System.out.println("Updated cache: " + cacheName);
82+
}
83+
}
84+
}
85+
// [END googlegenaisdk_contentcache_update]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.contentcache;
18+
19+
// [START googlegenaisdk_contentcache_use_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.GenerateContentConfig;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.HttpOptions;
25+
26+
public class ContentCacheUseWithText {
27+
28+
public static void main(String[] args) {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String modelId = "gemini-2.5-flash";
31+
// E.g cacheName = "projects/111111111111/locations/global/cachedContents/1111111111111111111"
32+
String cacheName = "your-cache-name";
33+
contentCacheUseWithText(modelId, cacheName);
34+
}
35+
36+
// Shows how to generate text using cached content
37+
public static String contentCacheUseWithText(String modelId, String cacheName) {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
try (Client client =
41+
Client.builder()
42+
.location("global")
43+
.vertexAI(true)
44+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
45+
.build()) {
46+
47+
GenerateContentResponse response =
48+
client.models.generateContent(
49+
modelId,
50+
"Summarize the pdfs",
51+
GenerateContentConfig.builder().cachedContent(cacheName).build());
52+
53+
System.out.println(response.text());
54+
// Example response
55+
// The Gemini family of multimodal models from Google DeepMind demonstrates remarkable
56+
// capabilities across various
57+
// modalities, including image, audio, video, and text....
58+
return response.text();
59+
}
60+
}
61+
}
62+
// [END googlegenaisdk_contentcache_use_with_txt]

0 commit comments

Comments
 (0)